diff options
author | Eric Engestrom <eric.engestrom@intel.com> | 2019-05-01 11:51:01 +0100 |
---|---|---|
committer | Juan A. Suarez Romero <jasuarez@igalia.com> | 2019-06-21 07:39:06 +0000 |
commit | ebd90fc7e00b50ec1d153aa6b0be3482a3f4d662 (patch) | |
tree | cdc824b15ca2b62a774f2f67a6231a2cac053a26 | |
parent | 25a34df61439b25645d03510d6354cb1f5e8a185 (diff) |
util/os_file: resize buffer to what was actually needed
Fixes: 316964709e21286c2af5 "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>
(cherry picked from commit 955c63d3643f30d7db0c5d16e06a5eda4f62f889)
-rw-r--r-- | src/util/os_file.c | 11 |
1 files changed, 11 insertions, 0 deletions
diff --git a/src/util/os_file.c b/src/util/os_file.c index da17d12b50e..18727d1121f 100644 --- a/src/util/os_file.c +++ b/src/util/os_file.c @@ -92,6 +92,17 @@ os_read_file(const char *filename) if (read > 0) offset += 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; |