Skip to content

Commit a25872d

Browse files
authored
Merge pull request #4845 from t-ikegami/mpz_crash_fix
Fix bit_length() method to work with zero-valued mpz integer.
2 parents 6bc4f80 + 9490010 commit a25872d

File tree

1 file changed

+3
-0
lines changed

1 file changed

+3
-0
lines changed

py/mpz.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,9 @@ static inline size_t mpz_max_num_bits(const mpz_t *z) {
142142
return z->len * MPZ_DIG_SIZE;
143143
}
144144
static inline size_t mpz_num_bits(const mpz_t *z) {
145+
if (mpz_is_zero(z)) {
146+
return 0;
147+
}
145148
size_t last_bits = (8 * (sizeof(long) - sizeof(mpz_dig_t))) - __builtin_clzl(z->dig[z->len - 1]);
146149
return z->len * MPZ_DIG_SIZE + last_bits;
147150
}

0 commit comments

Comments
 (0)