Skip to content

Fix repeatString overflow test on 32-bit platforms#2680

Open
jandubois wants to merge 1 commit intomikefarah:masterfrom
jandubois:fix-repeat-overflow-32bit
Open

Fix repeatString overflow test on 32-bit platforms#2680
jandubois wants to merge 1 commit intomikefarah:masterfrom
jandubois:fix-repeat-overflow-32bit

Conversation

@jandubois
Copy link
Copy Markdown
Contributor

@jandubois jandubois commented Apr 23, 2026

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.

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

Thanks!

Builds fine on armhf and x86

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.

Probably just running on x86 on CI should suffice for detecting regressions, but this works as-is.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants