util/os_file: resize buffer to what was actually needed
authorEric Engestrom <eric.engestrom@intel.com>
Wed, 1 May 2019 10:51:01 +0000 (11:51 +0100)
committerEric Engestrom <eric@engestrom.ch>
Thu, 20 Jun 2019 21:49:30 +0000 (21:49 +0000)
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>
src/util/os_file.c

index a700f3a..756164c 100644 (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;