]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cfgparse: Always check the section position
authorChristopher Faulet <cfaulet@haproxy.com>
Fri, 18 Nov 2022 14:46:06 +0000 (15:46 +0100)
committerChristopher Faulet <cfaulet@haproxy.com>
Fri, 18 Nov 2022 15:03:45 +0000 (16:03 +0100)
In diag mode, the section position is checked and a warning is emitted if a
global section is defined after any non-global one. Now, this check is
always performed. But the warning is still only emitted in diag mode. In
addition, the result of this check is now stored in a global variable, to be
used from anywhere.

The aim of this patch is to be able to restrict usage of some global
directives to the very first global sections. It will be useful to avoid
undefined behaviors. Indeed, some config parts may depend on global settings
and it is a problem if these settings are changed after.

include/haproxy/cfgparse.h
src/cfgparse.c

index 7c3d128bfb8fc54fd5558c6603eb6967def504ac..b569b4dbc6be778786e5bbc4755c68ab59c85672 100644 (file)
@@ -94,6 +94,7 @@ extern int cfg_maxconn;
 extern char *cfg_scope;
 extern struct cfg_kw_list cfg_keywords;
 extern char *cursection;
+extern int non_global_section_parsed;
 
 int cfg_parse_global(const char *file, int linenum, char **args, int inv);
 int cfg_parse_listen(const char *file, int linenum, char **args, int inv);
index a7652e9e350dfc412f35d2937aefaec2e4e8cd70..998afcd5e690024f2caf2165905948fee970ef9e 100644 (file)
@@ -104,6 +104,7 @@ char *cursection = NULL;
 int cfg_maxpconn = 0;                   /* # of simultaneous connections per proxy (-N) */
 int cfg_maxconn = 0;                   /* # of simultaneous connections, (-n) */
 char *cfg_scope = NULL;                 /* the current scope during the configuration parsing */
+int non_global_section_parsed = 0;
 
 /* how to handle default paths */
 static enum default_path_mode {
@@ -1577,16 +1578,14 @@ cfg_parse_track_sc_num(unsigned int *track_sc_num,
  * Detect a global section after a non-global one and output a diagnostic
  * warning.
  */
-static void check_section_position(char *section_name,
-                                   const char *file, int linenum,
-                                   int *non_global_parsed)
+static void check_section_position(char *section_name, const char *file, int linenum)
 {
        if (strcmp(section_name, "global") == 0) {
-               if (*non_global_parsed == 1)
+               if ((global.mode & MODE_DIAG) && non_global_section_parsed == 1)
                        _ha_diag_warning("parsing [%s:%d] : global section detected after a non-global one, the prevalence of their statements is unspecified\n", file, linenum);
        }
-       else if (*non_global_parsed == 0) {
-               *non_global_parsed = 1;
+       else if (non_global_section_parsed == 0) {
+               non_global_section_parsed = 1;
        }
 }
 
@@ -1743,7 +1742,6 @@ int readcfgfile(const char *file)
        int missing_lf = -1;
        int nested_cond_lvl = 0;
        enum nested_cond_state nested_conds[MAXNESTEDCONDS];
-       int non_global_section_parsed = 0;
        char *errmsg = NULL;
 
        global.cfg_curr_line = 0;
@@ -2455,12 +2453,7 @@ next_line:
                                cs = ics;
                                free(global.cfg_curr_section);
                                global.cfg_curr_section = strdup(*args[1] ? args[1] : args[0]);
-
-                               if (global.mode & MODE_DIAG) {
-                                       check_section_position(args[0], file, linenum,
-                                                              &non_global_section_parsed);
-                               }
-
+                               check_section_position(args[0], file, linenum);
                                break;
                        }
                }