mirror of
https://github.com/Z3Prover/z3
synced 2026-06-04 08:07:56 +00:00
Fix undefined behavior when negating SMALL_INT_MIN in power_of_two_multiple
Co-authored-by: nunoplopes <2998477+nunoplopes@users.noreply.github.com>
This commit is contained in:
parent
f9ec57947c
commit
23fe87612a
1 changed files with 10 additions and 1 deletions
|
|
@ -2273,7 +2273,16 @@ unsigned mpz_manager<SYNCH>::power_of_two_multiple(mpz const & a) {
|
||||||
if (val == 0) return 0;
|
if (val == 0) return 0;
|
||||||
|
|
||||||
// Work with absolute value for counting trailing zeros
|
// Work with absolute value for counting trailing zeros
|
||||||
uint64_t v = (val < 0) ? static_cast<uint64_t>(-val) : static_cast<uint64_t>(val);
|
// Handle SMALL_INT_MIN specially to avoid overflow
|
||||||
|
uint64_t v;
|
||||||
|
if (val == mpz::SMALL_INT_MIN) {
|
||||||
|
// SMALL_INT_MIN = -2^(SMALL_BITS-1), so it has (SMALL_BITS-1) trailing zeros
|
||||||
|
return mpz::SMALL_BITS - 1;
|
||||||
|
} else if (val < 0) {
|
||||||
|
v = static_cast<uint64_t>(-val);
|
||||||
|
} else {
|
||||||
|
v = static_cast<uint64_t>(val);
|
||||||
|
}
|
||||||
|
|
||||||
if ((v & 0xFFFFFFFF) == 0) {
|
if ((v & 0xFFFFFFFF) == 0) {
|
||||||
r += 32;
|
r += 32;
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue