Fix memory leaks found by valgrind
authorMichel Stam <michel@reverze.net>
Sun, 4 May 2014 20:28:50 +0000 (22:28 +0200)
committerJoey Schulze <joey@infodrom.org>
Fri, 8 Sep 2017 19:36:56 +0000 (21:36 +0200)
aux.c
cgi.c

diff --git a/aux.c b/aux.c
index a808b9d..dfb0f96 100644 (file)
--- a/aux.c
+++ b/aux.c
@@ -117,6 +117,7 @@ char *cgiGetLine (FILE *stream)
 {
     static char *line = NULL;
     static size_t size = 0;
 {
     static char *line = NULL;
     static size_t size = 0;
+    int len;
     char buf[BUFSIZE];
     char *cp;
 
     char buf[BUFSIZE];
     char *cp;
 
@@ -129,25 +130,35 @@ char *cgiGetLine (FILE *stream)
 
     while (!feof (stream)) {
       if ((cp = fgets (buf, sizeof (buf), stream)) == NULL)
 
     while (!feof (stream)) {
       if ((cp = fgets (buf, sizeof (buf), stream)) == NULL)
+      {
+       free(line);
+       line = NULL;
        return NULL;
        return NULL;
+      }
 
       if (strlen(line)+strlen(buf)+1 > size) {
        if ((cp = (char *)realloc (line, size + BUFSIZE)) == NULL)
          return line;
 
       if (strlen(line)+strlen(buf)+1 > size) {
        if ((cp = (char *)realloc (line, size + BUFSIZE)) == NULL)
          return line;
+       cp[size] = '\0';
        size += BUFSIZE;
        line = cp;
       }
 
       strcat (line, buf);
        size += BUFSIZE;
        line = cp;
       }
 
       strcat (line, buf);
-      if (line[strlen(line)-1] == '\n') {
-       line[strlen(line)-1] = '\0';
-       if (line[strlen(line)-1] == '\r')
-         line[strlen(line)-1] = '\0';
+      len = strlen(line);
+      if (len && line[len-1] == '\n') {
+       line[len-1] = '\0';
+       len --;
+       if (len && line[len-1] == '\r')
+         line[len-1] = '\0';
        cgiDebugOutput (4, "Read line '%s'", line);
        return line;
       }
     }
 
        cgiDebugOutput (4, "Read line '%s'", line);
        return line;
       }
     }
 
+    free(line);
+    line = NULL;
+
     return NULL;
 }
 
     return NULL;
 }
 
diff --git a/cgi.c b/cgi.c
index ad555c2..1f6b6a3 100644 (file)
--- a/cgi.c
+++ b/cgi.c
@@ -69,6 +69,7 @@ int cgiSetHeader (const char *name, const char *value)
     memset(pivot, 0, (cp-name + vp-value + 5) * sizeof (char));
 
     strncpy (pivot, name, cp-name);
     memset(pivot, 0, (cp-name + vp-value + 5) * sizeof (char));
 
     strncpy (pivot, name, cp-name);
+    pivot[cp-name] = '\0';
     strncat (pivot, ": ", 2);
     strncat (pivot, value, vp-value);
     strncat (pivot, "\r\n", 2);
     strncat (pivot, ": ", 2);
     strncat (pivot, value, vp-value);
     strncat (pivot, "\r\n", 2);
@@ -100,11 +101,19 @@ int cgiSetType (const char *type)
 void cgiHeader ()
 {
     if (cgiType)
 void cgiHeader ()
 {
     if (cgiType)
+    {
        printf ("Content-type: %s\r\n", cgiType);
        printf ("Content-type: %s\r\n", cgiType);
+       free(cgiType);
+       cgiType = NULL;
+    }
     else
        printf ("Content-type: text/html\r\n");
     if (cgiHeaderString)
     else
        printf ("Content-type: text/html\r\n");
     if (cgiHeaderString)
+    {
        printf ("%s", cgiHeaderString);
        printf ("%s", cgiHeaderString);
+       free(cgiHeaderString);
+       cgiHeaderString = NULL;
+    }
     printf ("\r\n");
 }
 
     printf ("\r\n");
 }