Skip to content

Commit 6e3c533

Browse files
committed
Use quot only if use_quot == true, in bc_divide_ex
1 parent ed8c557 commit 6e3c533

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
@@ -351,8 +351,10 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
351351
return false;
352352
}
353353

354-
bc_free_num(quot);
355354
size_t quot_scale = scale;
355+
if (use_quot) {
356+
bc_free_num(quot);
357+
}
356358

357359
/* If numerator is zero, the quotient is always zero. */
358360
if (bc_is_zero(numerator)) {
@@ -410,12 +412,14 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
410412
}
411413

412414
size_t quot_size = numerator_size - divisor_size + 1; /* numerator_size >= divisor_size */
413-
if (quot_size > quot_scale) {
414-
*quot = bc_new_num_nonzeroed(quot_size - quot_scale, quot_scale);
415-
} else {
416-
*quot = bc_new_num_nonzeroed(1, quot_scale); /* 1 is for 0 */
415+
if (use_quot) {
416+
if (quot_size > quot_scale) {
417+
*quot = bc_new_num_nonzeroed(quot_size - quot_scale, quot_scale);
418+
} else {
419+
*quot = bc_new_num_nonzeroed(1, quot_scale); /* 1 is for 0 */
420+
}
421+
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
417422
}
418-
(*quot)->n_sign = numerator->n_sign == divisor->n_sign ? PLUS : MINUS;
419423

420424
/* Size that can be read from numeratorptr */
421425
size_t numerator_readable_size = numerator->n_len + numerator->n_scale - numerator_leading_zeros;
@@ -433,13 +437,18 @@ bool bc_divide_ex(bc_num numerator, bc_num divisor, bc_num *quot, bc_num *rem, s
433437
quot, quot_size
434438
);
435439

436-
_bc_rm_leading_zeros(*quot);
437-
if (bc_is_zero(*quot)) {
438-
(*quot)->n_sign = PLUS;
440+
if (use_quot) {
441+
_bc_rm_leading_zeros(*quot);
442+
if (bc_is_zero(*quot)) {
443+
(*quot)->n_sign = PLUS;
444+
(*quot)->n_scale = 0;
445+
}
439446
}
440447
return true;
441448

442449
quot_zero:
443-
*quot = bc_copy_num(BCG(_zero_));
450+
if (use_quot) {
451+
*quot = bc_copy_num(BCG(_zero_));
452+
}
444453
return true;
445454
}

0 commit comments

Comments
 (0)