Skip to content

Commit

Permalink
add error code MP_OVF for integer overflow (too many digits)
Browse files Browse the repository at this point in the history
  • Loading branch information
minad committed Dec 23, 2019
1 parent 876c5fc commit ca6924b
Show file tree
Hide file tree
Showing 5 changed files with 10 additions and 7 deletions.
2 changes: 2 additions & 0 deletions mp_error_to_string.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
}
Expand Down
2 changes: 1 addition & 1 deletion mp_grow.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
8 changes: 4 additions & 4 deletions mp_init_multi.c
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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
2 changes: 1 addition & 1 deletion mp_init_size.c
Original file line number Diff line number Diff line change
Expand Up @@ -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 */
Expand Down
3 changes: 2 additions & 1 deletion tommath.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit ca6924b

Please sign in to comment.