util/os_file: resize buffer to what was actually needed

Fixes: 316964709e "util: add os_read_file() helper"
Reported-by: Jason Ekstrand <jason@jlekstrand.net>
Signed-off-by: Eric Engestrom <eric.engestrom@intel.com>
Reviewed-by: Tapani Pälli <tapani.palli@intel.com>
Reviewed-by: Jason Ekstrand <jason@jlekstrand.net>
This commit is contained in:
Eric Engestrom 2019-05-01 11:51:01 +01:00 committed by Eric Engestrom
parent 2743e34f20
commit 955c63d364

View file

@ -92,6 +92,17 @@ os_read_file(const char *filename)
if (actually_read > 0)
offset += actually_read;
/* Final resize to actual size */
len = offset + 1;
char *newbuf = realloc(buf, len);
if (!newbuf) {
free(buf);
close(fd);
errno = -ENOMEM;
return NULL;
}
buf = newbuf;
buf[offset] = '\0';
return buf;