fix: prevent infinite loop in cfIEEE1284NormalizeMakeModel on empty MFG/MDL (fixes #214) - #223
Conversation
zdohnal
left a comment
There was a problem hiding this comment.
I would prefer returning if there is no manufacturer or model values - while the current MR might be shorter solution at the moment, but IMO it is better to end early if we don't have proper required input rather than trying to recover.
I have my proposal at zdohnal@60d2b49 , but without test. We can combine them, if preferred.
|
For me this is OK, as the bug gets fixed. If there are few devices which have actually one of the manufacturer or model field empty or missing, they can still be used. |
|
Thanks @tillkamppeter and @zdohnal for reviewing this. I’ll leave the current implementation as-is based on the maintainer feedback, with the regression test covering the empty MFG/MDL cases and preserving the existing behavior for valid inputs. |
|
@tillkamppeter my fear with that is that allowing such empty values can spawn issues further down in the ecosystem. While I would guess empty manufacturer and model values are not valid, but I don't know any standard to support this guess. |
|
Thanks for the clarification. I’ve updated the implementation to return early when the required MFG or MDL value is empty, and added regression coverage for both empty and partial Device IDs while preserving the existing behavior for valid MFG + MDL inputs. |
Summary
Fixes an infinite loop in
cfIEEE1284NormalizeMakeModel()when given IEEE-1284 device IDs containing emptyMFGandMDLfields (such asMFG:;MDL:;andMFG:;MDL:;CMD:PostScript;).Root Cause
MFG:;was encountered,makeptrandbufptrremained atbuffer. The unpatched code checkedwhile (isspace(*(bufptr - 1))) bufptr--;, reading before thebufferarray start.if (bufptr < buffer + bufsize - 1)evaluated to true for emptyMFG, inserting a space' 'and advancingmakeptrtobuffer + 1.modelptr: WhenMDL:;was processed,bufptrwas decremented back tobufferand NUL-terminated (""). However,if (!nomakemodel && makeptr != bufptr)assignedmodelptr = makeptr(buffer + 1), causingmodelptrto point past the string terminator.compare_len = modelptr - buffer(1) and repeatedly matchedstrncasecmp("", garbage, 1) == 0, looping infinitely inmove_right_part().Fix Description
bufptr > bufferbounds checks before dereferencing*(bufptr - 1)in trailing whitespace removal loops (lines 807 & 825).bufptr > buffercondition at line 808 to prevent inserting a space separator when no manufacturer text was written.modelptrPointer Safety: Added a defensive bounds check at line 1164 (if (!modelptr || modelptr > buffer + strlen(buffer)) modelptr = buffer + strlen(buffer);) to guaranteemodelptrnever points past the string NUL-terminator.cupsfilters/test-ieee1284-normalize.cverifying that emptyMFGandMDLinputs return safely without hanging or crashing, while preserving validMDL:;andMFG:HP;MDL:DeskJet;inputs. Registered the test inMakefile.amundercheck_PROGRAMSandTESTS.Fixes #214.