Repository navigation
fix double free in add_enum_attributes/add_margin_attributes - #91
Merged
tillkamppeter merged 1 commit intoNov 24, 2026
Merged
Conversation
…butes Both functions build a de-duplicated list of values seen across the printers in a cluster using a single reusable "str" scratch buffer: each candidate value is snprintf'd into it, looked up in the array, and (if not already present) added to the array by that same pointer. Since the array only ever contains that one scratch address, every later cupsArrayFind() call compares the buffer against itself and always reports "found", so exactly one value ever gets added no matter how many distinct values are actually seen -- the merged cluster attribute silently loses all but the first value. Worse, after the loop the code does "free(str)" and then cupsArrayDelete(list), which frees every element still in the array -- that same str pointer -- a second time. Any cluster where the attribute has at least one value (print-quality-supported, finishings-supported, the media margins, etc. are all common) hits this double free. Add a strdup() of the scratch buffer's contents to the array instead of the scratch pointer itself, so each entry is independently owned, compared by content as intended, and freed exactly once.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Both functions build a de-duplicated list of values seen across the printers in a cluster using a single reusable "str" scratch buffer: each candidate value is
snprintf'd into it, looked up in the array, and (if not already present) added to the array by that same pointer.The bug
Since the array only ever contains that one scratch address, every later
cupsArrayFind()call compares the buffer against itself and always reports "found", so exactly one value ever gets added no matter how many distinct values are actually seen - the merged cluster attribute silently loses all but the first value.Worse, after the loop the code does
free(str)and thencupsArrayDelete(list), which frees every element still in the array - that samestrpointer - a second time. Any cluster where the attribute has at least one value (print-quality-supported,finishings-supported, the media margins, etc. are all common) hits this double free.Fix
Add a
strdup()of the scratch buffer's contents to the array instead of the scratch pointer itself, so each entry is independently owned, compared by content as intended, and freed exactly once. Both affected call sites (add_enum_attributesandadd_margin_attributes) share the identical pattern, so both are fixed together here.Test plan
upstream/master: both buggy lines (cupsArrayAdd(list, (void *)str)) are unchanged, so the bug is still present and reachable exactly as described at both call sites.