Let load_one_actions_file() use ssplit() like every other function
[privoxy.git] / cgi.c
diff --git a/cgi.c b/cgi.c
index 62de942..556aa43 100644 (file)
--- a/cgi.c
+++ b/cgi.c
@@ -1,4 +1,4 @@
-const char cgi_rcs[] = "$Id: cgi.c,v 1.147 2011/12/31 14:54:28 fabiankeil Exp $";
+const char cgi_rcs[] = "$Id: cgi.c,v 1.151 2012/06/08 15:07:53 fabiankeil Exp $";
 /*********************************************************************
  *
  * File        :  $Source: /cvsroot/ijbswa/current/cgi.c,v $
@@ -312,7 +312,7 @@ struct http_response *dispatch_cgi(struct client_state *csp)
    /* Note: "example.com" and "example.com." are equivalent hostnames. */
 
    /* Either the host matches CGI_SITE_1_HOST ..*/
-   if (  ( (0 == strcmpic(host, CGI_SITE_1_HOST))
+   if (   ( (0 == strcmpic(host, CGI_SITE_1_HOST))
          || (0 == strcmpic(host, CGI_SITE_1_HOST ".")))
        && (path[0] == '/'))
    {
@@ -320,7 +320,7 @@ struct http_response *dispatch_cgi(struct client_state *csp)
       path++;
    }
    /* Or it's the host part CGI_SITE_2_HOST, and the path CGI_SITE_2_PATH */
-   else if (( (0 == strcmpic(host, CGI_SITE_2_HOST))
+   else if ((  (0 == strcmpic(host, CGI_SITE_2_HOST))
             || (0 == strcmpic(host, CGI_SITE_2_HOST ".")))
           && (0 == strncmpic(path, CGI_SITE_2_PATH, strlen(CGI_SITE_2_PATH))))
    {
@@ -594,12 +594,36 @@ static struct http_response *dispatch_known_cgi(struct client_state * csp,
 static struct map *parse_cgi_parameters(char *argstring)
 {
    char *p;
-   char *vector[BUFFER_SIZE];
+   char **vector;
    int pairs, i;
    struct map *cgi_params;
 
+   /*
+    * XXX: This estimate is guaranteed to be high enough as we
+    *      let ssplit() ignore empty fields, but also a bit wasteful.
+    *      The same hack is used in get_last_url() so it looks like
+    *      a real solution is needed.
+    */
+   size_t max_segments = strlen(argstring) / 2;
+   if (max_segments == 0)
+   {
+      /*
+       * XXX: If the argstring is empty, there's really
+       *      no point in creating a param list, but currently
+       *      other parts of Privoxy depend on the list's existence.
+       */
+      max_segments = 1;
+   }
+   vector = malloc(max_segments * sizeof(char *));
+
+   if (NULL == vector)
+   {
+      return NULL;
+   }
+
    if (NULL == (cgi_params = new_map()))
    {
+      freez(vector);
       return NULL;
    }
 
@@ -613,7 +637,14 @@ static struct map *parse_cgi_parameters(char *argstring)
       *p = '\0';
    }
 
-   pairs = ssplit(argstring, "&", vector, SZ(vector), 1, 1);
+   pairs = ssplit(argstring, "&", vector, max_segments, 1, 1);
+   assert(pairs != -1);
+   if (pairs == -1)
+   {
+      freez(vector);
+      free_map(cgi_params);
+      return NULL;
+   }
 
    for (i = 0; i < pairs; i++)
    {
@@ -622,12 +653,15 @@ static struct map *parse_cgi_parameters(char *argstring)
          *p = '\0';
          if (map(cgi_params, url_decode(vector[i]), 0, url_decode(++p), 0))
          {
+            freez(vector);
             free_map(cgi_params);
             return NULL;
          }
       }
    }
 
+   freez(vector);
+
    return cgi_params;
 
 }
@@ -1808,7 +1842,7 @@ jb_err template_load(const struct client_state *csp, char **template_ptr,
    /* Validate template name.  Paranoia. */
    for (p = templatename; *p != 0; p++)
    {
-      if (((*p < 'a') || (*p > 'z'))
+      if ( ((*p < 'a') || (*p > 'z'))
         && ((*p < 'A') || (*p > 'Z'))
         && ((*p < '0') || (*p > '9'))
         && (*p != '-')
@@ -2168,7 +2202,7 @@ struct map *default_exports(const struct client_state *csp, const char *caller)
    if (!err) err = map_block_killer(exports, "can-toggle");
 #endif
 
-   if(!strcmp(CODE_STATUS, "stable"))
+   if (!strcmp(CODE_STATUS, "stable"))
    {
       if (!err) err = map_block_killer(exports, "unstable");
    }