Skip to content

ext/bcmath - Fixed GH-17064: Correctly round rounding mode with zero edge case - #17065

Closed
SakiTakamachi wants to merge 2 commits into
php:PHP-8.4from
SakiTakamachi:fix/gh17064
Closed

SakiTakamachi wants to merge 2 commits into
php:PHP-8.4from
SakiTakamachi:fix/gh17064

Conversation

@SakiTakamachi

Copy link
Copy Markdown
Member

fixes #17064

Comment thread ext/bcmath/libbcmath/src/round.c Outdated

Copy link
Copy Markdown
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

Copy link
Copy Markdown
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

@ndossche ndossche left a comment

Copy link
Copy Markdown
Member

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

Comment thread ext/bcmath/libbcmath/src/round.c Outdated
}

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

Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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)
@SakiTakamachi
SakiTakamachi deleted the fix/gh17064 branch April 23, 2025 23:33
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