Skip to content

cups-browsed: fix memory leaks when building cluster attributes - #88

Merged
tillkamppeter merged 1 commit into
OpenPrinting:masterfrom
rue19:fix-cluster-conflicts-leak
Nov 23, 2026
Merged

tillkamppeter merged 1 commit into
OpenPrinting:masterfrom
rue19:fix-cluster-conflicts-leak

Conversation

@rue19

@rue19 rue19 commented Nov 23, 2026

Copy link
Copy Markdown
Contributor

Two leaks in the code that builds the merged attributes of a cluster queue. They run every time a cluster's attributes are (re)built, so a long-running cups-browsed with clustered printers keeps growing.

  • check_printer_with_options() calls get_supported_options() twice per cluster member. That returns a newly allocated array, and neither array was ever freed. This is most of the leak.
  • add_mediadatabase_attributes() allocates the media_type/media_source strings of its scratch entry for every media-col-database value and never frees them. The array keeps its own copies through copy_media(), but its free callback was plain free(), so the strings inside each copy leaked too. This PR adds a free_media() callback and frees the scratch strings after each value.

How I found it: with a libFuzzer + ASan harness for the cluster path (get_cluster_attributes() → generate_cluster_conflicts() → get_cluster_sizes() → get_cluster_default_attributes() → ppdCreatePPDFromIPP2()), which I'm preparing for OpenPrinting/fuzzing#4. On the same 512 inputs, LeakSanitizer reports:

Leaked
master (de6571e) 478,592 bytes in 16,311 allocations
this PR 0

Testing: builds without warnings against distro CUPS 2.4 (Ubuntu 24.04), and make check passes (run as non-root with D-Bus and Avahi up, so the queues are created via DNS-SD and the test job completes). make check doesn't create a cluster, so the leak numbers above are what cover the changed code.

The cppcheck job is red on master for unrelated reasons; #87 fixes that.

check_printer_with_options() gets the supported values of two options
for every cluster member via get_supported_options(), which returns a
newly allocated array, but never frees either array.

add_mediadatabase_attributes() allocates the media_type/media_source
strings of its scratch entry for every media-col-database value and
never frees them; the array keeps its own copies via copy_media(), but
frees its entries with plain free(), leaking the strings inside them.

Free both arrays in check_printer_with_options(), free the scratch
strings after each value, and give the media database a free_media()
callback that also frees the strings.

These run whenever a cluster queue's attributes are (re)built, so a
long-running cups-browsed with clustered printers grew without bound.
Found with a libFuzzer/ASan harness for the cluster code path: on the
same 512 inputs, LeakSanitizer reports 478592 bytes in 16311
allocations before this change and none after.
@tillkamppeter
tillkamppeter merged commit 4956049 into OpenPrinting:master Nov 23, 2026
10 of 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.

2 participants