From: Bruno Haible Date: Fri, 5 Jul 2019 22:39:07 +0000 (+0200) Subject: areadlink-with-size: Don't return an excessive memory allocation. X-Git-Tag: v1.0~4795 X-Git-Url: https://gitweb.git.savannah.gnu.org/gitweb/?a=commitdiff_plain;h=eb76defb19b3cc01e12d77d8cc96d402b9b5097d;p=gnulib.git areadlink-with-size: Don't return an excessive memory allocation. Reported by Andreas Dilger . * lib/areadlink-with-size.c (areadlink_with_size): Shrink the buffer before returning it. --- diff --git a/ChangeLog b/ChangeLog index d6209efafc..c0ee6cec67 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,10 @@ +2019-07-05 Bruno Haible + + areadlink-with-size: Don't return an excessive memory allocation. + Reported by Andreas Dilger . + * lib/areadlink-with-size.c (areadlink_with_size): Shrink the buffer + before returning it. + 2019-07-03 Bruno Haible renameatu: Fix test failure on MSVC. diff --git a/lib/areadlink-with-size.c b/lib/areadlink-with-size.c index eacad3f613..364cc08584 100644 --- a/lib/areadlink-with-size.c +++ b/lib/areadlink-with-size.c @@ -87,6 +87,13 @@ areadlink_with_size (char const *file, size_t size) if (link_length < buf_size) { buffer[link_length] = 0; + /* Shrink BUFFER before returning it. */ + if (link_length + 1 < buf_size) + { + char *shrinked_buffer = realloc (buffer, link_length + 1); + if (shrinked_buffer != NULL) + buffer = shrinked_buffer; + } return buffer; }