lib: cgen: make _bf_round_next_power_of_2 correct on 64-bit systems#494
lib: cgen: make _bf_round_next_power_of_2 correct on 64-bit systems#494pzmarzly wants to merge 1 commit intofacebook:mainfrom
Conversation
Claude review of PR #494 (e205435)Clean, minimal fix. The Suggestions
|
| value |= value >> 4; | ||
| value |= value >> 8; | ||
| value |= value >> 16; | ||
| #if SIZE_MAX > 0xFFFFFFFFU |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
size_t can be 32-bit or 64-bit integer. And 2**0 should be 1.
d77ac8d to
e205435
Compare
size_t can be 32-bit or 64-bit integer. We can use a conditional macro to support both.
This was raised by Claude PR reviewer in unrelated PR.
This function is currently only used to calculate
_BF_LOG_MAP_SIZE(which is small static number), andimg_size(which is being removed by #487), so there isn't any danger at the moment. But let's avoid a future surprise.Also, on both 32-bit and 64-bit platforms, it gives incorrect result for
2**0. Should be 1, is 0. Fixing.