From 8f91b5cf3514d664289afaace0d6f832b9608f20 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 6 Jul 2019 00:40:34 +0200 Subject: [PATCH] areadlinkat-with-size: Don't return an excessive memory allocation. * lib/areadlinkat-with-size.c (areadlinkat_with_size): Shrink the buffer before returning it. --- ChangeLog | 6 ++++++ lib/areadlinkat-with-size.c | 7 +++++++ 2 files changed, 13 insertions(+) diff --git a/ChangeLog b/ChangeLog index c0ee6cec67..d091eb1060 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +2019-07-05 Bruno Haible + + 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 areadlink-with-size: Don't return an excessive memory allocation. diff --git a/lib/areadlinkat-with-size.c b/lib/areadlinkat-with-size.c index ed00b98e4d..5b2bccc4a5 100644 --- a/lib/areadlinkat-with-size.c +++ b/lib/areadlinkat-with-size.c @@ -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; } -- 2.39.5