Closed HeroicKatora closed 5 years ago
Possible fixed implementation:
fn slice_shift_char(src: &str) -> Option<(char, &str)> {
let mut chars = src.chars();
if let Some(ch) = chars.next() {
Some((ch, chars.as_str())) // available since Rust 1.4.0
} else {
None
}
}
Thanks for the report, and your suggested fix looks great! Can you submit this as a pull request along with corresponding tests? It looks like slice_shift_char
is used both at the beginning of the string and in the exponent position (for input like 1e100
).
play.
Expected behaviour
Nothing hapens. The method returns an error with kind
Invalid
which is ignored.Observed behaviour
The code panics.
Cause analysis
The underlying cause is that
slice_shift_char
splits the string at a hardcode byte offset of1
instead of the byte-length of the first character. This panics when it is not a UTF-8 byte boundary.https://github.com/rust-num/num-traits/blob/58f02a8677c13ad5d46d0eb0698d9052ca70ef73/src/lib.rs#L218-L220