]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: cfgparse: load_cfg_in_mem: fix null ptr dereference reported by coverity
authorValentine Krasnobaeva <vkrasnobaeva@haproxy.com>
Thu, 8 Aug 2024 14:34:54 +0000 (16:34 +0200)
committerWilly Tarreau <w@1wt.eu>
Thu, 8 Aug 2024 17:54:12 +0000 (19:54 +0200)
This helps to optimize a bit load_cfg_in_mem() and fixes the potential null ptr
dereference in fread() call. If (read_bytes + bytes_to_read) equals to initial
chunk_size (zero), realloc is never called, *cfg_content keeps its NULL value.

So, let's assure that initial number of bytes to read
(read_bytes + bytes_to_read) is stricly positive, when we enter into loop at
the first time.

src/cfgparse.c

index 24c22f9cc473da9cbd9a5ebc4a07e9c839a37444..bf43b02fd19599d5043e640d4c82aa92e0580f31 100644 (file)
@@ -1771,10 +1771,9 @@ ssize_t load_cfg_in_mem(char *filename, char **cfg_content)
                return -1;
        }
 
-       if (file_stat.st_size) {
-               /* as we need to read EOF to have feof(f)=1 */
-               bytes_to_read = file_stat.st_size + 1;
-       }
+       if (file_stat.st_size > chunk_size)
+               bytes_to_read = file_stat.st_size;
+
 
        if ((f = fopen(filename,"r")) == NULL) {
                ha_alert("Could not open configuration file %s : %s\n",