]> git.ipfire.org Git - thirdparty/cups.git/blobdiff - cgi-bin/search.c
Full sweep of all Clang warnings, plus some bug fixes for incorrect memcpy usage.
[thirdparty/cups.git] / cgi-bin / search.c
index 205a40cff9b9e2cea83de9959d900011f4ae77b6..b475d9600f60b11ae02bdbde3066c82051777e21 100644 (file)
@@ -1,31 +1,16 @@
 /*
- * "$Id: search.c 5963 2006-09-17 19:01:47Z mike $"
+ * "$Id$"
  *
- *   Search routines for the Common UNIX Printing System (CUPS).
+ * Search routines for CUPS.
  *
- *   Copyright 1997-2006 by Easy Software Products.
+ * Copyright 2007-2014 by Apple Inc.
+ * Copyright 1997-2006 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
- *
- * Contents:
- *
- *   cgiCompileSearch() - Compile a search string.
- *   cgiDoSearch()      - Do a search of some text.
- *   cgiFreeSearch()    - Free a compiled search context.
+ * These coded instructions, statements, and computer programs are the
+ * 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/".
  */
 
 /*
@@ -47,22 +32,30 @@ cgiCompileSearch(const char *query) /* I - Query string */
   char         *s,                     /* Regular expression string */
                *sptr,                  /* Pointer into RE string */
                *sword;                 /* Pointer to start of word */
-  int          slen;                   /* Allocated size of RE string */
+  size_t       slen;                   /* Allocated size of RE string */
   const char   *qptr,                  /* Pointer into query string */
                *qend;                  /* End of current word */
   const char   *prefix;                /* Prefix to add to next word */
   int          quoted;                 /* Word is quoted */
-  int          wlen;                   /* Word length */
+  size_t       wlen;                   /* Word length */
   char         *lword;                 /* Last word in query */
 
 
-  DEBUG_printf(("help_compile_search(query=\"%s\")\n", query ? query : "(nil)"));
+  DEBUG_printf(("cgiCompileSearch(query=\"%s\")\n", query));
+
+ /*
+  * Range check input...
+  */
+
+  if (!query)
+    return (NULL);
 
  /*
   * Allocate a regular expression storage structure...
   */
 
-  re = (regex_t *)calloc(1, sizeof(regex_t));
+  if ((re = (regex_t *)calloc(1, sizeof(regex_t))) == NULL)
+    return (NULL);
 
  /*
   * Allocate a buffer to hold the regular expression string, starting
@@ -74,7 +67,11 @@ cgiCompileSearch(const char *query)  /* I - Query string */
   if (slen < 1024)
     slen = 1024;
 
-  s = (char *)malloc(slen);
+  if ((s = (char *)malloc(slen)) == NULL)
+  {
+    free(re);
+    return (NULL);
+  }
 
  /*
   * Copy the query string to the regular expression, handling basic
@@ -136,13 +133,13 @@ cgiCompileSearch(const char *query)       /* I - Query string */
       for (qend = qptr + 1; *qend && !isspace(*qend); qend ++);
     }
 
-    wlen = qend - qptr;
+    wlen = (size_t)(qend - qptr);
 
    /*
     * Look for logic words: AND, OR
     */
 
-    if (wlen == 3 && !strncasecmp(qptr, "AND", 3))
+    if (wlen == 3 && !_cups_strncasecmp(qptr, "AND", 3))
     {
      /*
       * Logical AND with the following text...
@@ -153,7 +150,7 @@ cgiCompileSearch(const char *query) /* I - Query string */
 
       qptr = qend;
     }
-    else if (wlen == 2 && !strncasecmp(qptr, "OR", 2))
+    else if (wlen == 2 && !_cups_strncasecmp(qptr, "OR", 2))
     {
      /*
       * Logical OR with the following text...
@@ -171,7 +168,9 @@ cgiCompileSearch(const char *query) /* I - Query string */
       * string + RE overhead...
       */
 
-      wlen = (sptr - s) + 4 * wlen + 2 * strlen(prefix) + 4;
+      wlen = (size_t)(sptr - s) + 2 * 4 * wlen + 2 * strlen(prefix) + 11;
+      if (lword)
+        wlen += strlen(lword);
 
       if (wlen > slen)
       {
@@ -203,7 +202,7 @@ cgiCompileSearch(const char *query) /* I - Query string */
       * Add the prefix string...
       */
 
-      strcpy(sptr, prefix);
+      memcpy(sptr, prefix, strlen(prefix) + 1);
       sptr += strlen(sptr);
 
      /*
@@ -236,18 +235,24 @@ cgiCompileSearch(const char *query)       /* I - Query string */
         char *lword2;                  /* New "last word" */
 
 
-        lword2 = strdup(sword);
+        if ((lword2 = strdup(sword)) == NULL)
+       {
+         free(lword);
+         free(s);
+         free(re);
+         return (NULL);
+       }
 
-        strcpy(sptr, ".*|.*");
+        memcpy(sptr, ".*|.*", 6);
        sptr += 5;
 
-       strcpy(sptr, lword2);
+       memcpy(sptr, lword2, strlen(lword2) + 1);
        sptr += strlen(sptr);
 
-        strcpy(sptr, ".*");
+        memcpy(sptr, ".*", 3);
        sptr += 2;
 
-       strcpy(sptr, lword);
+       memcpy(sptr, lword, strlen(lword) + 1);
        sptr += strlen(sptr);
 
         free(lword);
@@ -276,7 +281,7 @@ cgiCompileSearch(const char *query) /* I - Query string */
     free(lword);
 
   if (sptr > s)
-    strcpy(sptr, ".*");
+    memcpy(sptr, ".*", 3);
   else
   {
    /*
@@ -366,5 +371,5 @@ cgiFreeSearch(void *search)         /* I - Search context */
 
 
 /*
- * End of "$Id: search.c 5963 2006-09-17 19:01:47Z mike $".
+ * End of "$Id$".
  */