]> Savannah Git Hosting - gnulib.git/commitdiff
areadlink-with-size: Don't return an excessive memory allocation.
authorBruno Haible <bruno@clisp.org>
Fri, 5 Jul 2019 22:39:07 +0000 (00:39 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 5 Jul 2019 22:39:07 +0000 (00:39 +0200)
Reported by Andreas Dilger <adilger@whamcloud.com>.

* lib/areadlink-with-size.c (areadlink_with_size): Shrink the buffer
before returning it.

ChangeLog
lib/areadlink-with-size.c

index d6209efafc970f62baa9993786204b0ba5b28276..c0ee6cec67a06953d9468b7cb6efcbdb9ab360f2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,10 @@
+2019-07-05  Bruno Haible  <bruno@clisp.org>
+
+       areadlink-with-size: Don't return an excessive memory allocation.
+       Reported by Andreas Dilger <adilger@whamcloud.com>.
+       * lib/areadlink-with-size.c (areadlink_with_size): Shrink the buffer
+       before returning it.
+
 2019-07-03  Bruno Haible  <bruno@clisp.org>
 
        renameatu: Fix test failure on MSVC.
index eacad3f613deb4f7805bfd0bac8edcd9a2796ca5..364cc085845f261dc9abf67e11efdc6cc34c45e8 100644 (file)
@@ -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;
         }