>From 98802d166215602a88988ae566259edf4f67efe8 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 6 Jul 2019 00:43:01 +0200 Subject: [PATCH 3/5] xgethostname: Don't return an excessive memory allocation. * lib/xgethostname.c (xgethostname): Shrink the hostname buffer before returning it. --- ChangeLog | 6 ++++++ lib/xgethostname.c | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/ChangeLog b/ChangeLog index d091eb1..c8dd18a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2019-07-05 Bruno Haible + xgethostname: Don't return an excessive memory allocation. + * lib/xgethostname.c (xgethostname): Shrink the hostname buffer before + returning it. + +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. diff --git a/lib/xgethostname.c b/lib/xgethostname.c index eba34b8..4bcb00f 100644 --- a/lib/xgethostname.c +++ b/lib/xgethostname.c @@ -70,5 +70,16 @@ xgethostname (void) } } + /* Shrink HOSTNAME before returning it. */ + { + size_t actual_size = strlen (hostname) + 1; + if (actual_size < size) + { + char *shrinked_hostname = realloc (hostname, actual_size); + if (shrinked_hostname != NULL) + hostname = shrinked_hostname; + } + } + return hostname; } -- 2.7.4