]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/var.c
Merge changes from CUPS 1.4svn-r8606.
[thirdparty/cups.git] / cgi-bin / var.c
index 113b8d026ed33de582c3f4015d55ee18744613fc..628ce8add577b80f624384b14ebfe4e74ad366df 100644 (file)
@@ -1,25 +1,16 @@
 /*
- * "$Id: var.c 5548 2006-05-19 19:38:31Z mike $"
+ * "$Id: var.c 7460 2008-04-16 02:19:54Z mike $"
  *
  *   CGI form variable and array functions.
  *
+ *   Copyright 2007-2009 by Apple Inc.
  *   Copyright 1997-2005 by Easy Software Products.
  *
  *   These coded instructions, statements, and computer programs are the
- *   property of Easy Software Products and are protected by Federal
- *   copyright law.  Distribution and use rights are outlined in the file
- *   "LICENSE.txt" which should have been included with this file.  If this
- *   file is missing or damaged please contact Easy Software Products
- *   at:
- *
- *       Attn: CUPS Licensing Information
- *       Easy Software Products
- *       44141 Airport View Drive, Suite 204
- *       Hollywood, Maryland 20636 USA
- *
- *       Voice: (301) 373-9600
- *       EMail: cups-info@cups.org
- *         WWW: http://www.cups.org
+ *   property of Apple Inc. and are protected by Federal copyright
+ *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
+ *   which should have been included with this file.  If this file is
+ *   file is missing or damaged, see the license at "http://www.cups.org/".
  *
  * Contents:
  *
@@ -157,9 +148,6 @@ cgiGetArray(const char *name,               /* I - Name of array variable */
   if ((var = cgi_find_variable(name)) == NULL)
     return (NULL);
 
-  if (var->nvalues == 1)
-    return (var->values[0]);
-
   if (element < 0 || element >= var->nvalues)
     return (NULL);
 
@@ -212,10 +200,10 @@ cgiGetVariable(const char *name)  /* I - Name of variable */
 
 #ifdef DEBUG
   if (var == NULL)
-    printf("cgiGetVariable(\"%s\") is returning NULL...\n", name);
+    DEBUG_printf(("cgiGetVariable(\"%s\") is returning NULL...\n", name));
   else
-    printf("cgiGetVariable(\"%s\") is returning \"%s\"...\n", name,
-           var->values[var->nvalues - 1]);
+    DEBUG_printf(("cgiGetVariable(\"%s\") is returning \"%s\"...\n", name,
+                 var->values[var->nvalues - 1]));
 #endif /* DEBUG */
 
   return ((var == NULL) ? NULL : var->values[var->nvalues - 1]);
@@ -251,7 +239,6 @@ cgiInitialize(void)
   */
 
   setbuf(stdout, NULL);
-  puts("Content-type: text/plain\n");
 #endif /* DEBUG */
 
  /*
@@ -331,9 +318,15 @@ cgiSetArray(const char *name,              /* I - Name of variable */
   {
     if (element >= var->avalues)
     {
+      const char **temp;               /* Temporary pointer */
+
+      temp = (const char **)realloc((void *)(var->values),
+                                    sizeof(char *) * (element + 16));
+      if (!temp)
+        return;
+
       var->avalues = element + 16;
-      var->values  = (const char **)realloc((void *)(var->values),
-                                            sizeof(char *) * var->avalues);
+      var->values  = temp;
     }
 
     if (element >= var->nvalues)
@@ -371,9 +364,15 @@ cgiSetSize(const char *name,               /* I - Name of variable */
 
   if (size >= var->avalues)
   {
+    const char **temp;                 /* Temporary pointer */
+
+    temp = (const char **)realloc((void *)(var->values),
+                                 sizeof(char *) * (size + 16));
+    if (!temp)
+      return;
+
     var->avalues = size + 16;
-    var->values  = (const char **)realloc((void *)(var->values),
-                                          sizeof(char *) * var->avalues);
+    var->values  = temp;
   }
 
   if (size > var->nvalues)
@@ -435,31 +434,40 @@ cgi_add_variable(const char *name,        /* I - Variable name */
                 int        element,    /* I - Array element number */
                  const char *value)    /* I - Variable value */
 {
-  _cgi_var_t   *var;                           /* New variable */
+  _cgi_var_t   *var;                   /* New variable */
 
 
   if (name == NULL || value == NULL || element < 0 || element > 100000)
     return;
 
-#ifdef DEBUG
-  printf("Adding variable \'%s\' with value \'%s\'...\n", name, value);
-#endif /* DEBUG */
+  DEBUG_printf(("cgi_add_variable: Adding variable \'%s\' with value "
+                "\'%s\'...\n", name, value));
 
   if (form_count >= form_alloc)
   {
+    _cgi_var_t *temp_vars;             /* Temporary form pointer */
+
+
     if (form_alloc == 0)
-      form_vars = malloc(sizeof(_cgi_var_t) * 16);
+      temp_vars = malloc(sizeof(_cgi_var_t) * 16);
     else
-      form_vars = realloc(form_vars, (form_alloc + 16) * sizeof(_cgi_var_t));
+      temp_vars = realloc(form_vars, (form_alloc + 16) * sizeof(_cgi_var_t));
 
+    if (!temp_vars)
+      return;
+
+    form_vars  = temp_vars;
     form_alloc += 16;
   }
 
-  var                  = form_vars + form_count;
+  var = form_vars + form_count;
+
+  if ((var->values = calloc(element + 1, sizeof(char *))) == NULL)
+    return;
+
   var->name            = strdup(name);
   var->nvalues         = element + 1;
   var->avalues         = element + 1;
-  var->values          = calloc(element + 1, sizeof(char *));
   var->values[element] = strdup(value);
 
   form_count ++;
@@ -509,9 +517,7 @@ cgi_initialize_get(void)
   char *data;                          /* Pointer to form data string */
 
 
-#ifdef DEBUG
-  puts("Initializing variables using GET method...");
-#endif /* DEBUG */
+  DEBUG_puts("cgi_initialize_get: Initializing variables using GET method...");
 
  /*
   * Check to see if there is anything for us to read...
@@ -765,9 +771,7 @@ cgi_initialize_post(void)
        status;                         /* Return status */
 
 
-#ifdef DEBUG
-  puts("Initializing variables using POST method...");
-#endif /* DEBUG */
+  DEBUG_puts("cgi_initialize_post: Initializing variables using POST method...");
 
  /*
   * Check to see if there is anything for us to read...
@@ -793,11 +797,27 @@ cgi_initialize_post(void)
 
   for (tbytes = 0; tbytes < length; tbytes += nbytes)
     if ((nbytes = read(0, data + tbytes, length - tbytes)) < 0)
+    {
       if (errno != EAGAIN)
       {
         free(data);
         return (0);
       }
+      else
+        nbytes = 0;
+    }
+    else if (nbytes == 0)
+    {
+     /*
+      * CUPS STR #3176: OpenBSD: Early end-of-file on POST data causes 100% CPU
+      *
+      * This should never happen, but does on OpenBSD.  If we see early end-of-
+      * file, treat this as an error and process no data.
+      */
+
+      free(data);
+      return (0);
+    }
 
   data[length] = '\0';
 
@@ -914,7 +934,7 @@ cgi_initialize_string(const char *data)     /* I - Form data string */
     if (s > value)
       s --;
 
-    while (s >= value && *s == ' ')
+    while (s >= value && isspace(*s & 255))
       *s-- = '\0';
 
    /*
@@ -979,7 +999,7 @@ cgi_sort_variables(void)
   int  i;
 
 
-  puts("Sorting variables...");
+  DEBUG_puts("cgi_sort_variables: Sorting variables...");
 #endif /* DEBUG */
 
   if (form_count < 2)
@@ -989,10 +1009,11 @@ cgi_sort_variables(void)
         (int (*)(const void *, const void *))cgi_compare_variables);
 
 #ifdef DEBUG
-  puts("Sorted variable list is:");
+  DEBUG_puts("cgi_sort_variables: Sorted variable list is:");
   for (i = 0; i < form_count; i ++)
-    printf("%d: %s (%d) = \"%s\" ...\n", i, form_vars[i].name,
-           form_vars[i].nvalues, form_vars[i].values[0]);
+    DEBUG_printf(("cgi_sort_variables: %d: %s (%d) = \"%s\" ...\n", i,
+                  form_vars[i].name, form_vars[i].nvalues,
+                 form_vars[i].values[0]));
 #endif /* DEBUG */
 }
 
@@ -1027,5 +1048,5 @@ cgi_unlink_file(void)
 
 
 /*
- * End of "$Id: var.c 5548 2006-05-19 19:38:31Z mike $".
+ * End of "$Id: var.c 7460 2008-04-16 02:19:54Z mike $".
  */