Skip to content

Fix potential underflow in bigint_to_scientific - #791

Merged
Anand Krishnamoorthi (anakrish) merged 1 commit into
microsoft:mainfrom
jaylorch:fix-bigint-to-scientific
Aug 19, 2026
Merged

Fix potential underflow in bigint_to_scientific#791
Anand Krishnamoorthi (anakrish) merged 1 commit into
microsoft:mainfrom
jaylorch:fix-bigint-to-scientific

Conversation

@jaylorch

Copy link
Copy Markdown
Member

Now that Verus has some support for format!, I was able to start checking functions that use it, including bigint_to_scientific. This uncovered a potential panic in that function. If usize is 64 bits (as it is on many platforms), then digits.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 an i32 will produce i32::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 an i32.

@jaylorch Jay Lorch (jaylorch) changed the title Fix potential underflow in bigint_to_scientific Fix potential underflow in bigint_to_scientific Aug 19, 2026
@jaylorch
Jay Lorch (jaylorch) marked this pull request as ready for review August 19, 2026 03:32

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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 - 1 with digits.len() - 1 to avoid overflow-sensitive casting.

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/number.rs
}

let exponent = digits.len() as i32 - 1;
let exponent = digits.len() - 1;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Your suggestion leads to incorrect output, giving the wrong exponent. I don't understand how it fixes anything.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for identifying and fixing this! It would be great to prove absence of panics.

@anakrish
Anand Krishnamoorthi (anakrish) merged commit 3496f05 into microsoft:main Aug 19, 2026
60 checks passed
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.

3 participants