use calloc so we don't have to zero the digits ourself

this also has the nice side-effect that potential multiplication
overflows in `mp_init_size` are now eliminiated
This commit is contained in:
Steffen Jaeckel 2019-04-04 12:38:36 +02:00
parent d01b531897
commit d6c6f8c3cc
2 changed files with 2 additions and 16 deletions

View File

@ -15,19 +15,12 @@
/* init a new mp_int */
int mp_init(mp_int *a)
{
int i;
/* allocate memory required and clear it */
a->dp = (mp_digit *) XMALLOC(MP_PREC * sizeof(mp_digit));
a->dp = (mp_digit *) XCALLOC((size_t)MP_PREC, sizeof(mp_digit));
if (a->dp == NULL) {
return MP_MEM;
}
/* set the digits to zero */
for (i = 0; i < MP_PREC; i++) {
a->dp[i] = 0;
}
/* set the used to zero, allocated digits to the default precision
* and sign to positive */
a->used = 0;

View File

@ -15,13 +15,11 @@
/* init an mp_init for a given size */
int mp_init_size(mp_int *a, int size)
{
int x;
/* pad size so there are always extra digits */
size += (MP_PREC * 2) - (size % MP_PREC);
/* alloc mem */
a->dp = (mp_digit *) XMALLOC((size_t)size * sizeof(mp_digit));
a->dp = (mp_digit *) XCALLOC((size_t)size, sizeof(mp_digit));
if (a->dp == NULL) {
return MP_MEM;
}
@ -31,11 +29,6 @@ int mp_init_size(mp_int *a, int size)
a->alloc = size;
a->sign = MP_ZPOS;
/* zero the digits */
for (x = 0; x < size; x++) {
a->dp[x] = 0;
}
return MP_OKAY;
}
#endif