1
0
mirror of https://github.com/bspeice/dtparse synced 2024-12-22 12:28:08 -05:00

Merge pull request #7 from bspeice/fuzzing_fix

Fixes #6
This commit is contained in:
bspeice 2018-06-29 22:55:57 -04:00 committed by GitHub
commit 9423a1c0a2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 26 additions and 2 deletions

View File

@ -1218,6 +1218,21 @@ impl Parser {
let len_l = tokens.len(); let len_l = tokens.len();
let mut hms_idx = None; let mut hms_idx = None;
// There's a super weird edge case that can happen
// because Python safely handles negative array indices,
// and Rust (because of usize) does not.
let idx_minus_two = if idx == 1 && len_l > 0 {
len_l - 1
} else if idx == 0 && len_l > 1 {
len_l - 2
} else if idx > 1 {
idx - 2
} else if len_l == 0{
panic!("Attempting to find_hms_index() wih no tokens.");
} else {
0
};
if idx + 1 < len_l && info.get_hms(&tokens[idx + 1]).is_some() { if idx + 1 < len_l && info.get_hms(&tokens[idx + 1]).is_some() {
hms_idx = Some(idx + 1) hms_idx = Some(idx + 1)
} else if allow_jump && idx + 2 < len_l && tokens[idx + 1] == " " } else if allow_jump && idx + 2 < len_l && tokens[idx + 1] == " "
@ -1227,7 +1242,7 @@ impl Parser {
} else if idx > 0 && info.get_hms(&tokens[idx - 1]).is_some() { } else if idx > 0 && info.get_hms(&tokens[idx - 1]).is_some() {
hms_idx = Some(idx - 1) hms_idx = Some(idx - 1)
} else if len_l > 0 && idx > 0 && idx == len_l - 1 && tokens[idx - 1] == " " } else if len_l > 0 && idx > 0 && idx == len_l - 1 && tokens[idx - 1] == " "
&& info.get_hms(&tokens[idx - 2]).is_some() && info.get_hms(&tokens[idx_minus_two]).is_some()
{ {
hms_idx = Some(idx - 2) hms_idx = Some(idx - 2)
} }

View File

@ -1,8 +1,17 @@
use ParseError; use chrono::NaiveDate;
use std::collections::HashMap;
use parse; use parse;
use ParseError;
use Parser;
#[test] #[test]
fn test_fuzz() { fn test_fuzz() {
assert_eq!(parse("\x2D\x38\x31\x39\x34\x38\x34"), Err(ParseError::InvalidMonth)); assert_eq!(parse("\x2D\x38\x31\x39\x34\x38\x34"), Err(ParseError::InvalidMonth));
let default = NaiveDate::from_ymd(2016, 6, 29).and_hms(0, 0, 0);
let mut p = Parser::default();
let res = p.parse("\x0D\x31", None, None, false, false, Some(&default), false, HashMap::new()).unwrap();
assert_eq!(res.0, default);
} }