]> Savannah Git Hosting - gnulib.git/commitdiff
areadlinkat-with-size: Don't return an excessive memory allocation.
authorBruno Haible <bruno@clisp.org>
Fri, 5 Jul 2019 22:40:34 +0000 (00:40 +0200)
committerBruno Haible <bruno@clisp.org>
Fri, 5 Jul 2019 22:40:34 +0000 (00:40 +0200)
* lib/areadlinkat-with-size.c (areadlinkat_with_size): Shrink the buffer
before returning it.

ChangeLog
lib/areadlinkat-with-size.c

index c0ee6cec67a06953d9468b7cb6efcbdb9ab360f2..d091eb10601cd6c98e930b34c4d5afdbc9250c48 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2019-07-05  Bruno Haible  <bruno@clisp.org>
+
+       areadlinkat-with-size: Don't return an excessive memory allocation.
+       * lib/areadlinkat-with-size.c (areadlinkat_with_size): Shrink the buffer
+       before returning it.
+
 2019-07-05  Bruno Haible  <bruno@clisp.org>
 
        areadlink-with-size: Don't return an excessive memory allocation.
index ed00b98e4df8b277b96837f05b471247cfb63307..5b2bccc4a5508b4ebcc5cf573b1019b5ad07169e 100644 (file)
@@ -92,6 +92,13 @@ areadlinkat_with_size (int fd, 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;
         }