]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cfgparse: Warn on truncated lines / files
authorTim Duesterhus <tim@bastelstu.be>
Mon, 22 Jun 2020 20:57:45 +0000 (22:57 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 23 Jun 2020 03:14:59 +0000 (05:14 +0200)
As discussed on the list: https://www.mail-archive.com/haproxy@formilux.org/msg37698.html

This patch adds warnings to the configuration parser that detect the
following situations:

- A line being truncated by a null byte in the middle.
- A file not ending in a new line (and possibly being truncated).

src/cfgparse.c

index 4ccca14d2b3d85155ce970db60bb7a26250c5e44..257df05a1e78962ff200346155a04131e6760069 100644 (file)
@@ -1854,6 +1854,7 @@ int readcfgfile(const char *file)
        size_t outlen = 0;
        size_t outlinesize = 0;
        int fatal = 0;
+       int missing_lf = -1;
 
        if ((thisline = malloc(sizeof(*thisline) * linesize)) == NULL) {
                ha_alert("parsing [%s] : out of memory.\n", file);
@@ -1872,6 +1873,14 @@ next_line:
                char *args[MAX_LINE_ARGS + 1];
                char *line = thisline;
 
+               if (missing_lf != -1) {
+                       ha_warning("parsing [%s:%d]: Stray NUL character at position %d. "
+                                  "This will become a hard error in HAProxy 2.3.\n",
+                                  file, linenum, (missing_lf + 1));
+                       err_code |= ERR_WARN;
+                       missing_lf = -1;
+               }
+
                linenum++;
 
                if (fatal >= 50) {
@@ -1909,6 +1918,10 @@ next_line:
                        /* kill trailing LF */
                        *(end - 1) = 0;
                }
+               else {
+                       /* mark this line as truncated */
+                       missing_lf = end - line;
+               }
 
                /* skip leading spaces */
                while (isspace((unsigned char)*line))
@@ -2058,6 +2071,13 @@ next_line:
                }
        }
 
+       if (missing_lf != -1) {
+               ha_warning("parsing [%s:%d]: Missing LF on last line, file might have been truncated at position %d. "
+                          "This will become a hard error in HAProxy 2.3.\n",
+                          file, linenum, (missing_lf + 1));
+               err_code |= ERR_WARN;
+       }
+
        if (cs && cs->post_section_parser)
                err_code |= cs->post_section_parser();