Skip to content

Prevent int overflow on $decimals in number_format for PHP < 8.3 #11714

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

Closed
wants to merge 2 commits into from

Conversation

marc-mabe
Copy link
Contributor

@marc-mabe marc-mabe commented Jul 16, 2023

This is a follow-up PR for #11649 targeting 8.2

As #11487 is available in 8.3 only and a positive $decimals will most probably end up in OOM error ... I don't have a good idea how to test this

@@ -1156,7 +1157,17 @@ PHP_FUNCTION(number_format)
thousand_sep_len = 1;
}

RETURN_STR(_php_math_number_format_ex(num, (int)dec, dec_point, dec_point_len, thousand_sep, thousand_sep_len));
#if SIZEOF_ZEND_LONG > SIZEOF_INT
Copy link
Member

@nielsdos nielsdos Jul 16, 2023

Choose a reason for hiding this comment

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

You can use ZEND_LONG_INT_OVFL & ZEND_LONG_INT_UDFL which will allow you to avoid this #if and make the code a bit cleaner. These macros will resolve to false if zend_long and int are the same size.
There's also ZEND_LONG_EXCEEDS_INT, but that's unusable here because you still want to set dec_int to a sane value.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated

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.

This looks logical to me and seems to work right. If no more comments of other reviewers arrive I'll go ahead and merge.
I think this also needs to be applied to 8.1 though, but no need to change the target branch as this will require manual merging anyway and I can deal with it.
I understand the issues with the test you listed in #11649. Upon merging into 8.3 I can take your tests from that other PR.

@nielsdos nielsdos closed this in 429f20e Jul 21, 2023
@marc-mabe marc-mabe deleted the 82-num-format-int-overflow branch June 16, 2024 07:42
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