Fix potential underflow in bigint_to_scientific - #791
Merged
Anand Krishnamoorthi (anakrish) merged 1 commit intoAug 19, 2026
Merged
Conversation
bigint_to_scientific
Jay Lorch (jaylorch)
marked this pull request as ready for review
August 19, 2026 03:32
Jay Lorch (jaylorch)
requested review from
Anand Krishnamoorthi (anakrish)
and
a lite review from Copilot
August 19, 2026 03:32
Contributor
There was a problem hiding this comment.
Pull request overview
This pull request adjusts bigint_to_scientific in src/number.rs to avoid a potential overflow/underflow panic caused by casting digits.len() from usize to i32 before subtracting 1.
Changes:
- Replace
digits.len() as i32 - 1withdigits.len() - 1to avoid overflow-sensitive casting.
💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
| } | ||
|
|
||
| let exponent = digits.len() as i32 - 1; | ||
| let exponent = digits.len() - 1; |
Member
Author
There was a problem hiding this comment.
Your suggestion leads to incorrect output, giving the wrong exponent. I don't understand how it fixes anything.
Anand Krishnamoorthi (anakrish)
approved these changes
Aug 19, 2026
Anand Krishnamoorthi (anakrish)
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for identifying and fixing this! It would be great to prove absence of panics.
Anand Krishnamoorthi (anakrish)
merged commit Aug 19, 2026
3496f05
into
microsoft:main
60 checks passed
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.
Now that Verus has some support for
format!, I was able to start checking functions that use it, includingbigint_to_scientific. This uncovered a potential panic in that function. Ifusizeis 64 bits (as it is on many platforms), thendigits.len()can be 2^31 or greater. If it's equal to 2^31, or congruent to 2^31 modulo 2^32, then casting to ani32will producei32::MIN, and then subtracting 1 will panic due to underflow. This PR prevents that in a pretty straightforward way: It just doesn't bother casting to ani32.