Skip to content

Commit ac9342d

Browse files
committed
Fixed array allocation
1 parent ec9f8df commit ac9342d

File tree

1 file changed

+10
-6
lines changed

1 file changed

+10
-6
lines changed

ext/bcmath/libbcmath/src/recmul.c

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,15 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, int n2len, bc_nu
100100
size_t n2_arr_size = n2len / BC_LONGABLE_DIGITS + (n2len % BC_LONGABLE_DIGITS ? 1 : 0);
101101
size_t prod_arr_size = n1_arr_size + n2_arr_size - 1;
102102

103-
unsigned long *n1_l = emalloc(n1_arr_size * sizeof(unsigned long));
104-
unsigned long *n2_l = emalloc(n2_arr_size * sizeof(unsigned long));
105-
unsigned long *prod_l = ecalloc(prod_arr_size, sizeof(unsigned long));
103+
unsigned long *buf = emalloc((n1_arr_size + n2_arr_size + prod_arr_size) * sizeof(unsigned long));
104+
105+
unsigned long *n1_l = buf;
106+
unsigned long *n2_l = buf + n1_arr_size;
107+
unsigned long *prod_l = n2_l + n2_arr_size;
108+
109+
for (i = 0; i < prod_arr_size; i++) {
110+
prod_l[i] = 0;
111+
}
106112

107113
/* Convert n1 to long[] */
108114
i = 0;
@@ -162,9 +168,7 @@ static void bc_standard_mul(bc_num n1, size_t n1len, bc_num n2, int n2len, bc_nu
162168
prod_l[i] /= BASE;
163169
}
164170

165-
efree(n1_l);
166-
efree(n2_l);
167-
efree(prod_l);
171+
efree(buf);
168172
}
169173

170174
/* The multiply routine. N2 times N1 is put int PROD with the scale of

0 commit comments

Comments
 (0)