From 98802d166215602a88988ae566259edf4f67efe8 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sat, 6 Jul 2019 00:43:01 +0200 Subject: [PATCH] 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 d091eb1060..c8dd18a7d7 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,9 @@ +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. diff --git a/lib/xgethostname.c b/lib/xgethostname.c index eba34b8867..4bcb00f633 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.39.5