From c6046021245f84431361b171262afb59ab8637e8 Mon Sep 17 00:00:00 2001 From: Harsha Vardhan Date: Mon, 31 Aug 2026 14:49:16 +0530 Subject: [PATCH] fix: prevent infinite loop in cfIEEE1284NormalizeMakeModel on empty MFG/MDL (fixes #214) --- Makefile.am | 11 ++++ cupsfilters/ieee1284.c | 9 ++-- cupsfilters/test-ieee1284-normalize.c | 73 +++++++++++++++++++++++++++ 3 files changed, 90 insertions(+), 3 deletions(-) create mode 100644 cupsfilters/test-ieee1284-normalize.c diff --git a/Makefile.am b/Makefile.am index 2689a6047..6f8749a9b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -106,6 +106,7 @@ check_PROGRAMS = \ test-analyze \ test-pdf \ test-ps \ + test-ieee1284-normalize \ testfilters TESTS = \ @@ -115,6 +116,7 @@ TESTS = \ test-analyze \ test-pdf \ test-ps \ + test-ieee1284-normalize \ cupsfilters/testfilters.sh \ cupsfilters/test-pclm-overflow.sh \ cupsfilters/test-pdftoraster-copy-height.sh @@ -289,6 +291,15 @@ test1284_CFLAGS = \ -I$(srcdir)/cupsfilters/ \ $(CUPS_CFLAGS) +test_ieee1284_normalize_SOURCES = \ + cupsfilters/test-ieee1284-normalize.c +test_ieee1284_normalize_CFLAGS = \ + -I$(srcdir)/cupsfilters/ \ + $(CUPS_CFLAGS) +test_ieee1284_normalize_LDADD = \ + libcupsfilters.la \ + $(CUPS_LIBS) + testpdf1_SOURCES = \ cupsfilters/testpdf1.c \ cupsfilters/fontembed-private.h diff --git a/cupsfilters/ieee1284.c b/cupsfilters/ieee1284.c index 95d580d4b..559a73075 100644 --- a/cupsfilters/ieee1284.c +++ b/cupsfilters/ieee1284.c @@ -804,8 +804,8 @@ cfIEEE1284NormalizeMakeModel( makeptr ++; bufptr ++; } - while (isspace(*(bufptr - 1))) bufptr --; - if (bufptr < buffer + bufsize - 1) + while (bufptr > buffer && isspace(*(bufptr - 1))) bufptr --; + if (bufptr > buffer && bufptr < buffer + bufsize - 1) { *bufptr = ' '; makeptr ++; @@ -822,7 +822,7 @@ cfIEEE1284NormalizeMakeModel( modelptr ++; bufptr ++; } - while (isspace(*(bufptr - 1))) bufptr --; + while (bufptr > buffer && isspace(*(bufptr - 1))) bufptr --; *bufptr = '\0'; if (!nomakemodel && makeptr != bufptr) modelptr = makeptr; @@ -1161,6 +1161,9 @@ cfIEEE1284NormalizeMakeModel( // Remove repeated manufacturer names... // + if (!modelptr || modelptr > buffer + strlen(buffer)) + modelptr = buffer + strlen(buffer); + compare_len = modelptr - buffer; while (compare_len > 0 && strncasecmp(buffer, modelptr, compare_len) == 0) move_right_part(buffer, bufsize, modelptr, buffer - modelptr); diff --git a/cupsfilters/test-ieee1284-normalize.c b/cupsfilters/test-ieee1284-normalize.c new file mode 100644 index 000000000..bb55d5653 --- /dev/null +++ b/cupsfilters/test-ieee1284-normalize.c @@ -0,0 +1,73 @@ +// +// IEEE-1284 normalization regression test for libcupsfilters (Issue #214). +// +// Copyright © 2026 by OpenPrinting. +// +// Licensed under Apache License v2.0. See the file "LICENSE" for more +// information. +// + +#include +#include +#include +#include + +int +main(void) +{ + char buffer[1024]; // Make/model buffer + char *model = NULL; // Pointer to model name + char *res; // Result pointer + + // + // Issue #214 regression test: empty MFG and MDL fields in IEEE-1284 device ID + // + + res = cfIEEE1284NormalizeMakeModel("MFG:;MDL:;", NULL, + CF_IEEE1284_NORMALIZE_HUMAN, NULL, + buffer, sizeof(buffer), &model, NULL, + NULL); + if (res != NULL) + { + printf("FAIL: MFG:;MDL:; expected NULL, got \"%s\"\n", res); + return (1); + } + + res = cfIEEE1284NormalizeMakeModel("MFG:;MDL:;CMD:PostScript;", NULL, + CF_IEEE1284_NORMALIZE_HUMAN, NULL, + buffer, sizeof(buffer), &model, NULL, + NULL); + if (res != NULL) + { + printf("FAIL: MFG:;MDL:;CMD:PostScript; expected NULL, got \"%s\"\n", res); + return (1); + } + + // + // Verify valid inputs behavior preservation + // + + res = cfIEEE1284NormalizeMakeModel("MDL:;", NULL, + CF_IEEE1284_NORMALIZE_HUMAN, NULL, + buffer, sizeof(buffer), &model, NULL, + NULL); + if (!res || strcmp(res, "MDL:;")) + { + printf("FAIL: MDL:; expected \"MDL:;\", got \"%s\"\n", res ? res : "NULL"); + return (1); + } + + res = cfIEEE1284NormalizeMakeModel("MFG:HP;MDL:DeskJet;", NULL, + CF_IEEE1284_NORMALIZE_HUMAN, NULL, + buffer, sizeof(buffer), &model, NULL, + NULL); + if (!res || strcmp(res, "HP DeskJet")) + { + printf("FAIL: MFG:HP;MDL:DeskJet; expected \"HP DeskJet\", got \"%s\"\n", + res ? res : "NULL"); + return (1); + } + + puts("PASS: cfIEEE1284NormalizeMakeModel issue #214 tests passed."); + return (0); +}