Merge pull request #200 from libtom/cast-cleanup

remove unnecessary size_t casts, fix Wconversion/Wsign-conversion issues
This commit is contained in:
Steffen Jaeckel 2019-04-05 10:54:06 +02:00 committed by GitHub
commit 32b3351d15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
18 changed files with 24 additions and 22 deletions

View File

@ -23,7 +23,7 @@ unsigned long mp_get_long(const mp_int *a)
} }
/* get number of digits of the lsb we have to read */ /* get number of digits of the lsb we have to read */
i = MIN(a->used, ((((int)sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1; i = MIN(a->used, (((CHAR_BIT * (int)sizeof(unsigned long)) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
/* get most significant digit of result */ /* get most significant digit of result */
res = (unsigned long)a->dp[i]; res = (unsigned long)a->dp[i];

View File

@ -23,7 +23,7 @@ unsigned long long mp_get_long_long(const mp_int *a)
} }
/* get number of digits of the lsb we have to read */ /* get number of digits of the lsb we have to read */
i = MIN(a->used, ((((int)sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1; i = MIN(a->used, (((CHAR_BIT * (int)sizeof(unsigned long long)) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
/* get most significant digit of result */ /* get most significant digit of result */
res = (unsigned long long)a->dp[i]; res = (unsigned long long)a->dp[i];

View File

@ -29,7 +29,7 @@ int mp_grow(mp_int *a, int size)
* in case the operation failed we don't want * in case the operation failed we don't want
* to overwrite the dp member of a. * to overwrite the dp member of a.
*/ */
tmp = (mp_digit *) XREALLOC(a->dp, sizeof(mp_digit) * (size_t)size); tmp = (mp_digit *) XREALLOC(a->dp, (size_t)size * sizeof(mp_digit));
if (tmp == NULL) { if (tmp == NULL) {
/* reallocation failed but "a" is still valid [can be freed] */ /* reallocation failed but "a" is still valid [can be freed] */
return MP_MEM; return MP_MEM;

View File

@ -18,7 +18,7 @@ int mp_init(mp_int *a)
int i; int i;
/* allocate memory required and clear it */ /* allocate memory required and clear it */
a->dp = (mp_digit *) XMALLOC(sizeof(mp_digit) * (size_t)MP_PREC); a->dp = (mp_digit *) XMALLOC(MP_PREC * sizeof(mp_digit));
if (a->dp == NULL) { if (a->dp == NULL) {
return MP_MEM; return MP_MEM;
} }

View File

@ -21,7 +21,7 @@ int mp_init_size(mp_int *a, int size)
size += (MP_PREC * 2) - (size % MP_PREC); size += (MP_PREC * 2) - (size % MP_PREC);
/* alloc mem */ /* alloc mem */
a->dp = (mp_digit *) XMALLOC(sizeof(mp_digit) * (size_t)size); a->dp = (mp_digit *) XMALLOC((size_t)size * sizeof(mp_digit));
if (a->dp == NULL) { if (a->dp == NULL) {
return MP_MEM; return MP_MEM;
} }

View File

@ -28,7 +28,7 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
if ((digs < (int)MP_WARRAY) && if ((digs < (int)MP_WARRAY) &&
(x->used <= (int)MP_WARRAY) && (x->used <= (int)MP_WARRAY) &&
(n->used < (n->used <
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) { (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
return fast_mp_montgomery_reduce(x, n, rho); return fast_mp_montgomery_reduce(x, n, rho);
} }

View File

@ -42,7 +42,7 @@ int mp_mul(const mp_int *a, const mp_int *b, mp_int *c)
#ifdef BN_FAST_S_MP_MUL_DIGS_C #ifdef BN_FAST_S_MP_MUL_DIGS_C
if ((digs < (int)MP_WARRAY) && if ((digs < (int)MP_WARRAY) &&
(MIN(a->used, b->used) <= (MIN(a->used, b->used) <=
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) { (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
res = fast_s_mp_mul_digs(a, b, c, digs); res = fast_s_mp_mul_digs(a, b, c, digs);
} else } else
#endif #endif

View File

@ -300,7 +300,7 @@ int mp_prime_is_prime(const mp_int *a, int t, int *result)
if ((err = mp_rand(&b, 1)) != MP_OKAY) { if ((err = mp_rand(&b, 1)) != MP_OKAY) {
goto LBL_B; goto LBL_B;
} }
fips_rand <<= sizeof(mp_digit) * CHAR_BIT; fips_rand <<= CHAR_BIT * sizeof(mp_digit);
fips_rand |= (unsigned int) b.dp[0]; fips_rand |= (unsigned int) b.dp[0];
fips_rand &= mask; fips_rand &= mask;
} }

View File

@ -52,13 +52,13 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
} }
/* calc the maskAND value for the MSbyte*/ /* calc the maskAND value for the MSbyte*/
maskAND = ((size&7) == 0) ? 0xFF : (0xFF >> (8 - (size & 7))); maskAND = ((size&7) == 0) ? 0xFF : (unsigned char)(0xFF >> (8 - (size & 7)));
/* calc the maskOR_msb */ /* calc the maskOR_msb */
maskOR_msb = 0; maskOR_msb = 0;
maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0; maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
if ((flags & LTM_PRIME_2MSB_ON) != 0) { if ((flags & LTM_PRIME_2MSB_ON) != 0) {
maskOR_msb |= 0x80 >> ((9 - size) & 7); maskOR_msb |= (unsigned char)(0x80 >> ((9 - size) & 7));
} }
/* get the maskOR_lsb */ /* get the maskOR_lsb */
@ -76,7 +76,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
/* work over the MSbyte */ /* work over the MSbyte */
tmp[0] &= maskAND; tmp[0] &= maskAND;
tmp[0] |= 1 << ((size - 1) & 7); tmp[0] |= (unsigned char)(1 << ((size - 1) & 7));
/* mix in the maskORs */ /* mix in the maskORs */
tmp[maskOR_msb_offset] |= maskOR_msb; tmp[maskOR_msb_offset] |= maskOR_msb;

View File

@ -86,7 +86,7 @@ static int s_read_win_csp(mp_digit *p)
static int s_read_getrandom(mp_digit *p) static int s_read_getrandom(mp_digit *p)
{ {
int ret; ssize_t ret;
do { do {
ret = getrandom(p, sizeof(*p), 0); ret = getrandom(p, sizeof(*p), 0);
} while ((ret == -1) && (errno == EINTR)); } while ((ret == -1) && (errno == EINTR));

View File

@ -23,7 +23,7 @@ int mp_shrink(mp_int *a)
} }
if (a->alloc != used) { if (a->alloc != used) {
if ((tmp = (mp_digit *) XREALLOC(a->dp, sizeof(mp_digit) * (size_t)used)) == NULL) { if ((tmp = (mp_digit *) XREALLOC(a->dp, (size_t)used * sizeof(mp_digit))) == NULL) {
return MP_MEM; return MP_MEM;
} }
a->dp = tmp; a->dp = tmp;

View File

@ -34,7 +34,7 @@ int mp_sqr(const mp_int *a, mp_int *b)
/* can we use the fast comba multiplier? */ /* can we use the fast comba multiplier? */
if ((((a->used * 2) + 1) < (int)MP_WARRAY) && if ((((a->used * 2) + 1) < (int)MP_WARRAY) &&
(a->used < (a->used <
(int)(1u << (((sizeof(mp_word) * (size_t)CHAR_BIT) - (2u * (size_t)DIGIT_BIT)) - 1u)))) { (int)(1u << (((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT)) - 1u)))) {
res = fast_s_mp_sqr(a, b); res = fast_s_mp_sqr(a, b);
} else } else
#endif #endif

View File

@ -64,13 +64,13 @@ int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
/* subtract first digit */ /* subtract first digit */
*tmpc = *tmpa++ - b; *tmpc = *tmpa++ - b;
mu = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u); mu = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
*tmpc++ &= MP_MASK; *tmpc++ &= MP_MASK;
/* handle rest of the digits */ /* handle rest of the digits */
for (ix = 1; ix < a->used; ix++) { for (ix = 1; ix < a->used; ix++) {
*tmpc = *tmpa++ - mu; *tmpc = *tmpa++ - mu;
mu = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u); mu = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
*tmpc++ &= MP_MASK; *tmpc++ &= MP_MASK;
} }
} }

View File

@ -27,7 +27,7 @@ int s_mp_mul_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
/* can we use the fast multiplier? */ /* can we use the fast multiplier? */
if ((digs < (int)MP_WARRAY) && if ((digs < (int)MP_WARRAY) &&
(MIN(a->used, b->used) < (MIN(a->used, b->used) <
(int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) { (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
return fast_s_mp_mul_digs(a, b, c, digs); return fast_s_mp_mul_digs(a, b, c, digs);
} }

View File

@ -26,7 +26,7 @@ int s_mp_mul_high_digs(const mp_int *a, const mp_int *b, mp_int *c, int digs)
/* can we use the fast multiplier? */ /* can we use the fast multiplier? */
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
if (((a->used + b->used + 1) < (int)MP_WARRAY) if (((a->used + b->used + 1) < (int)MP_WARRAY)
&& (MIN(a->used, b->used) < (int)(1u << (((size_t)CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) { && (MIN(a->used, b->used) < (int)(1u << ((CHAR_BIT * sizeof(mp_word)) - (2u * (size_t)DIGIT_BIT))))) {
return fast_s_mp_mul_high_digs(a, b, c, digs); return fast_s_mp_mul_high_digs(a, b, c, digs);
} }
#endif #endif

View File

@ -50,7 +50,7 @@ int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
* if a carry does occur it will propagate all the way to the * if a carry does occur it will propagate all the way to the
* MSB. As a result a single shift is enough to get the carry * MSB. As a result a single shift is enough to get the carry
*/ */
u = *tmpc >> (((size_t)CHAR_BIT * sizeof(mp_digit)) - 1u); u = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
/* Clear carry from T[i] */ /* Clear carry from T[i] */
*tmpc++ &= MP_MASK; *tmpc++ &= MP_MASK;
@ -62,7 +62,7 @@ int s_mp_sub(const mp_int *a, const mp_int *b, mp_int *c)
*tmpc = *tmpa++ - u; *tmpc = *tmpa++ - u;
/* U = carry bit of T[i] */ /* U = carry bit of T[i] */
u = *tmpc >> (((size_t)CHAR_BIT * sizeof(mp_digit)) - 1u); u = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
/* Clear carry from T[i] */ /* Clear carry from T[i] */
*tmpc++ &= MP_MASK; *tmpc++ &= MP_MASK;

View File

@ -51,8 +51,10 @@ CFLAGS += -I./ -Wall -Wsign-compare -Wextra -Wshadow
ifndef NO_ADDTL_WARNINGS ifndef NO_ADDTL_WARNINGS
# additional warnings # additional warnings
CFLAGS += -Wsystem-headers -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align CFLAGS += -Wsystem-headers
CFLAGS += -Wdeclaration-after-statement -Wbad-function-cast -Wcast-align
CFLAGS += -Wstrict-prototypes -Wpointer-arith CFLAGS += -Wstrict-prototypes -Wpointer-arith
#CFLAGS += -Wconversion -Wsign-conversion
endif endif
ifdef COMPILE_DEBUG ifdef COMPILE_DEBUG

View File

@ -145,7 +145,7 @@ extern int KARATSUBA_MUL_CUTOFF,
#endif #endif
/* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */ /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */
#define MP_WARRAY (1u << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1)) #define MP_WARRAY (1u << (((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT)) + 1))
/* the infamous mp_int structure */ /* the infamous mp_int structure */
typedef struct { typedef struct {