From 642a3b653f25f785887f1a9a71f335bc7ef21b11 Mon Sep 17 00:00:00 2001 From: Alistair Buxton Date: Sun, 9 Nov 2014 19:28:37 +0000 Subject: [PATCH] Improve the hostname fetching some more. Instead of allocating a fixed size array before getting the hostname, place it into a local array first, and then g_strdup it. This way the local array can be arbitrarily large, and the final buffer will never be any bigger than necessary. --- src/display.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/display.c b/src/display.c index 3c2e7ba..3c388f1 100644 --- a/src/display.c +++ b/src/display.c @@ -44,10 +44,6 @@ #include "client.h" #include "compositor.h" -#ifndef HOST_NAME_MAX -#define HOST_NAME_MAX 255 -#endif /* HOST_NAME_MAX */ - #ifndef CURSOR_ROOT #define CURSOR_ROOT XC_left_ptr #endif @@ -198,6 +194,7 @@ myDisplayInit (GdkDisplay *gdisplay) DisplayInfo *display; int major, minor; int dummy; + char nametmp[512]; display = g_new0 (DisplayInfo, 1); @@ -313,13 +310,16 @@ myDisplayInit (GdkDisplay *gdisplay) display->nb_screens = 0; display->current_time = CurrentTime; - display->hostname = g_new0 (gchar, (size_t) HOST_NAME_MAX + 1); - if (gethostname ((char *) display->hostname, HOST_NAME_MAX + 1)) + if (gethostname (nametmp, sizeof(nametmp) - 1)) { g_warning ("The display's hostname could not be determined."); - g_free (display->hostname); display->hostname = NULL; } + else + { + nametmp[sizeof(nametmp) - 1] = '\0'; + display->hostname = g_strdup(nametmp); + } compositorInitDisplay (display); -- 1.9.1