fix: don't panic on invalid hex strings

This commit is contained in:
rvcas 2023-11-15 14:39:51 -05:00 committed by Lucas
parent dfa0378404
commit 308fb47e40
2 changed files with 16 additions and 6 deletions

View File

@ -203,16 +203,24 @@ peg::parser! {
= b:$("True" / "False") { b == "True" }
rule bytestring() -> Vec<u8>
= "#" i:ident()* { hex::decode(String::from_iter(i)).unwrap() }
= "#" i:ident()* {?
hex::decode(String::from_iter(i)).map_err(|_| "Invalid bytestring")
}
rule bls_element() -> Vec<u8>
= "0x" i:ident()* { hex::decode(String::from_iter(i)).unwrap() }
= "0x" i:ident()* {?
hex::decode(String::from_iter(i)).map_err(|_| "Invalid bls element hex")
}
rule g1_element() -> blst::blst_p1
= element:bls_element() { blst::blst_p1::uncompress(&element).unwrap() }
= element:bls_element() {?
blst::blst_p1::uncompress(&element).map_err(|_| "Invalid bls g1 element encoding")
}
rule g2_element() -> blst::blst_p2
= element:bls_element() { blst::blst_p2::uncompress(&element).unwrap() }
= element:bls_element() {?
blst::blst_p2::uncompress(&element).map_err(|_| "Invalid bls g2 element encoding")
}
rule string() -> String
= "\"" s:character()* "\"" { String::from_iter(s) }

View File

@ -16,8 +16,10 @@ const EVALUATION_FAILURE: &str = "evaluation failure";
fn expected_to_program(expected_file: &PathBuf) -> Result<Program<Name>, String> {
let code = fs::read_to_string(expected_file).expect("Failed to read .uplc.expected file");
if code == PARSE_ERROR || code == EVALUATION_FAILURE {
Err(code)
if code.contains(PARSE_ERROR) {
Err(PARSE_ERROR.to_string())
} else if code.contains(EVALUATION_FAILURE) {
Err(EVALUATION_FAILURE.to_string())
} else {
parser::program(&code).map_err(|_| code)
}