From ee9c6513344bf4723e04342f207135b0353ed583 Mon Sep 17 00:00:00 2001 From: rue19 Date: Tue, 24 Nov 2026 08:47:58 +0530 Subject: [PATCH] Fix cppcheck 2.19 findings The cppcheck job has been failing since ubuntu-latest moved to Ubuntu 26.04, which ships cppcheck 2.19 instead of 2.13. It reports 112 findings on master: - syntaxError in ppd-generator.c: the workflow passed -DVERSION=2.x.y without quotes, so "..." VERSION "..." string concatenation did not parse. Quote it, as cups-browsed already does. - dangerousTypeCast (58), a check new in 2.19, for the C-style casts in the C++ code that comes from CUPS (the PPD compiler, genstrings, ppdhtml, ppdpo and ppd-collection.cxx). Suppress it in the workflow, in line with how findings in the CUPS-derived code are already handled there, instead of rewriting the casts. - nullPointerOutOfResources (33) for unchecked tmpfile() results in the unit tests, and nullPointerOutOfMemory in testppd.c. Check them and fail the test. - intToPointerCast (10) for the (ppd_file_t *)1 placeholder that the argument-check tests pass as a non-NULL PPD. Pass the address of a static dummy ppd_file_t instead. - nullPointerOutOfMemory in ppdFilterLoadPPDFile(): the calloc() of the "libppd" filter data extension was not checked. Log an error, close the PPD and return -1, like the PPD loading failure above it. - uninitvar (3), false positives for "if ((s.field = f()) == NULL)" on a partly initialized struct in ppdFilterCUPSWrapper(), ppdFindMarkedChoice() and ppdIsMarked(). Moving the assignment out of the condition avoids them without a suppression. With this, cppcheck 2.19 and 2.13 run with the CI job's options report nothing. The build and "make check" still pass. --- .github/workflows/cppcheck.yml | 12 ++++++++---- ppd/ppd-filter.c | 10 +++++++++- ppd/ppd-mark.c | 6 ++++-- ppd/test_ppd_attr.c | 5 +++++ ppd/test_ppd_cache.c | 10 ++++++++++ ppd/test_ppd_conflicts.c | 23 +++++++++++++++++++---- ppd/test_ppd_custom.c | 10 ++++++++++ ppd/test_ppd_emit.c | 12 ++++++++++-- ppd/test_ppd_ipp.c | 10 ++++++++++ ppd/test_ppd_load_profile.c | 18 ++++++++++++++---- ppd/test_ppd_localize.c | 5 +++++ ppd/test_ppd_mark.c | 5 +++++ ppd/test_ppd_page.c | 10 ++++++++++ ppd/testppd.c | 15 +++++++++++++++ 14 files changed, 134 insertions(+), 17 deletions(-) diff --git a/.github/workflows/cppcheck.yml b/.github/workflows/cppcheck.yml index a9bb224b..d09f20b7 100644 --- a/.github/workflows/cppcheck.yml +++ b/.github/workflows/cppcheck.yml @@ -27,9 +27,12 @@ jobs: # The PPD-compiler sources (ppdc*.{h,cxx}) plus encode.c, ppd-cache.c, # raster-interpret.c and ppdc-import.cxx are inherited verbatim from CUPS # (github.com/OpenPrinting/cups); their findings are suppressed here to - # keep that code identical to upstream instead of diverging it. The rest - # are cppcheck false positives (short-circuited NULL checks and a deflate - # output buffer) in libppd's own code. + # keep that code identical to upstream instead of diverging it. This + # includes dangerousTypeCast (new in cppcheck 2.19), which flags the + # C-style casts that all of the C++ code from CUPS (the PPD compiler, + # its tools and ppd-collection.cxx) uses. The rest are cppcheck false + # positives (short-circuited NULL checks and a deflate output buffer) + # in libppd's own code. cppcheck \ --enable=warning,performance,portability \ --error-exitcode=1 \ @@ -38,6 +41,7 @@ jobs: --suppress=unknownMacro \ --suppress=noCopyConstructor \ --suppress=noOperatorEq \ + --suppress=dangerousTypeCast \ --suppress=uninitvar:ppd/encode.c \ --suppress=invalidScanfArgType_int:ppd/ppd-cache.c \ --suppress=shiftNegativeLHS:ppd/raster-interpret.c \ @@ -48,5 +52,5 @@ jobs: --suppress=uninitvar:ppd/rastertops.c \ --suppress=nullPointerRedundantCheck:ppd/test_ppd_attr.c \ --suppress=nullPointerRedundantCheck:ppd/test_ppd_custom.c \ - -DVERSION="$(grep -oP 'AC_INIT\(\[[^\]]*\],\s*\[\K[^\]]*' configure.ac)" \ + -DVERSION="\"$(grep -oP 'AC_INIT\(\[[^\]]*\],\s*\[\K[^\]]*' configure.ac)\"" \ . diff --git a/ppd/ppd-filter.c b/ppd/ppd-filter.c index 9c342752..34de329b 100644 --- a/ppd/ppd-filter.c +++ b/ppd/ppd-filter.c @@ -114,7 +114,8 @@ ppdFilterCUPSWrapper( if (argc > 5) num_options = cupsParseOptions(argv[5], NULL, 0, &options); - if ((filter_data.printer = getenv("PRINTER")) == NULL) + filter_data.printer = getenv("PRINTER"); + if (filter_data.printer == NULL) filter_data.printer = argv[0]; filter_data.job_id = argc > 1 ? atoi(argv[1]) : 0; filter_data.job_user = argc > 2 ? argv[2] : NULL; @@ -228,6 +229,13 @@ ppdFilterLoadPPDFile(cf_filter_data_t *data, // I/O - Job and printer data filter_data_ext = (ppd_filter_data_ext_t *)calloc(1, sizeof(ppd_filter_data_ext_t)); + if (!filter_data_ext) + { + if (log) log(ld, CF_LOGLEVEL_ERROR, + "ppdFilterLoadPPDFile: Out of memory"); + ppdClose(ppd); + return (-1); + } filter_data_ext->ppdfile = strdup(ppdfile); // PPD file name filter_data_ext->ppd = ppd; // PPD data diff --git a/ppd/ppd-mark.c b/ppd/ppd-mark.c index b8099915..95e4e305 100644 --- a/ppd/ppd-mark.c +++ b/ppd/ppd-mark.c @@ -402,7 +402,8 @@ ppdFindMarkedChoice(ppd_file_t *ppd, // I - PPD file DEBUG_printf(("2ppdFindMarkedChoice(ppd=%p, option=\"%s\")", ppd, option)); - if ((key.option = ppdFindOption(ppd, option)) == NULL) + key.option = ppdFindOption(ppd, option); + if (key.option == NULL) { DEBUG_puts("3ppdFindMarkedChoice: Option not found, returning NULL"); return (NULL); @@ -484,7 +485,8 @@ ppdIsMarked(ppd_file_t *ppd, // I - PPD file data if (!ppd) return (0); - if ((key.option = ppdFindOption(ppd, option)) == NULL) + key.option = ppdFindOption(ppd, option); + if (key.option == NULL) return (0); if ((c = (ppd_choice_t *)cupsArrayFind(ppd->marked, &key)) == NULL) diff --git a/ppd/test_ppd_attr.c b/ppd/test_ppd_attr.c index 52b1a935..79e48d00 100644 --- a/ppd/test_ppd_attr.c +++ b/ppd/test_ppd_attr.c @@ -224,6 +224,11 @@ main(void) testBegin("ppdOpen(embedded attribute test PPD)"); f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(test_ppd_text, f); rewind(f); ppd = ppdOpen(f); diff --git a/ppd/test_ppd_cache.c b/ppd/test_ppd_cache.c index 3daae07d..22a62cf5 100644 --- a/ppd/test_ppd_cache.c +++ b/ppd/test_ppd_cache.c @@ -343,6 +343,11 @@ main(void) testBegin("ppdOpen(embedded cache test PPD)"); f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(test_ppd_text, f); rewind(f); ppd = ppdOpen(f); @@ -657,6 +662,11 @@ main(void) "*DefaultAuditEmpty: None\n" "*CloseUI: *AuditEmpty\n"; FILE *fp_foo = tmpfile(); + if (!fp_foo) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(foomatic_empty_opt_ppd, fp_foo); rewind(fp_foo); ppd_file_t *ppd_foo = ppdOpen(fp_foo); diff --git a/ppd/test_ppd_conflicts.c b/ppd/test_ppd_conflicts.c index 4e3a69a7..a7cdb022 100644 --- a/ppd/test_ppd_conflicts.c +++ b/ppd/test_ppd_conflicts.c @@ -265,6 +265,11 @@ static const char test_ppd_installable_text[] = "*UIConstraints: \"*Duplex DuplexNoTumble *OptionDuplexer False\"\n"; +// Non-NULL PPD for the argument-check tests: the functions must return +// before they look at it. +static ppd_file_t dummy_ppd; + + // // 'main()' - Run all ppd-conflicts.c unit tests. // @@ -310,12 +315,12 @@ main(void) // T03 — ppdGetConflicts(ppd, NULL option, ...) — `!option` clause. testBegin("ppdGetConflicts(ppd, NULL option, ...) returns 0"); options = NULL; - testEnd(ppdGetConflicts((ppd_file_t *)1, NULL, "x", &options) == 0); + testEnd(ppdGetConflicts(&dummy_ppd, NULL, "x", &options) == 0); // T04 — ppdGetConflicts(ppd, opt, NULL choice, ...) — `!choice` clause. testBegin("ppdGetConflicts(ppd, opt, NULL choice, ...) returns 0"); options = NULL; - testEnd(ppdGetConflicts((ppd_file_t *)1, "Duplex", NULL, &options) == 0); + testEnd(ppdGetConflicts(&dummy_ppd, "Duplex", NULL, &options) == 0); // T05 — ppdGetConflicts(ppd, opt, choice, NULL **options) — `!options` // clause. Note: because *options would be dereferenced earlier @@ -323,7 +328,7 @@ main(void) // null-check (line 84) — so passing NULL for the slot is safe and // still returns 0. testBegin("ppdGetConflicts(ppd, opt, choice, NULL slot) returns 0"); - testEnd(ppdGetConflicts((ppd_file_t *)1, "Duplex", "DuplexNoTumble", NULL) + testEnd(ppdGetConflicts(&dummy_ppd, "Duplex", "DuplexNoTumble", NULL) == 0); // T06 — ppdInstallableConflict(NULL, ...) — `!ppd` (line 656-657). @@ -332,7 +337,7 @@ main(void) // T07 — ppdInstallableConflict(ppd, NULL option, ...) — `!option`. testBegin("ppdInstallableConflict(ppd, NULL, ...) returns 0"); - testEnd(ppdInstallableConflict((ppd_file_t *)1, NULL, "x") == 0); + testEnd(ppdInstallableConflict(&dummy_ppd, NULL, "x") == 0); // T08 — ppdResolveConflicts(NULL, ...) — `!ppd` (line 202-203). The // same line also enforces the symmetry rule `(option == NULL) != @@ -360,6 +365,11 @@ main(void) testBegin("ppdOpen(embedded conflicts test PPD #1)"); f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(test_ppd_text, f); rewind(f); ppd = ppdOpen(f); @@ -587,6 +597,11 @@ main(void) testBegin("ppdOpen(embedded conflicts test PPD #2 — installable group)"); f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(test_ppd_installable_text, f); rewind(f); ppd2 = ppdOpen(f); diff --git a/ppd/test_ppd_custom.c b/ppd/test_ppd_custom.c index 04f0be82..9241e580 100644 --- a/ppd/test_ppd_custom.c +++ b/ppd/test_ppd_custom.c @@ -312,6 +312,11 @@ main(void) testBegin("ppdOpen(embedded custom test PPD)"); f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(test_ppd_text, f); rewind(f); ppd = ppdOpen(f); @@ -531,6 +536,11 @@ main(void) testBegin("ppdOpen(%s) %s", param_cases[i].title, param_cases[i].opens ? "succeeds" : "fails"); f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(custom_ppd_head, f); fputs(param_cases[i].params, f); fputs(custom_page_size, f); diff --git a/ppd/test_ppd_emit.c b/ppd/test_ppd_emit.c index 87e2dc3a..f544de99 100644 --- a/ppd/test_ppd_emit.c +++ b/ppd/test_ppd_emit.c @@ -379,6 +379,9 @@ open_embedded_ppd(const char *text) FILE *f = tmpfile(); ppd_file_t *ppd; + if (!f) + return (NULL); + fputs(text, f); rewind(f); ppd = ppdOpen(f); @@ -387,6 +390,11 @@ open_embedded_ppd(const char *text) } +// Non-NULL PPD for the argument-check tests: the functions must return +// before they look at it. +static ppd_file_t dummy_ppd; + + int // O - Exit status (0 = all pass) main(void) { @@ -418,7 +426,7 @@ main(void) // T02 — !fp half of the same guard. testBegin("ppdEmit(ppd, NULL fp, ...) returns -1"); - testEnd(ppdEmit((ppd_file_t *)1, NULL, PPD_ORDER_JCL) == -1); + testEnd(ppdEmit(&dummy_ppd, NULL, PPD_ORDER_JCL) == -1); // T03 — ppdEmitString: `if (!ppd) return (NULL);` (line 692). testBegin("ppdEmitString(NULL, ...) returns NULL"); @@ -430,7 +438,7 @@ main(void) // T05 — fd < 0 half of the same guard. testBegin("ppdEmitFd(ppd, fd=-1, ...) returns -1"); - testEnd(ppdEmitFd((ppd_file_t *)1, -1, PPD_ORDER_JCL) == -1); + testEnd(ppdEmitFd(&dummy_ppd, -1, PPD_ORDER_JCL) == -1); // T06 — ppdCollect(NULL, …, &choices) returns 0 AND nulls the slot. testBegin("ppdCollect(NULL ppd, …, &choices) returns 0, nulls slot"); diff --git a/ppd/test_ppd_ipp.c b/ppd/test_ppd_ipp.c index 8814390c..236003c8 100644 --- a/ppd/test_ppd_ipp.c +++ b/ppd/test_ppd_ipp.c @@ -194,6 +194,11 @@ load_test_ppd(void) int line; // Error line number f = tmpfile(); + if (!f) + { + testError("tmpfile() failed"); + return (NULL); + } fputs(test_ppd_text, f); rewind(f); ppd = ppdOpen(f); @@ -1007,6 +1012,11 @@ main(void) testBegin("ppdLoadAttributes(21-item cupsPwgRaster attribute) caps at 20 items"); { FILE *f21 = tmpfile(); + if (!f21) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(test_ppd_text, f21); fputs("*cupsFilter: \"image/pwg-raster 0 rastertopwg\"\n" "*cupsPwgRasterAuditSupported: \"v1,v2,v3,v4,v5,v6,v7,v8,v9,v10," diff --git a/ppd/test_ppd_load_profile.c b/ppd/test_ppd_load_profile.c index 17a42c3e..9db8505c 100644 --- a/ppd/test_ppd_load_profile.c +++ b/ppd/test_ppd_load_profile.c @@ -217,6 +217,11 @@ static const char test_ppd_text[] = "*cupsInkChannels EightChan.Plain.600dpi: \"8\"\n"; +// Non-NULL PPD for the argument-check tests: the functions must return +// before they look at it. +static ppd_file_t dummy_ppd; + + // // 'main()' - Run all ppd-load-profile.c unit tests. // @@ -257,18 +262,18 @@ main(void) // pointer; the !name short-circuit fires before any dereference. // Cast a dummy pointer (never dereferenced) to satisfy the type. testBegin("ppdFindColorAttr(ppd, NULL name, ...) returns NULL"); - testEnd(ppdFindColorAttr((ppd_file_t *)1, NULL, "RGB", "Plain", "600dpi", + testEnd(ppdFindColorAttr(&dummy_ppd, NULL, "RGB", "Plain", "600dpi", spec, sizeof(spec), NULL, NULL) == NULL); // T03 — NULL colormodel trips the !colormodel guard. testBegin("ppdFindColorAttr(.., NULL colormodel, ..) returns NULL"); - testEnd(ppdFindColorAttr((ppd_file_t *)1, "cupsTestL1", NULL, "Plain", + testEnd(ppdFindColorAttr(&dummy_ppd, "cupsTestL1", NULL, "Plain", "600dpi", spec, sizeof(spec), NULL, NULL) == NULL); // T04 — NULL spec output buffer trips the !spec guard. testBegin("ppdFindColorAttr(.., NULL spec out-buffer, ..) returns NULL"); - testEnd(ppdFindColorAttr((ppd_file_t *)1, "cupsTestL1", "RGB", "Plain", + testEnd(ppdFindColorAttr(&dummy_ppd, "cupsTestL1", "RGB", "Plain", "600dpi", NULL, sizeof(spec), NULL, NULL) == NULL); @@ -276,7 +281,7 @@ main(void) // buffer is far smaller than IPP_MAX_NAME (256 on every supported // cups build), so the guard always fires. testBegin("ppdFindColorAttr(.., specsize < IPP_MAX_NAME) returns NULL"); - testEnd(ppdFindColorAttr((ppd_file_t *)1, "cupsTestL1", "RGB", "Plain", + testEnd(ppdFindColorAttr(&dummy_ppd, "cupsTestL1", "RGB", "Plain", "600dpi", smallbuf, (int)sizeof(smallbuf), NULL, NULL) == NULL); @@ -292,6 +297,11 @@ main(void) testBegin("ppdOpen(embedded profile test PPD)"); f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(test_ppd_text, f); rewind(f); ppd = ppdOpen(f); diff --git a/ppd/test_ppd_localize.c b/ppd/test_ppd_localize.c index d05fd6d3..20ada688 100644 --- a/ppd/test_ppd_localize.c +++ b/ppd/test_ppd_localize.c @@ -94,6 +94,11 @@ main(void) testBegin("ppdOpen(embedded test PPD)"); f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(test_ppd_text, f); rewind(f); ppd = ppdOpen(f); diff --git a/ppd/test_ppd_mark.c b/ppd/test_ppd_mark.c index 798665a3..6ffb079f 100644 --- a/ppd/test_ppd_mark.c +++ b/ppd/test_ppd_mark.c @@ -324,6 +324,11 @@ main(void) testBegin("ppdOpen(embedded mark test PPD)"); f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(test_ppd_text, f); rewind(f); ppd = ppdOpen(f); diff --git a/ppd/test_ppd_page.c b/ppd/test_ppd_page.c index 88199a4d..cba04a8c 100644 --- a/ppd/test_ppd_page.c +++ b/ppd/test_ppd_page.c @@ -280,6 +280,11 @@ main(void) testBegin("ppdOpen(embedded variable-size test PPD)"); f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(test_ppd_text, f); rewind(f); ppd = ppdOpen(f); @@ -509,6 +514,11 @@ main(void) testBegin("ppdOpen(embedded no-variable-size test PPD)"); f = tmpfile(); + if (!f) + { + testEndMessage(false, "tmpfile() failed"); + return (1); + } fputs(test_ppd_novar, f); rewind(f); ppd2 = ppdOpen(f); diff --git a/ppd/testppd.c b/ppd/testppd.c index ba96649f..cf25e174 100644 --- a/ppd/testppd.c +++ b/ppd/testppd.c @@ -1248,9 +1248,19 @@ main(int argc, // I - Number of command-line arguments s = (char *)cupsArrayGetNext(dirlist), i ++) { col = (ppd_collection_t *)calloc(1, sizeof(ppd_collection_t)); + if (!col) + { + puts("Out of memory"); + return (1); + } col->path = s; printf("Searching directory %d, %s ...\n", i, col->path); col->name = (char *)calloc(20, sizeof(char)); + if (!col->name) + { + puts("Out of memory"); + return (1); + } snprintf(col->name, 19, "%d", i); cupsArrayAdd(ppd_collections, col); } @@ -1359,6 +1369,11 @@ main(int argc, // I - Number of command-line arguments name = argv[1]; filename = strdup(name); + if (!filename) + { + puts("Out of memory"); + return (1); + } if ((s = strchr(filename, ':')) != NULL) *s = '\0';