RUST-1881 Check integer conversions #1045
Merged
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.
RUST-1881
This vendors the
Checked
type from https://github.com/zeta12ti/Checked, minus thenum_traits
support, plus a few conveniences for our specific error type, and uses it to check both integer conversions and integer arithmetic for particularly sensitive values like buffer sizes. The guidelines I used for this:usize
Checked<usize>
starting at the earliest possible point (ideally, initialization from a constant or single value), and persisting as late as possible so that all arithmetic is also checked.Result
,unwrap
is used on the grounds that a crash is better than silent bad behaviorI added a lint for risky
as
conversions to the toplevellib.rs
so future uses will get caught :)(Once this goes in I will, of course, cherry-pick this into 2.8.x.)