]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: peers: do not use localpeer as an array anymore
authorDragan Dosen <ddosen@haproxy.com>
Thu, 18 Jun 2020 14:56:47 +0000 (16:56 +0200)
committerWilliam Lallemand <wlallemand@haproxy.org>
Fri, 19 Jun 2020 09:37:11 +0000 (11:37 +0200)
It is now dynamically allocated by using strdup().

include/haproxy/global.h
src/haproxy.c

index 339214c4af67ee96660bc13e0746078b98cd6c91..23ce3057336945587033035739a0ab6a3b642beb 100644 (file)
@@ -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 */
index 22c2fe54513a2344e38a5c2e512e18a3dda052f7..1efa3acac8bba7c6a1d90cf6866d77cf5670eca4 100644 (file)
@@ -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;