Skip to content

Fix for parsing floats that don't include . or e#23

Open
zjijz wants to merge 1 commit intowlentz:masterfrom
zjijz:master
Open

Fix for parsing floats that don't include . or e#23
zjijz wants to merge 1 commit intowlentz:masterfrom
zjijz:master

Conversation

@zjijz
Copy link

@zjijz zjijz commented Jul 10, 2021

Rust allows strings of integral values to be parsed to f32 or f64 using the str::parse method. I was trying to use scan_fmt to parse an integral value to a floating point, but I was getting the following panic:
Code:

let res = scan_fmt::scan_fmt!("1000", "{f}");

Error:

thread 'parse::test_integer_for_float' panicked at 'index out of bounds: the len is 4 but the index is 4', src/parse.rs:68:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

The issue had to do with scan_dec10_nest potentially exhausting the input, but then scan_float called vs.cur() to check for . or e, which would throw the out-of-bounds error since we were at end-of-input. The fix for this was to check if we were at end-of-input before doing the check for . or e.

I added a test (test_integer_for_float) to check for this edge case and ensured none of the other tests failed with the change.

I also manually checked the other uses of scan_dec10_nest (and scan_dec10) and it didn't seem like it was used in a location that assumed end-of-input was not reached already.

@zjijz zjijz changed the title Fix for floats that don't include . or e Fix for parsing floats that don't include . or e Jul 10, 2021
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 this pull request may close these issues.

1 participant