From dc775d2c2dc750d697a1f0009fc997416295dac5 Mon Sep 17 00:00:00 2001 From: ManasBagul23 Date: Sun, 22 Nov 2026 00:31:33 +0530 Subject: [PATCH] ppd-collection: check malloc before copying into it Two spots allocate a small record with malloc() and immediately memcpy() into the result without checking for NULL: ppdCollectionListPPDs() when copying a matched PPD record for the caller, and load_ppds()'s directory-loop-detection bookkeeping. Both are on the normal path of listing/scanning a PPD collection (e.g. "cups-driverd list" on a Printer Application's PPD database), so an allocation failure there is currently a NULL-pointer memcpy rather than a clean error. ppdCollectionListPPDs() logs and stops adding further PPDs to the result it already built; load_ppds() logs and returns failure, since it can no longer safely track this directory for loop detection. --- ppd/ppd-collection.cxx | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/ppd/ppd-collection.cxx b/ppd/ppd-collection.cxx index 74854111..3125fa27 100644 --- a/ppd/ppd-collection.cxx +++ b/ppd/ppd-collection.cxx @@ -586,7 +586,14 @@ ppdCollectionListPPDs( cupsArrayAdd(result, strdup(ppd->record.make)); else { - newppd = (ppd_info_t *)malloc(sizeof(ppd_info_t)); + if ((newppd = (ppd_info_t *)malloc(sizeof(ppd_info_t))) == NULL) + { + if (log) log(ld, CF_LOGLEVEL_ERROR, + "libppd: [PPD Collections] Ran out of memory copying " + "PPD %s!", + ppd->record.name); + break; + } memcpy(newppd, ppd, sizeof(ppd_info_t)); cupsArrayAdd(result, newppd); } @@ -2089,7 +2096,14 @@ load_ppds(const char *d, // I - Actual directory // Nope, add it to the Inodes array and continue... // - dinfoptr = (struct stat *)malloc(sizeof(struct stat)); + if ((dinfoptr = (struct stat *)malloc(sizeof(struct stat))) == NULL) + { + if (log) log(ld, CF_LOGLEVEL_ERROR, + "libppd: [PPD Collections] Ran out of memory recording " + "\"%s\" for loop detection!", + d); + return (0); + } memcpy(dinfoptr, &dinfo, sizeof(struct stat)); cupsArrayAdd(ppdlist->Inodes, dinfoptr);