]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] force null-termination of hostname
authorWilly Tarreau <w@1wt.eu>
Fri, 12 Mar 2010 20:58:54 +0000 (21:58 +0100)
committerWilly Tarreau <w@1wt.eu>
Fri, 12 Mar 2010 20:58:54 +0000 (21:58 +0100)
Marcello Gorlani reported that at least on FreeBSD, a long hostname
was reported with garbage on the stats page. POSIX does not make it
mandatory for gethostname() to NULL-terminate the string in case of
truncation, and at least FreeBSD appears not to do it. So let's
force null-termination to keep safe.

src/haproxy.c

index e82ab5656a82e0c648bb4233cf9c0bbd32f1b5ab..17ee27d5af76fb26cb6df55289994fb5bae91210 100644 (file)
@@ -147,7 +147,7 @@ const int zero = 0;
 const int one = 1;
 const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 };
 
-char hostname[MAX_HOSTNAME_LEN] = "";
+char hostname[MAX_HOSTNAME_LEN];
 
 
 /*********************************************************************/
@@ -542,7 +542,12 @@ void init(int argc, char **argv)
        if (LIST_ISEMPTY(&cfg_cfgfiles))
                usage(old_argv);
 
-       gethostname(hostname, MAX_HOSTNAME_LEN);
+       /* NB: POSIX does not make it mandatory for gethostname() to NULL-terminate
+        * the string in case of truncation, and at least FreeBSD appears not to do
+        * it.
+        */
+       memset(hostname, 0, sizeof(hostname));
+       gethostname(hostname, sizeof(hostname) - 1);
 
        have_appsession = 0;
        global.maxsock = 10; /* reserve 10 fds ; will be incremented by socket eaters */