Skip to content

Fix compiler warning about uninitialized rb_parent_color - #1484

Merged
flyingmutant merged 1 commit into
cmus:masterfrom
chggr:uninitialized-warning
Jan 10, 2026
Merged

flyingmutant merged 1 commit into
cmus:masterfrom
chggr:uninitialized-warning

Conversation

@chggr

@chggr chggr commented Jan 5, 2026

Copy link
Copy Markdown
Contributor

When compiling with GCC 15.2.1, the compiler issues a maybe-uninitialized warning with message: rb_parent_color may be used uninitialized. This is happening because the RB_CLEAR_NODE macro currently expands to rb_set_parent(node, node), which in turn attempts to read the node->rb_parent_color. In certain cases where the node has not yet been initialized, reading the rb_parent_color could return garbage.

This change fixes the issue and clears the warning by updating the RB_CLEAR_NODE macro to directly set the parent pointer without reading the potential garbage color value. The new RB_CLEAR_NODE macro implementation is similar to what is used in the Linux kernel.

When compiling with GCC 15.2.1, the compiler issues a maybe-uninitialized warning with
message: rb_parent_color may be used uninitialized. This is happening because the
RB_CLEAR_NODE macro currently expands to rb_set_parent(node, node), which in turn
attempts to read the node->rb_parent_color. In certain cases where the node has not
yet been initialized, reading the rb_parent_color could return garbage.

This change fixes the issue and clears the warning by updating the RB_CLEAR_NODE macro
to directly set the parent pointer without reading the potential garbage color value.
The new RB_CLEAR_NODE macro implementation is similar to what is used in the Linux kernel.
@chggr

chggr commented Jan 5, 2026 •

Copy link
Copy Markdown
Contributor Author

The full text of the compiler warning for reference:

In file included from track.h:23,
from track.c:19:
In function 'rb_set_parent',
inlined from 'simple_track_init' at track.c:36:2,
inlined from 'simple_track_new' at track.c:44:2:
rbtree.h:126:34: warning: '((struct rb_node *)((char *)ptr + 8))[2].rb_parent_color' may be used uninitialized [-Wmaybe-uninitialized]
126 | rb->rb_parent_color = (rb->rb_parent_color & 3) | (unsigned long)p;
| `

@pgaskin
pgaskin self-requested a review January 6, 2026 07:26

@pgaskin pgaskin left a comment •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

This change makes sense. It's basically the same as before, except the node pointer isn't also OR'd with the uninitialized rb_parent_color.

When it's used, e.g., in track.c by simple_track_new, it's done against memory allocated with xnew, which comes from malloc.

When modifying to the tree, the color is set explicitly.

@flyingmutant
flyingmutant merged commit d335e90 into cmus:master Jan 10, 2026
@BrewTestBot BrewTestBot mentioned this pull request Oct 28, 2026
1 task done
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