]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
BUG/MINOR: server: check return value of fgets() in apply_server_state()
authorDragan Dosen <ddosen@haproxy.com>
Wed, 4 Nov 2015 22:03:26 +0000 (23:03 +0100)
committerWilly Tarreau <w@1wt.eu>
Thu, 5 Nov 2015 09:39:09 +0000 (10:39 +0100)
fgets() can return NULL on error or when EOF occurs. This patch adds a
check of fgets() return value and displays a warning if the first line of
the server state file can not be read. Additionally, we make sure to close
the previously opened file descriptor.

src/server.c

index 224d536f1973e704c301e2184616792ddf1fc296..b45a7fd9d7661c552fb5d49d00197f3ea65fe992 100644 (file)
@@ -2330,12 +2330,16 @@ void apply_server_state(void)
                version = 0;
 
                /* first character of first line of the file must contain the version of the export */
-               fgets(mybuf, SRV_STATE_LINE_MAXLEN, f);
+               if (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f) == NULL) {
+                       Warning("Can't read first line of the server state file '%s'\n", filepath);
+                       goto fileclose;
+               }
+
                cur = mybuf;
                version = atoi(cur);
                if ((version < SRV_STATE_FILE_VERSION_MIN) ||
                    (version > SRV_STATE_FILE_VERSION_MAX))
-                       continue;
+                       goto fileclose;
 
                while (fgets(mybuf, SRV_STATE_LINE_MAXLEN, f)) {
                        int bk_f_forced_id = 0;
@@ -2462,6 +2466,7 @@ void apply_server_state(void)
                        /* now we can proceed with server's state update */
                        srv_update_state(srv, version, srv_params);
                }
+fileclose:
                fclose(f);
        }
 }