[PATCH] Fix parsing empty QUERY_STRING

From: Petr Písař <petr.pisar_at_atlas.cz>
Date: Sun, 20 Aug 2017 22:15:25 +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 | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/cgi.c b/cgi.c
index e390b37..5fe00e7 100644
--- a/cgi.c
+++ b/cgi.c
@@ -525,7 +525,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);
@@ -653,6 +653,7 @@ s_cgi *cgiReadVariables ()
 	cp = ++ip;
     }
 
+    free(line);
     res->vars = result;
     res->cookies = NULL;
     res->files = NULL;
-- 
2.13.5
Received on Sun Aug 20 2017 - 22:15:25 CEST

This archive was generated by hypermail 2.2.0 : Sun Aug 20 2017 - 22:15:50 CEST