Skip to content

Commit 0865562

Browse files
committed
Use quot only if use_quot == true, in bc_divide_ex
1 parent c0000cd commit 0865562

File tree

1 file changed

+19
-10
lines changed
  • ext/bcmath/libbcmath/src

1 file changed

+19
-10
lines changed

ext/bcmath/libbcmath/src/div.c

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -346,8 +346,10 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
346346
return false;
347347
}
348348

349-
bc_free_num(quot);
350349
size_t quot_scale = scale;
350+
if (use_quot) {
351+
bc_free_num(quot);
352+
}
351353

352354
/* If numerator is zero, the quotient is always zero. */
353355
if (bc_is_zero(numerator)) {
@@ -399,12 +401,14 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
399401
numerator_size -= divisor_trailing_zeros;
400402

401403
size_t quot_size = numerator_size - divisor_size + 1; /* numerator_size >= divisor_size */
402-
if (quot_size > quot_scale) {
403-
*quot = bc_new_num_nonzeroed(quot_size - quot_scale, quot_scale);
404-
} else {
405-
*quot = bc_new_num_nonzeroed(1, quot_scale); /* 1 is for 0 */
404+
if (use_quot) {
405+
if (quot_size > quot_scale) {
406+
*quot = bc_new_num_nonzeroed(quot_size - quot_scale, quot_scale);
407+
} else {
408+
*quot = bc_new_num_nonzeroed(1, quot_scale); /* 1 is for 0 */
409+
}
410+
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
406411
}
407-
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
408412

409413
/* Size that can be read from numeratorptr */
410414
size_t numerator_readable_size = numerator->n_len + numerator->n_scale - numerator_leading_zeros;
@@ -422,13 +426,18 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
422426
quot, quot_size
423427
);
424428

425-
_bc_rm_leading_zeros(*quot);
426-
if (bc_is_zero(*quot)) {
427-
(*quot)->n_sign = PLUS;
429+
if (use_quot) {
430+
_bc_rm_leading_zeros(*quot);
431+
if (bc_is_zero(*quot)) {
432+
(*quot)->n_sign = PLUS;
433+
(*quot)->n_scale = 0;
434+
}
428435
}
429436
return true;
430437

431438
quot_zero:
432-
*quot = bc_copy_num(BCG(_zero_));
439+
if (use_quot) {
440+
*quot = bc_copy_num(BCG(_zero_));
441+
}
433442
return true;
434443
}

0 commit comments

Comments
 (0)