From e78427443094163d1839387a2470fd22612ca8d1 Mon Sep 17 00:00:00 2001 From: Damien George Date: Thu, 28 Dec 2017 14:14:06 +1100 Subject: [PATCH] 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. --- py/mpz.c | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-) diff --git a/py/mpz.c b/py/mpz.c index 78dee3132..380e8968e 100644 --- a/py/mpz.c +++ b/py/mpz.c @@ -1647,16 +1647,12 @@ char *mpz_as_str(const mpz_t *i, unsigned int base) { } #endif -// assumes enough space as calculated by mp_int_format_size +// assumes enough space in str as calculated by mp_int_format_size +// base must be between 2 and 32 inclusive // returns length of string, not including null byte size_t mpz_as_str_inpl(const mpz_t *i, unsigned int base, const char *prefix, char base_char, char comma, char *str) { - if (str == NULL) { - return 0; - } - if (base < 2 || base > 32) { - str[0] = 0; - return 0; - } + assert(str != NULL); + assert(2 <= base && base <= 32); size_t ilen = i->len;