Skip to content

Commit

Permalink
Makes xsd:dateTime year parsing stricter
Browse files Browse the repository at this point in the history
Do not allow syntaxes not allowed by the grammar
  • Loading branch information
Tpt committed Jun 5, 2023
1 parent 5f2c9a3 commit edec370
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 3 deletions.
4 changes: 4 additions & 0 deletions lib/oxsdatatypes/src/date_time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2024,6 +2024,10 @@ mod tests {
assert_eq!(GDay::from_str("---01")?.to_string(), "---01");
assert_eq!(GMonth::from_str("--01+01:00")?.to_string(), "--01+01:00");
assert_eq!(GMonth::from_str("--01")?.to_string(), "--01");

assert!(GYear::from_str("02020").is_err());
assert!(GYear::from_str("+2020").is_err());
assert!(GYear::from_str("33").is_err());
Ok(())
}

Expand Down
9 changes: 6 additions & 3 deletions lib/oxsdatatypes/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,12 +432,15 @@ fn year_frag(input: &str) -> Result<(i64, &str), XsdParseError> {
(1, input)
};
let (number_str, input) = integer_prefix(input);
let number = i64::from_str(number_str)?;
if number < 1000 && number_str.len() != 4 {
if number_str.len() < 4 {
return Err(XsdParseError::msg("The year should be encoded on 4 digits"));
}
if number_str.len() > 4 && number_str.starts_with('0') {
return Err(XsdParseError::msg(
"The years below 1000 must be encoded on exactly 4 digits",
"The years value must not start with 0 if it can be encoded in at least 4 digits",
));
}
let number = i64::from_str(number_str)?;
Ok((sign * number, input))
}

Expand Down

0 comments on commit edec370

Please sign in to comment.