Skip to content

Commit

Permalink
Merge pull request #127 from anweiss/126-size-control-validation-errors
Browse files Browse the repository at this point in the history
Fix size control validation errors
  • Loading branch information
anweiss authored Sep 12, 2022
2 parents 45442c5 + 51e0b15 commit a67322d
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 2 deletions.
5 changes: 4 additions & 1 deletion src/validator/cbor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3060,7 +3060,10 @@ where
Some(Token::LE) if i128::from(*i) <= *v as i128 => None,
Some(Token::GT) if i128::from(*i) > *v as i128 => None,
Some(Token::GE) if i128::from(*i) >= *v as i128 => None,
Some(Token::SIZE) if i128::from(*i) < 256i128.pow(*v as u32) => None,
Some(Token::SIZE) => match 256i128.checked_pow(*v as u32) {
Some(n) if i128::from(*i) < n => None,
_ => Some(format!("expected value .size {}, got {:?}", v, i)),
},
Some(Token::BITS) => {
if let Some(sv) = 1u32.checked_shl(*v as u32) {
if (i128::from(*i) & sv as i128) != 0 {
Expand Down
34 changes: 33 additions & 1 deletion src/validator/json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2228,7 +2228,10 @@ impl<'a> Visitor<'a, Error> for JSONValidator<'a> {
Some(Token::LE) if i <= *v as u64 => None,
Some(Token::GT) if i > *v as u64 => None,
Some(Token::GE) if i >= *v as u64 => None,
Some(Token::SIZE) if i < 256u64.pow(*v as u32) => None,
Some(Token::SIZE) => match 256u128.checked_pow(*v as u32) {
Some(n) if (i as u128) < n => None,
_ => Some(format!("expected value .size {}, got {}", v, n)),
},
#[cfg(feature = "additional-controls")]
Some(Token::PLUS) => {
if i == *v as u64 {
Expand Down Expand Up @@ -2594,4 +2597,33 @@ mod tests {

Ok(())
}

#[test]
fn size_control_validation_error() -> std::result::Result<(), Box<dyn std::error::Error>> {
let cddl = indoc!(
r#"
start = Record
Record = {
id: Id
}
Id = uint .size 8
"#
);

let json = r#"{ "id": 5 }"#;

let cddl = cddl_from_str(cddl, true).map_err(json::Error::CDDLParsing);
if let Err(e) = &cddl {
println!("{}", e);
}

let json = serde_json::from_str::<serde_json::Value>(json).map_err(json::Error::JSONParsing)?;

let cddl = cddl.unwrap();

let mut jv = JSONValidator::new(&cddl, json, None);
jv.validate()?;

Ok(())
}
}

0 comments on commit a67322d

Please sign in to comment.