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

ChangeLog
lib/xgethostname.c

index d091eb10601cd6c98e930b34c4d5afdbc9250c48..c8dd18a7d70f7fdf74430b6032e25602ce8bf36e 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2019-07-05  Bruno Haible  <bruno@clisp.org>
+
+       xgethostname: Don't return an excessive memory allocation.
+       * lib/xgethostname.c (xgethostname): Shrink the hostname buffer before
+       returning it.
+
 2019-07-05  Bruno Haible  <bruno@clisp.org>
 
        areadlinkat-with-size: Don't return an excessive memory allocation.
index eba34b8867ebe5fa356085da032089f072301077..4bcb00f633a3d2b324be185bb27486cb73279913 100644 (file)
@@ -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;
 }