From 2ebddaf6f7587175420ab4bb0e9fc5039c4bb96c Mon Sep 17 00:00:00 2001 From: ManasBagul23 Date: Sun, 22 Nov 2026 00:20:01 +0530 Subject: [PATCH] ppd-custom: guard ppdFindCustomOption() against a NULL keyword Every other ppdFind*() lookup function in this library (ppdFindOption, ppdFindAttr, and the other three functions in this very file) checks both of its pointer arguments before use. ppdFindCustomOption() only checked ppd, then called strlcpy(key.keyword, keyword, ...) unconditionally - strlcpy() calls strlen() on keyword first, so a NULL keyword crashes. Added the missing check and a regression test with a real, open PPD. --- ppd/ppd-custom.c | 2 +- ppd/test_ppd_custom.c | 17 ++++++++++++----- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/ppd/ppd-custom.c b/ppd/ppd-custom.c index 42b1f1a5..32a2c8fd 100644 --- a/ppd/ppd-custom.c +++ b/ppd/ppd-custom.c @@ -33,7 +33,7 @@ ppdFindCustomOption(ppd_file_t *ppd, // I - PPD file ppd_coption_t key; // Custom option search key - if (!ppd) + if (!ppd || !keyword) return (NULL); strlcpy(key.keyword, keyword, sizeof(key.keyword)); diff --git a/ppd/test_ppd_custom.c b/ppd/test_ppd_custom.c index 085b49d4..04f0be82 100644 --- a/ppd/test_ppd_custom.c +++ b/ppd/test_ppd_custom.c @@ -276,13 +276,14 @@ main(void) // Group 1: NULL/argument guards (T01–T04) // // Each public function in ppd-custom.c opens with a NULL guard - // (`if (!ppd)` for ppdFindCustomOption, `if (!opt)` for the other three) - // and must return NULL without dereferencing the pointer. These run - // before any PPD is opened so there is no dependency on PPD content. + // (`if (!ppd || !keyword)` for ppdFindCustomOption, `if (!opt)` for the + // other three) and must return NULL without dereferencing the pointer. + // These run before any PPD is opened so there is no dependency on PPD + // content. // ========================================================================= - // T01 — ppdFindCustomOption: `if (!ppd) return (NULL);` — NULL PPD must - // short-circuit before strlcpy()/cupsArrayFind(). + // T01 — ppdFindCustomOption: `if (!ppd || !keyword) return (NULL);` — NULL + // PPD must short-circuit before strlcpy()/cupsArrayFind(). testBegin("ppdFindCustomOption(NULL ppd, \"PageSize\") returns NULL"); testEnd(ppdFindCustomOption(NULL, "PageSize") == NULL); @@ -332,6 +333,12 @@ main(void) copt = ppdFindCustomOption(ppd, "PageSize"); testEnd(copt != NULL); + // T06b — Same guard as T01, but with a real, open PPD this time: a NULL + // keyword must still short-circuit before strlcpy() calls + // strlen() on it, rather than crashing. + testBegin("ppdFindCustomOption(ppd, NULL keyword) returns NULL"); + testEnd(ppdFindCustomOption(ppd, NULL) == NULL); + // T07 — The stored keyword preserves the PPD's casing (the comparator is // case-insensitive but strlcpy() copied the keyword verbatim). testBegin("ppdFindCustomOption(\"PageSize\")->keyword == \"PageSize\"");