Skip to content

Commit e784274

Browse files
committed
py/mpz: In mpz_as_str_inpl, convert always-false checks to assertions.
There are two checks that are always false so can be converted to (negated) assertions to save code space and execution time. They are: 1. The check of the str parameter, which is required to be non-NULL as per the original comment that it has enough space in it as calculated by mp_int_format_size. And for all uses of this function str is indeed non-NULL. 2. The check of the base parameter, which is already required to be between 2 and 16 (inclusive) via the assertion in mp_int_format_size.
1 parent 9766fdd commit e784274

1 file changed

Lines changed: 4 additions & 8 deletions

File tree

py/mpz.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1647,16 +1647,12 @@ char *mpz_as_str(const mpz_t *i, unsigned int base) {
16471647
}
16481648
#endif
16491649

1650-
// assumes enough space as calculated by mp_int_format_size
1650+
// assumes enough space in str as calculated by mp_int_format_size
1651+
// base must be between 2 and 32 inclusive
16511652
// returns length of string, not including null byte
16521653
size_t mpz_as_str_inpl(const mpz_t *i, unsigned int base, const char *prefix, char base_char, char comma, char *str) {
1653-
if (str == NULL) {
1654-
return 0;
1655-
}
1656-
if (base < 2 || base > 32) {
1657-
str[0] = 0;
1658-
return 0;
1659-
}
1654+
assert(str != NULL);
1655+
assert(2 <= base && base <= 32);
16601656

16611657
size_t ilen = i->len;
16621658

0 commit comments

Comments
 (0)