MINOR: cfgparse: tighten the scope of newnameserver variable, free it on error.
authorEric Salama <esalama@haproxy.com>
Fri, 13 Nov 2020 14:56:36 +0000 (15:56 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 13 Nov 2020 15:53:39 +0000 (16:53 +0100)
This should fix issue GH #931.

Also remove a misleading comment.

This commit can be backported as far as 1.9

(cherry picked from commit 9139ec34ed7aae4fe63926ca5c87a5b32b376488)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 6057016df764c7e7f218dcdd39d024ef65cd1d43)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>
(cherry picked from commit 3f2053a0a243690afa685011e3e04458869567e8)
Signed-off-by: Christopher Faulet <cfaulet@haproxy.com>

src/cfgparse.c

index 22bcced..054d4f0 100644 (file)
@@ -970,7 +970,6 @@ out:
 int cfg_parse_resolvers(const char *file, int linenum, char **args, int kwm)
 {
        static struct dns_resolvers *curr_resolvers = NULL;
-       struct dns_nameserver *newnameserver = NULL;
        const char *err;
        int err_code = 0;
        char *errmsg = NULL;
@@ -1031,6 +1030,7 @@ int cfg_parse_resolvers(const char *file, int linenum, char **args, int kwm)
                HA_SPIN_INIT(&curr_resolvers->lock);
        }
        else if (strcmp(args[0], "nameserver") == 0) { /* nameserver definition */
+               struct dns_nameserver *newnameserver = NULL;
                struct sockaddr_storage *sk;
                int port1, port2;
                struct protocol *proto;
@@ -1104,6 +1104,7 @@ int cfg_parse_resolvers(const char *file, int linenum, char **args, int kwm)
                newnameserver->addr = *sk;
        }
        else if (strcmp(args[0], "parse-resolv-conf") == 0) {
+               struct dns_nameserver *newnameserver = NULL;
                const char *whitespace = "\r\n\t ";
                char *resolv_line = NULL;
                int resolv_linenum = 0;
@@ -1192,6 +1193,7 @@ int cfg_parse_resolvers(const char *file, int linenum, char **args, int kwm)
                        if (newnameserver->conf.file == NULL) {
                                ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
                                err_code |= ERR_ALERT | ERR_FATAL;
+                               free(newnameserver);
                                goto resolv_out;
                        }
 
@@ -1199,6 +1201,8 @@ int cfg_parse_resolvers(const char *file, int linenum, char **args, int kwm)
                        if (newnameserver->id == NULL) {
                                ha_alert("parsing [/etc/resolv.conf:%d] : out of memory.\n", resolv_linenum);
                                err_code |= ERR_ALERT | ERR_FATAL;
+                               free((char *)newnameserver->conf.file);
+                               free(newnameserver);
                                goto resolv_out;
                        }
 
@@ -1348,7 +1352,7 @@ resolv_out:
                        err_code |= ERR_ALERT | ERR_FATAL;
                        goto out;
                }
-       } /* neither "nameserver" nor "resolvers" */
+       }
        else if (*args[0] != 0) {
                ha_alert("parsing [%s:%d] : unknown keyword '%s' in '%s' section\n", file, linenum, args[0], cursection);
                err_code |= ERR_ALERT | ERR_FATAL;