summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorEric Engestrom <[email protected]>2019-05-01 11:51:01 +0100
committerEric Engestrom <[email protected]>2019-06-20 21:49:30 +0000
commit955c63d3643f30d7db0c5d16e06a5eda4f62f889 (patch)
tree29951517a9eb4cb4d5efaffb1f1ef4e9606b8cca /src
parent2743e34f20f6c45094c88ba80d90fdf6bb010903 (diff)
util/os_file: resize buffer to what was actually needed
Fixes: 316964709e21286c2af5 "util: add os_read_file() helper" Reported-by: Jason Ekstrand <[email protected]> Signed-off-by: Eric Engestrom <[email protected]> Reviewed-by: Tapani Pälli <[email protected]> Reviewed-by: Jason Ekstrand <[email protected]>
Diffstat (limited to 'src')
-rw-r--r--src/util/os_file.c11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/util/os_file.c b/src/util/os_file.c
index a700f3aada3..756164c3dfe 100644
--- a/src/util/os_file.c
+++ b/src/util/os_file.c
@@ -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;