Fix parsing empty QUERY_STRING master
authorPetr Písař <petr.pisar@atlas.cz>
Sun, 20 Aug 2017 20:15:25 +0000 (22:15 +0200)
committerJoey Schulze <joey@infodrom.org>
Fri, 8 Sep 2017 19:42:06 +0000 (21:42 +0200)
If an CGI script is requested vith GET method without any query parameters,
the QUERY_STRING environment value will be an empty string. The old code
reported an error in this case by returning NULL from cgiInit(). This
patch fixes it.

This patch also fixes a memory leak in cgiReadVariables(). The query
string duplicated into line variable was never deallocated. This is
fixed now. Actually in any error raised from cgiReadVariables() forgets
to deallocted line and other variables but since it's an error state for
short-time living CGI script I did not bother fixing them.

cgi.c

diff --git a/cgi.c b/cgi.c
index 1f6b6a3..3ed0af0 100644 (file)
--- a/cgi.c
+++ b/cgi.c
@@ -534,7 +534,7 @@ s_cgi *cgiReadVariables ()
            return NULL;
     } else if (cp && !strcmp(cp, "GET")) {
        esp = getenv("QUERY_STRING");
-       if (esp && strlen(esp)) {
+       if (esp) {
            if ((line = (char *)malloc (strlen(esp)+2)) == NULL)
                return NULL;
            sprintf (line, "%s", esp);
@@ -662,6 +662,7 @@ s_cgi *cgiReadVariables ()
        cp = ++ip;
     }
 
+    free(line);
     res->vars = result;
     res->cookies = NULL;
     res->files = NULL;