]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cfgparse: remove line size limitation
authorWilliam Lallemand <wlallemand@haproxy.com>
Tue, 12 May 2015 12:25:37 +0000 (14:25 +0200)
committerWilly Tarreau <w@1wt.eu>
Tue, 12 May 2015 13:28:07 +0000 (15:28 +0200)
Remove the line size limitation of the configuration parser.  The buffer
is now allocated dynamically and grows when the line is too long.

src/cfgparse.c

index 8469dfd24862c8d6cfbc63b4058f1637f2f067c8..04cbd1d599599ffb0ceef9e5d57bac2eb28e0212 100644 (file)
@@ -6274,12 +6274,19 @@ out:
  */
 int readcfgfile(const char *file)
 {
-       char thisline[LINESIZE];
+       char *thisline;
+       int linesize = LINESIZE;
        FILE *f;
        int linenum = 0;
        int err_code = 0;
        struct cfg_section *cs = NULL;
        struct cfg_section *ics;
+       int readbytes = 0;
+
+       if ((thisline = malloc(sizeof(*thisline) * linesize)) == NULL) {
+               Alert("parsing [%s:%d] : out of memory.\n", file, linenum);
+               return -1;
+       }
 
        /* Register internal sections */
        if (!cfg_register_section("listen",   cfg_parse_listen) ||
@@ -6297,7 +6304,7 @@ int readcfgfile(const char *file)
        if ((f=fopen(file,"r")) == NULL)
                return -1;
 
-       while (fgets(thisline, sizeof(thisline), f) != NULL) {
+       while (fgets(thisline + readbytes, linesize - readbytes, f) != NULL) {
                int arg, kwm = KWM_STD;
                char *end;
                char *args[MAX_LINE_ARGS + 1];
@@ -6309,15 +6316,29 @@ int readcfgfile(const char *file)
 
                end = line + strlen(line);
 
-               if (end-line == sizeof(thisline)-1 && *(end-1) != '\n') {
+               if (end-line == linesize-1 && *(end-1) != '\n') {
                        /* Check if we reached the limit and the last char is not \n.
                         * Watch out for the last line without the terminating '\n'!
                         */
-                       Alert("parsing [%s:%d]: line too long, limit: %d.\n",
-                             file, linenum, (int)sizeof(thisline)-1);
-                       err_code |= ERR_ALERT | ERR_FATAL;
+                       char *newline;
+                       int newlinesize = linesize * 2;
+
+                       newline = realloc(thisline, sizeof(*thisline) * newlinesize);
+                       if (newline == NULL) {
+                               Alert("parsing [%s:%d]: line too long, cannot allocate memory.\n",
+                                     file, linenum);
+                               err_code |= ERR_ALERT | ERR_FATAL;
+                               continue;
+                       }
+
+                       readbytes = linesize - 1;
+                       linesize = newlinesize;
+                       thisline = newline;
+                       continue;
                }
 
+               readbytes = 0;
+
                /* skip leading spaces */
                while (isspace((unsigned char)*line))
                        line++;
@@ -6486,6 +6507,7 @@ int readcfgfile(const char *file)
                        break;
        }
        cursection = NULL;
+       free(thisline);
        fclose(f);
        return err_code;
 }