From 212f997e5103e9925051e3fd5af6d740fec294c9 Mon Sep 17 00:00:00 2001 From: aljazerzen Date: Thu, 5 May 2022 11:21:11 +0200 Subject: [PATCH] Fix panic on overflowing integer --- src/lib.rs | 2 ++ src/tokenizer.rs | 16 ++++++++++------ 2 files changed, 12 insertions(+), 6 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index fee5985..7f00284 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1016,6 +1016,8 @@ mod tests { format(input, &QueryParams::Indexed(params), options), expected ); + + format("?62666666121266666612", &QueryParams::None, options); } #[test] diff --git a/src/tokenizer.rs b/src/tokenizer.rs index 0e369c8..3f480d2 100644 --- a/src/tokenizer.rs +++ b/src/tokenizer.rs @@ -301,12 +301,16 @@ fn get_indexed_placeholder_token(input: &str) -> IResult<&str, Token<'_>> { Token { kind: TokenKind::Placeholder, value: token, - key: if token.starts_with('$') { - let index = token[1..].parse::().unwrap(); - Some(PlaceholderKind::OneIndexed(index)) - } else if token.len() > 1 { - let index = token[1..].parse::().unwrap(); - Some(PlaceholderKind::ZeroIndexed(index)) + key: if token.len() > 1 { + if let Ok(index) = token[1..].parse::() { + Some(if token.starts_with('$') { + PlaceholderKind::OneIndexed(index) + } else { + PlaceholderKind::ZeroIndexed(index) + }) + } else { + None + } } else { None },