From 873c84f0722b4edf98e46d6c966c0d3618bfb2ac Mon Sep 17 00:00:00 2001 From: Niclas Zeising Date: Wed, 31 Aug 2016 19:12:27 +0200 Subject: Fix build on FreeBSD. FreeBSD lacks the constant HOST_NAME_MAX, instead using sysconf(3) to find out the value of the maximum host name length at run time. Patch to use this instead of HOST_NAME_MAX. This brings with it the need to use malloc instead of using a statically sized buffer for the host name, since the size of the buffer cannot be known at run time. Errors from sysconf or malloc just means that the entire block of code is skipped over (the same way it's skipped if the call to gethostname() fails), rather than returning any kind of error to the caller or logging an error message somewhere. --- src/winwidget.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/src/winwidget.c b/src/winwidget.c index 9495c7f..945c123 100644 --- a/src/winwidget.c +++ b/src/winwidget.c @@ -152,8 +152,9 @@ void winwidget_create_window(winwidget ret, int w, int h) pid_t pid; int x = 0; int y = 0; + long host_name_max; char *tmpname; - char hostname[HOST_NAME_MAX]; + char *hostname; D(("winwidget_create_window %dx%d\n", w, h)); @@ -278,11 +279,16 @@ void winwidget_create_window(winwidget ret, int w, int h) XChangeProperty(disp, ret->win, prop, XA_CARDINAL, sizeof(pid_t) * 8, PropModeReplace, (const unsigned char *)&pid, 1); - if (gethostname(hostname, HOST_NAME_MAX) == 0) { - hostname[HOST_NAME_MAX-1] = '\0'; - prop = XInternAtom(disp, "WM_CLIENT_MACHINE", False); - XChangeProperty(disp, ret->win, prop, XA_STRING, sizeof(char) * 8, - PropModeReplace, (unsigned char *)hostname, strlen(hostname)); + if ((host_name_max = sysconf(_SC_HOST_NAME_MAX)) != -1 ) { + if ((hostname = calloc(1, host_name_max + 1)) != NULL ) { + if (gethostname(hostname, host_name_max) == 0) { + hostname[host_name_max] = '\0'; + prop = XInternAtom(disp, "WM_CLIENT_MACHINE", False); + XChangeProperty(disp, ret->win, prop, XA_STRING, sizeof(char) * 8, + PropModeReplace, (unsigned char *)hostname, strlen(hostname)); + } + free(hostname); + } } XSetWMProtocols(disp, ret->win, &wmDeleteWindow, 1); -- cgit v1.2.3 From 5ab630b59fe449c451d3a6f024d3c997af020a22 Mon Sep 17 00:00:00 2001 From: Niclas Zeising Date: Wed, 31 Aug 2016 20:03:36 +0200 Subject: Only use sysconf() if HOST_NAME_MAX is undefined On some systsems sysconf() can return a very large value, unsuitable for use with malloc(). Only use sysconf() if HOST_NAME_MAX isn't avalable. --- src/winwidget.c | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/winwidget.c b/src/winwidget.c index 945c123..dc1fe13 100644 --- a/src/winwidget.c +++ b/src/winwidget.c @@ -152,9 +152,13 @@ void winwidget_create_window(winwidget ret, int w, int h) pid_t pid; int x = 0; int y = 0; - long host_name_max; char *tmpname; +#ifdef HOST_NAME_MAX + char hostname[HOST_NAME_MAX]; +#else /* ! HOST_NAME_MAX */ + long host_name_max; char *hostname; +#endif /* HOST_NAME_MAX */ D(("winwidget_create_window %dx%d\n", w, h)); @@ -279,6 +283,14 @@ void winwidget_create_window(winwidget ret, int w, int h) XChangeProperty(disp, ret->win, prop, XA_CARDINAL, sizeof(pid_t) * 8, PropModeReplace, (const unsigned char *)&pid, 1); +#ifdef HOST_NAME_MAX + if (gethostname(hostname, HOST_NAME_MAX) == 0) { + hostname[HOST_NAME_MAX-1] = '\0'; + prop = XInternAtom(disp, "WM_CLIENT_MACHINE", False); + XChangeProperty(disp, ret->win, prop, XA_STRING, sizeof(char) * 8, + PropModeReplace, (unsigned char *)hostname, strlen(hostname)); + } +#else /* ! HOST_NAME_MAX */ if ((host_name_max = sysconf(_SC_HOST_NAME_MAX)) != -1 ) { if ((hostname = calloc(1, host_name_max + 1)) != NULL ) { if (gethostname(hostname, host_name_max) == 0) { @@ -287,9 +299,10 @@ void winwidget_create_window(winwidget ret, int w, int h) XChangeProperty(disp, ret->win, prop, XA_STRING, sizeof(char) * 8, PropModeReplace, (unsigned char *)hostname, strlen(hostname)); } - free(hostname); + free(hostname); } } +#endif /* HOST_NAME_MAX */ XSetWMProtocols(disp, ret->win, &wmDeleteWindow, 1); winwidget_update_title(ret); -- cgit v1.2.3 From 96f5db936b65560333fed8eaee74c2e19b2c2e12 Mon Sep 17 00:00:00 2001 From: Daniel Friesel Date: Wed, 31 Aug 2016 20:23:31 +0200 Subject: winwidget: hostname always has a trailing null byte in the !HOST_NAME_MAX branch --- src/winwidget.c | 1 - 1 file changed, 1 deletion(-) diff --git a/src/winwidget.c b/src/winwidget.c index dc1fe13..758183b 100644 --- a/src/winwidget.c +++ b/src/winwidget.c @@ -294,7 +294,6 @@ void winwidget_create_window(winwidget ret, int w, int h) if ((host_name_max = sysconf(_SC_HOST_NAME_MAX)) != -1 ) { if ((hostname = calloc(1, host_name_max + 1)) != NULL ) { if (gethostname(hostname, host_name_max) == 0) { - hostname[host_name_max] = '\0'; prop = XInternAtom(disp, "WM_CLIENT_MACHINE", False); XChangeProperty(disp, ret->win, prop, XA_STRING, sizeof(char) * 8, PropModeReplace, (unsigned char *)hostname, strlen(hostname)); -- cgit v1.2.3