Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix num parsing for invalid multi-byte sign chars #126

Merged
merged 1 commit into from
Aug 30, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 15 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,12 @@ macro_rules! float_trait_impl {
}

fn slice_shift_char(src: &str) -> Option<(char, &str)> {
src.chars().nth(0).map(|ch| (ch, &src[1..]))
let mut chars = src.chars();
if let Some(ch) = chars.next() {
Some((ch, chars.as_str()))
} else {
None
}
}

let (is_positive, src) = match slice_shift_char(src) {
Expand Down Expand Up @@ -395,6 +400,15 @@ fn from_str_radix_unwrap() {
assert_eq!(f, 0.0);
}

#[test]
fn from_str_radix_multi_byte_fail() {
// Ensure parsing doesn't panic, even on invalid sign characters
assert!(f32::from_str_radix("™0.2", 10).is_err());

// Even when parsing the exponent sign
assert!(f32::from_str_radix("0.2E™1", 10).is_err());
}

#[test]
fn wrapping_is_num() {
fn require_num<T: Num>(_: &T) {}
Expand Down