Skip to content

Commit

Permalink
Merge pull request #4 from near/fix-bf_set_ui
Browse files Browse the repository at this point in the history
fix undefined behavior: shift 32 bits for uint32_t in bf_set_ui
  • Loading branch information
ailisp authored Oct 10, 2022
2 parents 83c426d + a2c07b9 commit 572026e
Showing 1 changed file with 6 additions and 1 deletion.
7 changes: 6 additions & 1 deletion libbf.c
Original file line number Diff line number Diff line change
Expand Up @@ -235,8 +235,13 @@ int bf_set_ui(bf_t *r, uint64_t a)
a0 = a;
a1 = a >> 32;
shift = clz(a1);
// shift < 32 because a > 0xffffffff
r->tab[0] = a0 << shift;
r->tab[1] = (a1 << shift) | (a0 >> (LIMB_BITS - shift));
if (shift == 0) {
r->tab[1] = a1;
} else {
r->tab[1] = (a1 << shift) | (a0 >> (LIMB_BITS - shift));
}
r->expn = 2 * LIMB_BITS - shift;
}
#endif
Expand Down

0 comments on commit 572026e

Please sign in to comment.