Commit 32b3351d155de1c938acdc104e4dffa67fc155b5

Steffen Jaeckel 2019-04-05T10:54:06

Merge pull request #200 from libtom/cast-cleanup remove unnecessary size_t casts, fix Wconversion/Wsign-conversion issues

diff --git a/bn_mp_get_long.c b/bn_mp_get_long.c
index b90cb3d..b95bb8a 100644
--- a/bn_mp_get_long.c
+++ b/bn_mp_get_long.c
@@ -23,7 +23,7 @@ unsigned long mp_get_long(const mp_int *a)
    }
 
    /* 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 */
    res = (unsigned long)a->dp[i];
diff --git a/bn_mp_get_long_long.c b/bn_mp_get_long_long.c
index 50c34aa..cafd9a4 100644
--- a/bn_mp_get_long_long.c
+++ b/bn_mp_get_long_long.c
@@ -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 */
-   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 */
    res = (unsigned long long)a->dp[i];
diff --git a/bn_mp_grow.c b/bn_mp_grow.c
index 255335d..1394451 100644
--- a/bn_mp_grow.c
+++ b/bn_mp_grow.c
@@ -29,7 +29,7 @@ int mp_grow(mp_int *a, int size)
        * in case the operation failed we don't want
        * 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) {
          /* reallocation failed but "a" is still valid [can be freed] */
          return MP_MEM;
diff --git a/bn_mp_init.c b/bn_mp_init.c
index 0be909c..3c0c489 100644
--- a/bn_mp_init.c
+++ b/bn_mp_init.c
@@ -18,7 +18,7 @@ int mp_init(mp_int *a)
    int i;
 
    /* 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) {
       return MP_MEM;
    }
diff --git a/bn_mp_init_size.c b/bn_mp_init_size.c
index b692f5b..1becb23 100644
--- a/bn_mp_init_size.c
+++ b/bn_mp_init_size.c
@@ -21,7 +21,7 @@ int mp_init_size(mp_int *a, int size)
    size += (MP_PREC * 2) - (size % MP_PREC);
 
    /* 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) {
       return MP_MEM;
    }
diff --git a/bn_mp_montgomery_reduce.c b/bn_mp_montgomery_reduce.c
index 382c7cc..4331a8a 100644
--- a/bn_mp_montgomery_reduce.c
+++ b/bn_mp_montgomery_reduce.c
@@ -28,7 +28,7 @@ int mp_montgomery_reduce(mp_int *x, const mp_int *n, mp_digit rho)
    if ((digs < (int)MP_WARRAY) &&
        (x->used <= (int)MP_WARRAY) &&
        (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);
    }
 
diff --git a/bn_mp_mul.c b/bn_mp_mul.c
index f83b1b7..9867f3c 100644
--- a/bn_mp_mul.c
+++ b/bn_mp_mul.c
@@ -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
          if ((digs < (int)MP_WARRAY) &&
              (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);
          } else
 #endif
diff --git a/bn_mp_prime_is_prime.c b/bn_mp_prime_is_prime.c
index b9cb396..b9e9497 100644
--- a/bn_mp_prime_is_prime.c
+++ b/bn_mp_prime_is_prime.c
@@ -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) {
                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 &= mask;
          }
diff --git a/bn_mp_prime_random_ex.c b/bn_mp_prime_random_ex.c
index 929c72e..fdd5140 100644
--- a/bn_mp_prime_random_ex.c
+++ b/bn_mp_prime_random_ex.c
@@ -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*/
-   maskAND = ((size&7) == 0) ? 0xFF : (0xFF >> (8 - (size & 7)));
+   maskAND = ((size&7) == 0) ? 0xFF : (unsigned char)(0xFF >> (8 - (size & 7)));
 
    /* calc the maskOR_msb */
    maskOR_msb        = 0;
    maskOR_msb_offset = ((size & 7) == 1) ? 1 : 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 */
@@ -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 */
       tmp[0]    &= maskAND;
-      tmp[0]    |= 1 << ((size - 1) & 7);
+      tmp[0]    |= (unsigned char)(1 << ((size - 1) & 7));
 
       /* mix in the maskORs */
       tmp[maskOR_msb_offset]   |= maskOR_msb;
diff --git a/bn_mp_rand.c b/bn_mp_rand.c
index 17aa5a2..423e488 100644
--- a/bn_mp_rand.c
+++ b/bn_mp_rand.c
@@ -86,7 +86,7 @@ static int s_read_win_csp(mp_digit *p)
 
 static int s_read_getrandom(mp_digit *p)
 {
-   int ret;
+   ssize_t ret;
    do {
       ret = getrandom(p, sizeof(*p), 0);
    } while ((ret == -1) && (errno == EINTR));
diff --git a/bn_mp_shrink.c b/bn_mp_shrink.c
index de15e12..b714fdd 100644
--- a/bn_mp_shrink.c
+++ b/bn_mp_shrink.c
@@ -23,7 +23,7 @@ int mp_shrink(mp_int *a)
    }
 
    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;
       }
       a->dp    = tmp;
diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c
index 63bb2e2..5b04f4f 100644
--- a/bn_mp_sqr.c
+++ b/bn_mp_sqr.c
@@ -34,7 +34,7 @@ int mp_sqr(const mp_int *a, mp_int *b)
          /* can we use the fast comba multiplier? */
          if ((((a->used * 2) + 1) < (int)MP_WARRAY) &&
              (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);
          } else
 #endif
diff --git a/bn_mp_sub_d.c b/bn_mp_sub_d.c
index d8ac250..eb481ae 100644
--- a/bn_mp_sub_d.c
+++ b/bn_mp_sub_d.c
@@ -64,13 +64,13 @@ int mp_sub_d(const mp_int *a, mp_digit b, mp_int *c)
 
       /* subtract first digit */
       *tmpc    = *tmpa++ - b;
-      mu       = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u);
+      mu       = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
       *tmpc++ &= MP_MASK;
 
       /* handle rest of the digits */
       for (ix = 1; ix < a->used; ix++) {
          *tmpc    = *tmpa++ - mu;
-         mu       = *tmpc >> ((sizeof(mp_digit) * (size_t)CHAR_BIT) - 1u);
+         mu       = *tmpc >> ((CHAR_BIT * sizeof(mp_digit)) - 1u);
          *tmpc++ &= MP_MASK;
       }
    }
diff --git a/bn_s_mp_mul_digs.c b/bn_s_mp_mul_digs.c
index 332e974..05f684f 100644
--- a/bn_s_mp_mul_digs.c
+++ b/bn_s_mp_mul_digs.c
@@ -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? */
    if ((digs < (int)MP_WARRAY) &&
        (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);
    }
 
diff --git a/bn_s_mp_mul_high_digs.c b/bn_s_mp_mul_high_digs.c
index 509682b..5e52d23 100644
--- a/bn_s_mp_mul_high_digs.c
+++ b/bn_s_mp_mul_high_digs.c
@@ -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? */
 #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
    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);
    }
 #endif
diff --git a/bn_s_mp_sub.c b/bn_s_mp_sub.c
index 88e44dc..6035e2f 100644
--- a/bn_s_mp_sub.c
+++ b/bn_s_mp_sub.c
@@ -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
           * 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] */
          *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;
 
          /* 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] */
          *tmpc++ &= MP_MASK;
diff --git a/makefile_include.mk b/makefile_include.mk
index 421c113..1b57833 100644
--- a/makefile_include.mk
+++ b/makefile_include.mk
@@ -51,8 +51,10 @@ CFLAGS += -I./ -Wall -Wsign-compare -Wextra -Wshadow
 
 ifndef NO_ADDTL_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 += -Wconversion -Wsign-conversion
 endif
 
 ifdef COMPILE_DEBUG
diff --git a/tommath.h b/tommath.h
index e225626..511666b 100644
--- a/tommath.h
+++ b/tommath.h
@@ -145,7 +145,7 @@ extern int KARATSUBA_MUL_CUTOFF,
 #endif
 
 /* 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 */
 typedef struct  {