Commit fd0da0be05de1da52f02e6b47ac1269f6b26e4e8

Steffen Jaeckel 2019-10-21T10:12:53

Merge pull request #393 from fperrad/20191020_lint some linting

diff --git a/demo/mtest_opponent.c b/demo/mtest_opponent.c
index 7fbd35e..e77fec9 100644
--- a/demo/mtest_opponent.c
+++ b/demo/mtest_opponent.c
@@ -6,7 +6,7 @@
 #define LTM_MTEST_RAND_SEED  23
 #endif
 
-static void draw(mp_int *a)
+static void draw(const mp_int *a)
 {
    ndraw(a, "");
 }
diff --git a/demo/shared.c b/demo/shared.c
index e47e481..a4ac833 100644
--- a/demo/shared.c
+++ b/demo/shared.c
@@ -1,8 +1,8 @@
 #include "shared.h"
 
-void ndraw(mp_int *a, const char *name)
+void ndraw(const mp_int *a, const char *name)
 {
-   char *buf = NULL;
+   char *buf;
    int size;
 
    mp_radix_size(a, 10, &size);
diff --git a/demo/shared.h b/demo/shared.h
index 4d5eb53..14818ac 100644
--- a/demo/shared.h
+++ b/demo/shared.h
@@ -17,5 +17,5 @@
 #define MP_WUR /* TODO: result checks disabled for now */
 #include "tommath_private.h"
 
-extern void ndraw(mp_int* a, const char* name);
+extern void ndraw(const mp_int* a, const char* name);
 extern void print_header(void);
diff --git a/demo/timing.c b/demo/timing.c
index f620b8c..47775da 100644
--- a/demo/timing.c
+++ b/demo/timing.c
@@ -21,7 +21,7 @@
 #endif
 
 
-static void ndraw(mp_int *a, const char *name)
+static void ndraw(const mp_int *a, const char *name)
 {
    char buf[4096];
 
@@ -30,7 +30,7 @@ static void ndraw(mp_int *a, const char *name)
    printf("%s\n", buf);
 }
 
-static void draw(mp_int *a)
+static void draw(const mp_int *a)
 {
    ndraw(a, "");
 }
@@ -149,7 +149,7 @@ int main(int argc, char **argv)
    printf("CLK_PER_SEC == %" PRIu64 "\n", CLK_PER_SEC);
 
 #ifdef LTM_TIMING_PRIME_IS_PRIME
-   if (should_test("prime", argc, argv)) {
+   if (should_test("prime", argc, argv) != 0) {
       for (m = 0; m < 2; ++m) {
          if (m == 0) {
             name = "    Arnault";
@@ -185,7 +185,7 @@ int main(int argc, char **argv)
    }
 #endif
 
-   if (should_test("add", argc, argv)) {
+   if (should_test("add", argc, argv) != 0) {
       log = FOPEN("logs/add.log", "w");
       for (cnt = 8; cnt <= 128; cnt += 8) {
          SLEEP;
@@ -208,7 +208,7 @@ int main(int argc, char **argv)
       FCLOSE(log);
    }
 
-   if (should_test("sub", argc, argv)) {
+   if (should_test("sub", argc, argv) != 0) {
       log = FOPEN("logs/sub.log", "w");
       for (cnt = 8; cnt <= 128; cnt += 8) {
          SLEEP;
@@ -232,7 +232,7 @@ int main(int argc, char **argv)
       FCLOSE(log);
    }
 
-   if (should_test("mulsqr", argc, argv)) {
+   if (should_test("mulsqr", argc, argv) != 0) {
       /* do mult/square twice, first without karatsuba and second with */
       old_kara_m = KARATSUBA_MUL_CUTOFF;
       old_kara_s = KARATSUBA_SQR_CUTOFF;
@@ -291,7 +291,7 @@ int main(int argc, char **argv)
       }
    }
 
-   if (should_test("expt", argc, argv)) {
+   if (should_test("expt", argc, argv) != 0) {
       const char *primes[] = {
          /* 2K large moduli */
          "179769313486231590772930519078902473361797697894230657273430081157732675805500963132708477322407536021120113879871393357658789768814416622492847430639474124377767893424865485276302219601246094119453082952085005768838150682342462881473913110540827237163350510684586239334100047359817950870678242457666208137217",
@@ -369,7 +369,7 @@ int main(int argc, char **argv)
       FCLOSE(logd);
    }
 
-   if (should_test("invmod", argc, argv)) {
+   if (should_test("invmod", argc, argv) != 0) {
       log = FOPEN("logs/invmod.log", "w");
       for (cnt = 4; cnt <= 32; cnt += 4) {
          SLEEP;
diff --git a/etc/pprime.c b/etc/pprime.c
index 009a18c..50be809 100644
--- a/etc/pprime.c
+++ b/etc/pprime.c
@@ -8,6 +8,9 @@
 #include <time.h>
 #include "tommath.h"
 
+/* TODO: Remove private_mp_word as soon as deprecated mp_word is removed from tommath. */
+typedef private_mp_word mp_word;
+
 static int   n_prime;
 static FILE *primes;
 
@@ -400,7 +403,7 @@ int main(void)
    pprime(k, li, &p, &q);
    t1 = clock() - t1;
 
-   printf("\n\nTook %d ticks, %d bits\n", t1, mp_count_bits(&p));
+   printf("\n\nTook %lu ticks, %d bits\n", t1, mp_count_bits(&p));
 
    mp_to_decimal(&p, buf, sizeof(buf));
    printf("P == %s\n", buf);
diff --git a/etc/tune.c b/etc/tune.c
index 2260a70..e6850c6 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -156,7 +156,7 @@ struct tune_args {
    int increment_print;
 } args;
 
-static void s_run(const char *name, uint64_t (*op)(int), int *cutoff)
+static void s_run(const char *name, uint64_t (*op)(int size), int *cutoff)
 {
    int x, count = 0;
    uint64_t t1, t2;
@@ -210,7 +210,7 @@ static long s_strtol(const char *str, char **endptr, const char *err)
       fprintf(stderr, "%s\n", err);
       exit(EXIT_FAILURE);
    }
-   if (endptr) *endptr = _endptr;
+   if (endptr != NULL) *endptr = _endptr;
    return val;
 }
 
@@ -446,7 +446,7 @@ int main(int argc, char **argv)
       struct {
          const char *name;
          int *cutoff, *update;
-         uint64_t (*fn)(int);
+         uint64_t (*fn)(int size);
       } test[] = {
 #define T_MUL_SQR(n, o, f)  { #n, &o##_CUTOFF, &(updated.o), MP_HAS(S_MP_##o) ? f : NULL }
          /*
@@ -464,7 +464,7 @@ int main(int argc, char **argv)
       /* Turn all limits from bncore.c to the max */
       set_cutoffs(&max_cutoffs);
       for (n = 0; n < sizeof(test)/sizeof(test[0]); ++n) {
-         if (test[n].fn) {
+         if (test[n].fn != NULL) {
             s_run(test[n].name, test[n].fn, test[n].cutoff);
             *test[n].update = *test[n].cutoff;
             *test[n].cutoff = INT_MAX;
diff --git a/mp_error_to_string.c b/mp_error_to_string.c
index a6f6fc7..1b34d02 100644
--- a/mp_error_to_string.c
+++ b/mp_error_to_string.c
@@ -17,6 +17,8 @@ const char *mp_error_to_string(mp_err code)
       return "Value out of range";
    case MP_ITER:
       return "Max. iterations reached";
+   case MP_BUF:
+      return "Buffer overflow";
    default:
       return "Invalid error code";
    }
diff --git a/mp_log_u32.c b/mp_log_u32.c
index 43748e5..2b49fe1 100644
--- a/mp_log_u32.c
+++ b/mp_log_u32.c
@@ -28,7 +28,7 @@ mp_err mp_log_u32(const mp_int *a, uint32_t base, uint32_t *c)
       return MP_OKAY;
    }
 
-   if (MP_HAS(S_MP_LOG_D) && a->used == 1) {
+   if (MP_HAS(S_MP_LOG_D) && (a->used == 1)) {
       *c = (uint32_t)s_mp_log_d(base, a->dp[0]);
       return MP_OKAY;
    }
diff --git a/mp_radix_size.c b/mp_radix_size.c
index 387e0ec..e9cfb9b 100644
--- a/mp_radix_size.c
+++ b/mp_radix_size.c
@@ -29,7 +29,7 @@ mp_err mp_radix_size(const mp_int *a, int radix, int *size)
    *size = (int)b;
 
    /* mp_ilogb truncates to zero, hence we need one extra put on top and one for `\0`. */
-   *size += 2 + (a->sign == MP_NEG);
+   *size += 2 + ((a->sign == MP_NEG) ? 1 : 0);
 
 LBL_ERR:
    return err;
diff --git a/mp_sqrtmod_prime.c b/mp_sqrtmod_prime.c
index 03ebf8a..723332f 100644
--- a/mp_sqrtmod_prime.c
+++ b/mp_sqrtmod_prime.c
@@ -59,7 +59,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
    }
 
    /* find a Z such that the Legendre symbol (Z|prime) == -1 */
-   mp_set(&Z, 2u);
+   mp_set(&Z, 2uL);
    /* Z = 2 */
    for (;;) {
       if ((err = mp_kronecker(&Z, prime, &legendre)) != MP_OKAY)     goto cleanup;
@@ -79,7 +79,7 @@ mp_err mp_sqrtmod_prime(const mp_int *n, const mp_int *prime, mp_int *ret)
    /* T = n ^ Q mod prime */
    if ((err = mp_copy(&S, &M)) != MP_OKAY)                       goto cleanup;
    /* M = S */
-   mp_set(&two, 2u);
+   mp_set(&two, 2uL);
 
    for (;;) {
       if ((err = mp_copy(&T, &t1)) != MP_OKAY)                    goto cleanup;