]> git.ipfire.org Git - thirdparty/cups.git/commitdiff
_ppdCacheAssignPresets(): Added NULL checks for option choice properties record
authorTill Kamppeter <till.kamppeter@gmail.com>
Sun, 24 Sep 2023 22:17:17 +0000 (00:17 +0200)
committerTill Kamppeter <till.kamppeter@gmail.com>
Sun, 24 Sep 2023 22:17:17 +0000 (00:17 +0200)
Static analysis of code, GitHub code scanning, found missing NULL
checks for allocating memory for option choice properties records
(gives NULL when out of memory) and for grabbing the records from a
CUPS array (should now never happen as we now skip the option on the
first memory allocation failure).

Added appropriate NULL checks. Now in case of unsufficient memory the
current option gets skipped on the first failed calloc() call.

cups/ppd-cache.c

index 8966dc207680518e2444e1d689a4c698ef6cc297..72a414ba22537609b81b97f62aab90868f125251 100644 (file)
@@ -2252,6 +2252,9 @@ _ppdCacheAssignPresets(ppd_file_t *ppd,
       {
        properties =
          (choice_properties_t *)calloc(1, sizeof(choice_properties_t));
+       /* No memory for choice properties, skip option */
+       if (properties == NULL)
+         break;
 
        c = option->choices[k].choice;
 
@@ -2645,6 +2648,9 @@ _ppdCacheAssignPresets(ppd_file_t *ppd,
        /* Add the properties of this choice */
        cupsArrayAdd(choice_properties, properties);
       }
+      /* Memory allocation failure, skip option */
+      if (k < option->num_choices)
+       goto skip;
 
      /*
       * Find the best choice for each field of the color/quality preset
@@ -2656,6 +2662,9 @@ _ppdCacheAssignPresets(ppd_file_t *ppd,
        for (k = 0; k < option->num_choices; k ++)
         {
          properties = cupsArrayIndex(choice_properties, k);
+         /* Should never happen, to satisfy GitHub code scanning */
+         if (properties == NULL)
+           continue;
 
          /* presets[0][0]: Mono/Draft */
          if (best_mono_draft >= 0 &&
@@ -2765,6 +2774,10 @@ _ppdCacheAssignPresets(ppd_file_t *ppd,
       for (k = 0; k < option->num_choices; k ++)
       {
        properties = cupsArrayIndex(choice_properties, k);
+       /* Should never happen, to satisfy GitHub code scanning */
+       if (properties == NULL)
+         continue;
+
        c = option->choices[k].choice;
 
        /* Vendor-specific options */
@@ -3046,7 +3059,8 @@ _ppdCacheAssignPresets(ppd_file_t *ppd,
 
       }
 
-      for (k = 0; k < option->num_choices; k ++)
+    skip:
+      for (k = 0; k < cupsArrayGetCount(choice_properties); k ++)
        free(cupsArrayIndex(choice_properties, k));
       cupsArrayDelete(choice_properties);
     }