-
Notifications
You must be signed in to change notification settings - Fork 7.8k
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
ext/bcmath - Fixed GH-17064: Correctly round rounding mode with zero edge case #17065
Conversation
cc2359a
to
8d33f2b
Compare
@@ -61,7 +96,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 comment
The 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.
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); | ||
} |
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.
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.
8d33f2b
to
8e0d7dc
Compare
8e0d7dc
to
d552f23
Compare
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.
Almost right, one small remark
ext/bcmath/libbcmath/src/round.c
Outdated
} | ||
|
||
/* If precision is -3, it becomes 1000. */ | ||
*result = bc_new_num(-precision + 1, 0); |
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:
<?php
echo bcround("0.01", PHP_INT_MIN, RoundingMode::AwayFromZero);
This gives:
/run/media/niels/MoreData/php-8.4/ext/bcmath/libbcmath/src/round.c:74:13: runtime error: negation of -9223372036854775808 cannot be represented in type 'zend_long' (aka 'long'); cast to an unsigned type to negate this value to itself
SUMMARY: UndefinedBehaviorSanitizer: undefined-behavior /run/media/niels/MoreData/php-8.4/ext/bcmath/libbcmath/src/round.c:74:13
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?
if (UNEXPECTED(precision == ZEND_LONG_MIN)) {
*result = bc_new_num((size_t) ZEND_LONG_MAX + 2, 0);
}
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!
* PHP-8.4: Correctly round rounding mode with zero edge case (#17065)
fixes #17064