Skip to content
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

Closed
wants to merge 2 commits into from

Conversation

SakiTakamachi
Copy link
Member

fixes #17064

@@ -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);
Copy link
Member Author

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.

Comment on lines +22 to +29
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);
}
Copy link
Member Author

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.

@SakiTakamachi SakiTakamachi marked this pull request as ready for review December 6, 2024 16:44
Copy link
Member

@nielsdos nielsdos left a 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

}

/* If precision is -3, it becomes 1000. */
*result = bc_new_num(-precision + 1, 0);
Copy link
Member

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.

Copy link
Member Author

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);
}

Copy link
Member

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.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

SakiTakamachi added a commit that referenced this pull request Dec 16, 2024
* PHP-8.4:
  Correctly round rounding mode with zero edge case (#17065)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants