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

from_str_radix panics when the first char is encoded with multiple bytes #125

Closed
HeroicKatora opened this issue Aug 30, 2019 · 2 comments · Fixed by #126
Closed

from_str_radix panics when the first char is encoded with multiple bytes #125

HeroicKatora opened this issue Aug 30, 2019 · 2 comments · Fixed by #126

Comments

@HeroicKatora
Copy link
Contributor

HeroicKatora commented Aug 30, 2019

play.

fn main() {
    let _ = f32::from_str_radix("™0.2", 10);
}

Expected behaviour

Nothing hapens. The method returns an error with kind Invalid which is ignored.

Observed behaviour

The code panics.

thread 'main' panicked at 'byte index 1 is not a char boundary; it is inside '™' (bytes 0..3) of `™0.2`', src/libcore/str/mod.rs:2034:5
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.

Cause analysis

The underlying cause is that slice_shift_char splits the string at a hardcode byte offset of 1 instead of the byte-length of the first character. This panics when it is not a UTF-8 byte boundary.

num-traits/src/lib.rs

Lines 218 to 220 in 58f02a8

fn slice_shift_char(src: &str) -> Option<(char, &str)> {
src.chars().nth(0).map(|ch| (ch, &src[1..]))
}

@HeroicKatora
Copy link
Contributor Author

HeroicKatora commented Aug 30, 2019

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
     }
 }

@cuviper
Copy link
Member

cuviper commented Aug 30, 2019

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).

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 a pull request may close this issue.

2 participants