Skip to content

Commit d6c6f8c

Browse files
committed
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
1 parent d01b531 commit d6c6f8c

File tree

2 files changed

+2
-16
lines changed

2 files changed

+2
-16
lines changed

bn_mp_init.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,12 @@
1515
/* init a new mp_int */
1616
int mp_init(mp_int *a)
1717
{
18-
int i;
19-
2018
/* allocate memory required and clear it */
21-
a->dp = (mp_digit *) XMALLOC(MP_PREC * sizeof(mp_digit));
19+
a->dp = (mp_digit *) XCALLOC((size_t)MP_PREC, sizeof(mp_digit));
2220
if (a->dp == NULL) {
2321
return MP_MEM;
2422
}
2523

26-
/* set the digits to zero */
27-
for (i = 0; i < MP_PREC; i++) {
28-
a->dp[i] = 0;
29-
}
30-
3124
/* set the used to zero, allocated digits to the default precision
3225
* and sign to positive */
3326
a->used = 0;

bn_mp_init_size.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,11 @@
1515
/* init an mp_init for a given size */
1616
int mp_init_size(mp_int *a, int size)
1717
{
18-
int x;
19-
2018
/* pad size so there are always extra digits */
2119
size += (MP_PREC * 2) - (size % MP_PREC);
2220

2321
/* alloc mem */
24-
a->dp = (mp_digit *) XMALLOC((size_t)size * sizeof(mp_digit));
22+
a->dp = (mp_digit *) XCALLOC((size_t)size, sizeof(mp_digit));
2523
if (a->dp == NULL) {
2624
return MP_MEM;
2725
}
@@ -31,11 +29,6 @@ int mp_init_size(mp_int *a, int size)
3129
a->alloc = size;
3230
a->sign = MP_ZPOS;
3331

34-
/* zero the digits */
35-
for (x = 0; x < size; x++) {
36-
a->dp[x] = 0;
37-
}
38-
3932
return MP_OKAY;
4033
}
4134
#endif

0 commit comments

Comments
 (0)