Skip to content

Simplify integer validation in JSON Path parser #96

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

Merged
merged 1 commit into from
May 13, 2025
Merged
Show file tree
Hide file tree
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
35 changes: 9 additions & 26 deletions src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ use pest::Parser;
#[derive(Parser)]
#[grammar = "parser/grammar/json_path_9535.pest"]
pub(super) struct JSPathParser;
const MAX_VAL: i64 = 9007199254740991; // Maximum safe integer value in JavaScript
const MIN_VAL: i64 = -9007199254740991; // Minimum safe integer value in JavaScript
// const MAX_VAL: i64 = 9007199254740991; // Maximum safe integer value in JavaScript
// const MIN_VAL: i64 = -9007199254740991; // Minimum safe integer value in JavaScript

pub type Parsed<T> = Result<T, JsonPathError>;

Expand Down Expand Up @@ -122,13 +122,13 @@ pub fn selector(rule: Pair<Rule>) -> Parsed<Selector> {
validate_js_str(child.as_str().trim())?.to_string(),
)),
Rule::wildcard_selector => Ok(Selector::Wildcard),
Rule::index_selector => Ok(Selector::Index(validate_range(
Rule::index_selector => Ok(Selector::Index(
child
.as_str()
.trim()
.parse::<i64>()
.map_err(|e| (e, "wrong integer"))?,
)?)),
)),
Rule::slice_selector => {
let (start, end, step) = slice_selector(child)?;
Ok(Selector::Slice(start, end, step))
Expand Down Expand Up @@ -237,16 +237,7 @@ pub fn singular_query_segments(rule: Pair<Rule>) -> Parsed<Vec<SingularQuerySegm
}
Ok(segments)
}
fn validate_range(val: i64) -> Result<i64, JsonPathError> {
if val > MAX_VAL || val < MIN_VAL {
Err(JsonPathError::InvalidJsonPath(format!(
"Value {} is out of range",
val
)))
} else {
Ok(val)
}
}

pub fn slice_selector(rule: Pair<Rule>) -> Parsed<(Option<i64>, Option<i64>, Option<i64>)> {
let mut start = None;
let mut end = None;
Expand All @@ -255,12 +246,12 @@ pub fn slice_selector(rule: Pair<Rule>) -> Parsed<(Option<i64>, Option<i64>, Opt

for r in rule.into_inner() {
match r.as_rule() {
Rule::start => start = Some(validate_range(get_int(r)?)?),
Rule::end => end = Some(validate_range(get_int(r)?)?),
Rule::start => start = Some(get_int(r)?),
Rule::end => end = Some(get_int(r)?),
Rule::step => {
step = {
if let Some(int) = r.into_inner().next() {
Some(validate_range(get_int(int)?)?)
Some(get_int(int)?)
} else {
None
}
Expand Down Expand Up @@ -319,15 +310,7 @@ pub fn literal(rule: Pair<Rule>) -> Parsed<Literal> {
if num.contains('.') || num.contains('e') || num.contains('E') {
Ok(Literal::Float(num.parse::<f64>().map_err(|e| (e, num))?))
} else {
let num = num.trim().parse::<i64>().map_err(|e| (e, num))?;
if num > MAX_VAL || num < MIN_VAL {
Err(JsonPathError::InvalidNumber(format!(
"number out of bounds: {}",
num
)))
} else {
Ok(Literal::Int(num))
}
Ok(Literal::Int(num.trim().parse::<i64>().map_err(|e| (e, num))?))
}
}

Expand Down
25 changes: 23 additions & 2 deletions src/parser/grammar/json_path_9535.pest
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ root = _{"$"}
name_selector = {string}
wildcard_selector = {"*"}
index_selector = {int}
int = { "0" | ("-"? ~ DIGIT1 ~ DIGIT*) }
int = { "0" | ("-"? ~ safe_int) }
step = {":" ~ S ~ int?}
start = {int}
end = {int}
Expand Down Expand Up @@ -81,4 +81,25 @@ HEXDIG = _{ DIGIT | "A" | "B" | "C" | "D" | "E" | "F" }
DIGIT = _{ ASCII_DIGIT }
DIGIT1 = _{ ASCII_NONZERO_DIGIT}
ALPHA = { ASCII_ALPHA }
WHITESPACE = _{ " " | "\t" | "\r\n" | "\n" | "\r"}
WHITESPACE = _{ " " | "\t" | "\r\n" | "\n" | "\r"}

// Matches any number less than 9007199254740991 early escape for any number <= 999999999999999
safe_int = _{
(
DIGIT1 ~ DIGIT{0,14} ~ !ASCII_DIGIT // 1 to 15 digits (well below the max)
| '1'..'8' ~ ASCII_DIGIT{15}
| "900" ~ '0'..'6' ~ ASCII_DIGIT{12}
| "90070" ~ ASCII_DIGIT{11}
| "90071" ~ '0'..'8' ~ ASCII_DIGIT{10}
| "900719" ~ '0'..'8' ~ ASCII_DIGIT{9}
| "9007199" ~ '0'..'1' ~ ASCII_DIGIT{8}
| "90071992" ~ '0'..'4' ~ ASCII_DIGIT{7}
| "900719925" ~ '0'..'3' ~ ASCII_DIGIT{6}
| "9007199254" ~ '0'..'6' ~ ASCII_DIGIT{5}
| "90071992547" ~ '0'..'3' ~ ASCII_DIGIT{4}
| "9007199254740" ~ '0'..'8' ~ ASCII_DIGIT{2}
| "90071992547409" ~ '0'..'8' ~ ASCII_DIGIT
| "900719925474099" ~ '0'..'1'
) ~ !ASCII_DIGIT
}