-
Notifications
You must be signed in to change notification settings - Fork 8k
ext/bcmath - Fixed GH-17064: Correctly round rounding mode with zero edge case #17065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,10 +33,50 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) | |
* - If the fractional part ends with zeros, the zeros are omitted and the number of digits in num is reduced. | ||
* Meaning we might end up in the previous case. | ||
*/ | ||
|
||
/* e.g. value is 0.1 and precision is -3, ret is 0 or 1000 */ | ||
if (precision < 0 && num->n_len < (size_t) (-(precision + Z_L(1))) + 1) { | ||
*result = bc_copy_num(BCG(_zero_)); | ||
switch (mode) { | ||
case PHP_ROUND_HALF_UP: | ||
case PHP_ROUND_HALF_DOWN: | ||
case PHP_ROUND_HALF_EVEN: | ||
case PHP_ROUND_HALF_ODD: | ||
case PHP_ROUND_TOWARD_ZERO: | ||
*result = bc_copy_num(BCG(_zero_)); | ||
return; | ||
|
||
case PHP_ROUND_CEILING: | ||
if (num->n_sign == MINUS) { | ||
*result = bc_copy_num(BCG(_zero_)); | ||
return; | ||
} | ||
break; | ||
|
||
case PHP_ROUND_FLOOR: | ||
if (num->n_sign == PLUS) { | ||
*result = bc_copy_num(BCG(_zero_)); | ||
return; | ||
} | ||
break; | ||
|
||
case PHP_ROUND_AWAY_FROM_ZERO: | ||
break; | ||
|
||
EMPTY_SWITCH_DEFAULT_CASE() | ||
} | ||
|
||
if (bc_is_zero(num)) { | ||
*result = bc_copy_num(BCG(_zero_)); | ||
return; | ||
} | ||
|
||
/* If precision is -3, it becomes 1000. */ | ||
*result = bc_new_num(-precision + 1, 0); | ||
(*result)->n_value[0] = 1; | ||
(*result)->n_sign = num->n_sign; | ||
return; | ||
} | ||
|
||
/* Just like bcadd('1', '1', 4) becomes '2.0000', it pads with zeros at the end if necessary. */ | ||
if (precision >= 0 && num->n_scale <= precision) { | ||
if (num->n_scale == precision) { | ||
|
@@ -61,7 +101,7 @@ void bc_round(bc_num num, zend_long precision, zend_long mode, bc_num *result) | |
* If the result of rounding is carried over, it will be added later, so first set it to 0 here. | ||
*/ | ||
if (rounded_len == 0) { | ||
*result = bc_copy_num(BCG(_zero_)); | ||
*result = bc_new_num(1, 0); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not related to this bug, but it was changing a global value and was dangerous, so I fixed it incidentally. |
||
} else { | ||
*result = bc_new_num(num->n_len, precision > 0 ? precision : 0); | ||
memcpy((*result)->n_value, num->n_value, rounded_len); | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,7 @@ bcmath | |
<?php | ||
foreach (RoundingMode::cases() as $mode) { | ||
foreach ([ | ||
'0', | ||
'0.1', | ||
'-0.1', | ||
'1.0', | ||
|
@@ -19,17 +20,20 @@ foreach (RoundingMode::cases() as $mode) { | |
'2.5', | ||
'-2.5', | ||
] as $number) { | ||
$func_ret = bcround($number, 0, $mode); | ||
$method_ret = (new BcMath\Number($number))->round(0, $mode); | ||
if ($method_ret->compare($func_ret) !== 0) { | ||
echo "Result is incorrect.\n"; | ||
var_dump($number, $mode, $func_ret, $method_ret); | ||
foreach ([0, 5, -5] as $scale) { | ||
$func_ret = bcround($number, $scale, $mode); | ||
$method_ret = (new BcMath\Number($number))->round($scale, $mode); | ||
if ($method_ret->compare($func_ret) !== 0) { | ||
echo "Result is incorrect.\n"; | ||
var_dump($number, $mode, $func_ret, $method_ret); | ||
} | ||
Comment on lines
+23
to
+29
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation added with foreach, making it difficult to see the change. I increased the test cases for scale 0 to test cases for scale 0, 5, and -5. |
||
} | ||
} | ||
} | ||
|
||
foreach (RoundingMode::cases() as $mode) { | ||
foreach ([ | ||
'0', | ||
'1.2345678', | ||
'-1.2345678', | ||
] as $number) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This line will break under UBSAN for the following code:
This gives:
But since this is an extreme edge case that won't be hit in practice, I think it suffices here to just return 0 as a sentinel and throw an exception.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@nielsdos
Nice point, thanks!
The first argument is
size_t
, so how about this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sure, it doesn't matter much as it'll fail to allocate that much memory anyway but it's a simple approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done!