Skip to content

fix double free in add_enum_attributes/add_margin_attributes - #91

Merged
tillkamppeter merged 1 commit into
OpenPrinting:masterfrom
ManasBagul23:fix-margin-enum-double-free
Nov 24, 2026
Merged

tillkamppeter merged 1 commit into
OpenPrinting:masterfrom
ManasBagul23:fix-margin-enum-double-free

Conversation

@ManasBagul23

Copy link
Copy Markdown
Contributor

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 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.

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_attributes and add_margin_attributes) share the identical pattern, so both are fixed together here.

Test plan

  • Code-traced against current 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.

…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.
Copilot AI balanced review requested due to automatic review settings November 24, 2026 19:56

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota. Try requesting a review at a lower effort level.

@tillkamppeter
tillkamppeter merged commit 7220d51 into OpenPrinting:master Nov 24, 2026
11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants