Skip to content

ext/bcmath: bounds-check $precision in bcround() and Number::round()#22182

Open
edorian wants to merge 3 commits into
php:masterfrom
edorian:bc-math-out-of-bounds-precission
Open

ext/bcmath: bounds-check $precision in bcround() and Number::round()#22182
edorian wants to merge 3 commits into
php:masterfrom
edorian:bc-math-out-of-bounds-precission

Conversation

@edorian
Copy link
Copy Markdown
Member

@edorian edorian commented May 29, 2026

Fix the ASAN issue with entry points passing $precision to bc_round() unchecked, allowing PHP_INT_MAX / PHP_INT_MIN to trigger oversized allocations and signed overflow in libbcmath/src/round.c.


Transparency Note: The ASAN issue was found during LLM-based scanning. Code partially generated by tooling, reviewed and tested by me.

edorian added 2 commits May 29, 2026 15:30
Fix ASAN issue withh entry points passed $precision to bc_round()
unchecked, allowing PHP_INT_MAX / PHP_INT_MIN to trigger oversized
allocations and signed overflow in libbcmath/src/round.c.
  - remove the zend_always_inline
  - use INT_MIN instead of -INT_MAX
  - use % matching in the tests so they work on 32bit
Comment thread ext/bcmath/bcmath.c Outdated

static zend_result bcmath_check_precision(zend_long precision, uint32_t arg_num)
{
if (UNEXPECTED(precision < INT_MIN || precision > INT_MAX)) {
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.

nit: ZEND_LONG_EXCEEDS_INT macro might works fine for your case.

Copy link
Copy Markdown
Member Author

@edorian edorian May 29, 2026

Choose a reason for hiding this comment

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

Thank you, wasn't aware of that macro.

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