]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: tools: free previously allocated strings on strdup failure in backup_env()
authorWilly Tarreau <w@1wt.eu>
Sun, 26 Apr 2026 20:48:17 +0000 (22:48 +0200)
committerWilly Tarreau <w@1wt.eu>
Mon, 27 Apr 2026 12:44:29 +0000 (14:44 +0200)
When strdup() fails after some entries have already been strdup'd, the function
returned -1 without freeing previously allocated strings. Added cleanup loop to
free all previously strdup'd entries and reset init_env.

This can be backported to 3.1.

src/tools.c

index cdd9cbe5fcb8c1dbaf1c43bef4cd69dbbb76f051..741622fa65916723b022f61bb5616a984173e6f0 100644 (file)
@@ -7562,6 +7562,13 @@ int backup_env(void)
                if (*tmp == NULL) {
                        ha_alert("Cannot allocate memory to backup env variable '%s'.\n",
                                 *env);
+                       tmp = init_env;
+                       while (*tmp) {
+                               free(*tmp);
+                               tmp++;
+                       }
+                       free(init_env);
+                       init_env = NULL;
                        return -1;
                }
                tmp++;