Skip to content

Commit a4a9447

Browse files
czurniedensjaeckel
authored andcommitted
Do not assume that more than enough memory is automatically allocated and set to '0'
1 parent 13f6a2d commit a4a9447

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

s_mp_div_school.c

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
2020
{
2121
mp_int q, x, y, t1, t2;
22+
mp_digit xdpi;
2223
int n, t, i, norm;
2324
bool neg;
2425
mp_err err;
@@ -68,14 +69,16 @@ mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
6869
if (i > x.used) {
6970
continue;
7071
}
72+
/* Do not assume that more than enough memory is automatically allocated and set to '0' */
73+
xdpi = (i == x.used) ? 0u : x.dp[i];
7174

7275
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
7376
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
74-
if (x.dp[i] == y.dp[t]) {
77+
if (xdpi == y.dp[t]) {
7578
q.dp[(i - t) - 1] = ((mp_digit)1 << (mp_digit)MP_DIGIT_BIT) - (mp_digit)1;
7679
} else {
7780
mp_word tmp;
78-
tmp = (mp_word)x.dp[i] << (mp_word)MP_DIGIT_BIT;
81+
tmp = (mp_word)xdpi << (mp_word)MP_DIGIT_BIT;
7982
tmp |= (mp_word)x.dp[i - 1];
8083
tmp /= (mp_word)y.dp[t];
8184
if (tmp > (mp_word)MP_MASK) {
@@ -103,7 +106,7 @@ mp_err s_mp_div_school(const mp_int *a, const mp_int *b, mp_int *c, mp_int *d)
103106
/* find right hand */
104107
t2.dp[0] = ((i - 2) < 0) ? 0u : x.dp[i - 2];
105108
t2.dp[1] = x.dp[i - 1]; /* i >= 1 always holds */
106-
t2.dp[2] = x.dp[i];
109+
t2.dp[2] = xdpi;
107110
t2.used = 3;
108111
} while (mp_cmp_mag(&t1, &t2) == MP_GT);
109112

0 commit comments

Comments
 (0)