Fix repeatString overflow test on 32-bit platforms#2680
Open
jandubois wants to merge 1 commit intomikefarah:masterfrom
Open
Fix repeatString overflow test on 32-bit platforms#2680jandubois wants to merge 1 commit intomikefarah:masterfrom
jandubois wants to merge 1 commit intomikefarah:masterfrom
Conversation
The test literal "ab" * 4611686018427387904 (2^62) exceeds MaxInt32,
so parseInt rejects it before the size guard runs. Compute the count
with 1 << (bits.UintSize - 2) to yield 2^30 on 32-bit and 2^62 on
64-bit. Both values, when doubled by len("ab"), wrap past MaxInt and
bypass a naive len*count guard, exercising the division-safe check
added in mikefarah#2644.
Signed-off-by: Jan Dubois <jan@jandubois.com>
Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Thanks!
Probably just running on x86 on CI should suffice for detecting regressions, but this works as-is. |
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.
The test literal "ab" * 4611686018427387904 (2^62) exceeds MaxInt32, so parseInt rejects it before the size guard runs. Compute the count with 1 << (bits.UintSize - 2) to yield 2^30 on 32-bit and 2^62 on 64-bit. Both values, when doubled by len("ab"), wrap past MaxInt and bypass a naive len*count guard, exercising the division-safe check added in #2644.
Ref #2644 (comment)
I've verified that the modified test case still triggers the original panic on 64-bit platforms unless the fix from #2644 is applied.
I've also verified that this change fixes cross-compilation to 32-bit mode.
I am NOT able to test the 32-bit binary itself, so I don't know if it will pass tests, and I don't know if this code will still trigger the original bug. From a pure math perspective it should.