From 4f01415d3b9e1667d06c218dbd9080827fce4c23 Mon Sep 17 00:00:00 2001 From: Dragan Dosen Date: Thu, 18 Jun 2020 16:56:47 +0200 Subject: [PATCH] MINOR: peers: do not use localpeer as an array anymore It is now dynamically allocated by using strdup(). --- include/haproxy/global.h | 2 +- src/haproxy.c | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 5 deletions(-) diff --git a/include/haproxy/global.h b/include/haproxy/global.h index 339214c4a..23ce30573 100644 --- a/include/haproxy/global.h +++ b/include/haproxy/global.h @@ -45,7 +45,7 @@ extern const struct linger nolinger; extern int stopping; /* non zero means stopping in progress */ extern int killed; /* >0 means a hard-stop is triggered, >1 means hard-stop immediately */ extern char hostname[MAX_HOSTNAME_LEN]; -extern char localpeer[MAX_HOSTNAME_LEN]; +extern char *localpeer; extern unsigned int warned; /* bitfield of a few warnings to emit just once */ extern volatile unsigned long sleeping_thread_mask; extern struct list proc_list; /* list of process in mworker mode */ diff --git a/src/haproxy.c b/src/haproxy.c index 22c2fe545..1efa3acac 100644 --- a/src/haproxy.c +++ b/src/haproxy.c @@ -215,7 +215,7 @@ const int one = 1; const struct linger nolinger = { .l_onoff = 1, .l_linger = 0 }; char hostname[MAX_HOSTNAME_LEN]; -char localpeer[MAX_HOSTNAME_LEN]; +char *localpeer = NULL; static char **old_argv = NULL; /* previous argv but cleaned up */ @@ -1728,8 +1728,11 @@ static void init(int argc, char **argv) */ memset(hostname, 0, sizeof(hostname)); gethostname(hostname, sizeof(hostname) - 1); - memset(localpeer, 0, sizeof(localpeer)); - memcpy(localpeer, hostname, (sizeof(hostname) > sizeof(localpeer) ? sizeof(localpeer) : sizeof(hostname)) - 1); + + if ((localpeer = strdup(hostname)) == NULL) { + ha_alert("Cannot allocate memory for local peer.\n"); + exit(EXIT_FAILURE); + } setenv("HAPROXY_LOCALPEER", localpeer, 1); /* we were in mworker mode, we should restart in mworker mode */ @@ -1955,7 +1958,11 @@ static void init(int argc, char **argv) case 'm' : global.rlimit_memmax_all = atol(*argv); break; case 'N' : cfg_maxpconn = atol(*argv); break; case 'L' : - strncpy(localpeer, *argv, sizeof(localpeer) - 1); + free(localpeer); + if ((localpeer = strdup(*argv)) == NULL) { + ha_alert("Cannot allocate memory for local peer.\n"); + exit(EXIT_FAILURE); + } setenv("HAPROXY_LOCALPEER", localpeer, 1); break; case 'f' : @@ -2840,6 +2847,7 @@ void deinit(void) free(global.node); global.node = NULL; free(global.desc); global.desc = NULL; free(oldpids); oldpids = NULL; + free(localpeer); localpeer = NULL; task_destroy(idle_conn_task); idle_conn_task = NULL;