You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The from_str methods of all integer types, signed and unsigned, may return Some() values in the case of an overflow. For example, between 256 and 1000, u8::from_str() returns None for less than half of the values and Some(nonsense) for the rest.
Floats don't seem to have this problem, but are inconsistent in their behavior on overflow:
f32::from_str("1000000000000000000000000000000000000000");// => Some(inf)
f32::from_str("10000000000000000000000000000000000000000");// => None
f32::from_str("1e40");// same number as above => Some(inf)
Why this happens:
from_str_bytes_common (from strconv.rs), which is used by all these methods, does the following:
It converts the string byte by byte and keeps an accumulator (of the type it converts to) with the value of the bytes already converted. After every byte it tries to detect overflows by checking if the accumulator got smaller.
The problem is that this only works if the value added to the accumulator is less than the maximum value its type can hold. In cases where the added value is bigger, the accumulator can overflow and still end up larger than before.
Example:
u8::from_str("1000");// byte 1: acc = 1, old = 0// byte 2: acc = 10, old = 1// byte 3: acc = 100, old = 10// byte 4: acc = 232, old = 100
The text was updated successfully, but these errors were encountered:
Here's a fix for issue #7588, "Overflow handling of from_str methods is broken".
The integer overflow issues are taken care of by checking to see if the multiply-by-radix-and-add-next-digit process is reversible. If it overflowed, then some information is lost and the process is irreversible, in which case, None is returned.
Floats now consistently return Some(Inf) of Some(-Inf) on overflow thanks to a call to NumStrConv::inf() and NumStrConv::neg_inf() respectively when the overflow is detected (which yields a value of None in the case of ints and uints anyway).
This is my first contribution to Rust, and my first time using the language in general, so any and all feedback is appreciated.
Re-write shadow lints
changelog: Move shadow_unrelated to restriction
changelog: The shadow lints find a lot more shadows and are not limited to certain patterns
Drastically simplifies the implementation. Catches a lot more cases.
I removed the "initialization happens here" note. It is not helpful IMO.
Closesrust-lang#318Fixesrust-lang#2890Fixesrust-lang#6563Fixesrust-lang#7588Fixesrust-lang#7620
The from_str methods of all integer types, signed and unsigned, may return Some() values in the case of an overflow. For example, between 256 and 1000, u8::from_str() returns None for less than half of the values and Some(nonsense) for the rest.
Examples:
Floats don't seem to have this problem, but are inconsistent in their behavior on overflow:
Why this happens:
from_str_bytes_common (from strconv.rs), which is used by all these methods, does the following:
It converts the string byte by byte and keeps an accumulator (of the type it converts to) with the value of the bytes already converted. After every byte it tries to detect overflows by checking if the accumulator got smaller.
The problem is that this only works if the value added to the accumulator is less than the maximum value its type can hold. In cases where the added value is bigger, the accumulator can overflow and still end up larger than before.
Example:
The text was updated successfully, but these errors were encountered: