ext/bcmath: Prevent overflow of uint32_t/uint64_t#14297
Merged
Conversation
SakiTakamachi
force-pushed
the
fix_bcmul_overflow
branch
from
May 22, 2024 13:43
b57b6ee to
c283029
Compare
If add more than a certain number of times, it will overflow, so need to adjust the digits before adding.
SakiTakamachi
force-pushed
the
fix_bcmul_overflow
branch
from
May 22, 2024 13:48
c283029 to
0a1de07
Compare
ndossche
reviewed
May 22, 2024
ndossche
left a comment
Member
There was a problem hiding this comment.
Nice approach and good catch, thanks!
| # define BC_MUL_UINT_DIGITS 8 | ||
| # define BC_MUL_UINT_OVERFLOW 100000000 | ||
| # define BC_MUL_UINT_OVERFLOW (BC_UINT_T) 100000000 | ||
| # define BC_MUL_MAX_ADD_COUNT (ULONG_MAX / (BC_MUL_UINT_OVERFLOW * BC_MUL_UINT_OVERFLOW)) |
Member
There was a problem hiding this comment.
Note that the size of a long is not necessarily the size of size_t.
I think I prefer a single definition:
#define BC_MUL_MAX_ADD_COUNT (~((BC_UINT_T) 0) / (BC_MUL_UINT_OVERFLOW * BC_MUL_UINT_OVERFLOW))
ndossche
approved these changes
May 22, 2024
ndossche
left a comment
Member
There was a problem hiding this comment.
Seems right to me, thanks Saki!
SakiTakamachi
marked this pull request as ready for review
May 22, 2024 16:20
Member
Author
|
I'll merge this after CI passes |
Girgias
approved these changes
May 22, 2024
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
If add more than a certain number of times, it will overflow, so need to adjust the digits before adding.