add error code MP_OVF for integer overflow (too many digits)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
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 {