Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/libbpfilter/cgen/program.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,12 +65,18 @@

static inline size_t _bf_round_next_power_of_2(size_t value)
{
if (value == 0)
return 1;

value--;
value |= value >> 1;
value |= value >> 2;
value |= value >> 4;
value |= value >> 8;
value |= value >> 16;
#if SIZE_MAX > 0xFFFFFFFFU
Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude: suggestion: Pre-existing: _bf_round_next_power_of_2(0) returns 0 due to unsigned wrap-around (0 - 1 wraps to SIZE_MAX, OR-shifts fill all bits, then ++value wraps back to 0). Since you're already touching this function, consider adding a guard:

static inline size_t _bf_round_next_power_of_2(size_t value)
{
    if (value == 0)
        return 1;

    value--;
    ...

Neither current call site can pass 0 (sizeof(struct bf_log) * _BF_LOG_MAP_N_ENTRIES is always positive), so this is not blocking — just a defensive improvement for future callers.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

That's true, and it is something I stumbled upon already when using this function for bf_hashset (where initial capacity was 0).

2**0 == 1, but here _bf_round_next_power_of_2(0) == 0.

I'll address this here in this PR as well, but I'll let reviewer decide if this should be merged together.

value |= value >> 32;
#endif

return ++value;
}
Expand Down
Loading