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

This commit is contained in:
Daniel Mendler 2019-12-23 11:12:42 +01:00
parent 876c5fc3f6
commit ca6924b7e6
No known key found for this signature in database
GPG Key ID: D88ADB2A2693CA43
5 changed files with 10 additions and 7 deletions

View File

@ -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";
}

View File

@ -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

View File

@ -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

View File

@ -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 */

View File

@ -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 {