Commit ca6924b7e64f862ffe2140493cfa9bd736366bbb

Daniel Mendler 2019-12-23T11:12:42

add error code MP_OVF for integer overflow (too many digits)

diff --git a/mp_error_to_string.c b/mp_error_to_string.c
index 1b34d02..39adcd1 100644
--- a/mp_error_to_string.c
+++ b/mp_error_to_string.c
@@ -19,6 +19,8 @@ const char *mp_error_to_string(mp_err code)
       return "Max. iterations reached";
    case MP_BUF:
       return "Buffer overflow";
+   case MP_OVF:
+      return "Integer overflow";
    default:
       return "Invalid error code";
    }
diff --git a/mp_grow.c b/mp_grow.c
index ff3b96d..5bca1b5 100644
--- a/mp_grow.c
+++ b/mp_grow.c
@@ -11,7 +11,7 @@ mp_err mp_grow(mp_int *a, int size)
       mp_digit *dp;
 
       if (size > MP_MAX_DIGIT_COUNT) {
-         return MP_MEM;
+         return MP_OVF;
       }
 
       /* reallocate the array a->dp
diff --git a/mp_init_multi.c b/mp_init_multi.c
index 6567cf1..908b4df 100644
--- a/mp_init_multi.c
+++ b/mp_init_multi.c
@@ -7,14 +7,15 @@
 
 mp_err mp_init_multi(mp_int *mp, ...)
 {
-   mp_err err = MP_OKAY;      /* Assume ok until proven otherwise */
+   mp_err err = MP_OKAY;
    int n = 0;                 /* Number of ok inits */
    mp_int *cur_arg = mp;
    va_list args;
 
    va_start(args, mp);        /* init args to next argument from caller */
    while (cur_arg != NULL) {
-      if (mp_init(cur_arg) != MP_OKAY) {
+      err = mp_init(cur_arg);
+      if (err != MP_OKAY) {
          /* Oops - error! Back-track and mp_clear what we already
             succeeded in init-ing, then return error.
          */
@@ -28,14 +29,13 @@ mp_err mp_init_multi(mp_int *mp, ...)
             cur_arg = va_arg(clean_args, mp_int *);
          }
          va_end(clean_args);
-         err = MP_MEM;
          break;
       }
       n++;
       cur_arg = va_arg(args, mp_int *);
    }
    va_end(args);
-   return err;                /* Assumed ok, if error flagged above. */
+   return err;
 }
 
 #endif
diff --git a/mp_init_size.c b/mp_init_size.c
index 979a0b7..e28a3cd 100644
--- a/mp_init_size.c
+++ b/mp_init_size.c
@@ -9,7 +9,7 @@ mp_err mp_init_size(mp_int *a, int size)
    size = MP_MAX(MP_MIN_DIGIT_COUNT, size);
 
    if (size > MP_MAX_DIGIT_COUNT) {
-      return MP_MEM;
+      return MP_OVF;
    }
 
    /* alloc mem */
diff --git a/tommath.h b/tommath.h
index a2e7585..25a166e 100644
--- a/tommath.h
+++ b/tommath.h
@@ -100,7 +100,8 @@ typedef enum {
    MP_MEM   = -2,  /* out of mem */
    MP_VAL   = -3,  /* invalid input */
    MP_ITER  = -4,  /* maximum iterations reached */
-   MP_BUF   = -5   /* buffer overflow, supplied buffer too small */
+   MP_BUF   = -5,  /* buffer overflow, supplied buffer too small */
+   MP_OVF   = -6   /* mp_int overflow, too many digits */
 } mp_err;
 
 typedef enum {