diff options
author | Bruno Haible <bruno@clisp.org> | 2010-08-28 16:22:14 +0200 |
---|---|---|
committer | Bruno Haible <bruno@clisp.org> | 2010-08-28 16:22:14 +0200 |
commit | 32a3eff7f5191d76a82be7db1190a98ed9ef55d0 (patch) | |
tree | a0c4f1c8841e0b4c5cb75b8f5d1e0ef9d8cb8680 /lib | |
parent | 3e53db21df4287b43a46ad27fc2fac56b9194ffc (diff) | |
download | gnulib-32a3eff7f5191d76a82be7db1190a98ed9ef55d0.tar.gz |
read-file: Don't occupy too much unused memory.
Diffstat (limited to 'lib')
-rw-r--r-- | lib/read-file.c | 9 |
1 files changed, 9 insertions, 0 deletions
diff --git a/lib/read-file.c b/lib/read-file.c index 27241b4504..0a15c5a457 100644 --- a/lib/read-file.c +++ b/lib/read-file.c @@ -119,6 +119,15 @@ fread_file (FILE * stream, size_t * length) save_errno = errno; if (ferror (stream)) break; + + /* Shrink the allocated memory if possible. */ + if (size + 1 < alloc) + { + char *smaller_buf = realloc (buf, size + 1); + if (smaller_buf != NULL) + buf = smaller_buf; + } + buf[size] = '\0'; *length = size; return buf; |