From 655e0c0357bfab1e532450357a6e6fbd4f504749 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 12 Jun 2020 19:33:46 +0900 Subject: [PATCH] fix reading version bytes on version mismatch error (fix #29) --- wain-syntax-binary/src/parser.rs | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-) diff --git a/wain-syntax-binary/src/parser.rs b/wain-syntax-binary/src/parser.rs index 0ca82b1..c4decc2 100644 --- a/wain-syntax-binary/src/parser.rs +++ b/wain-syntax-binary/src/parser.rs @@ -246,9 +246,8 @@ impl<'s> Parse<'s> for Module<'s> { match parser.input { [0x01, 0x00, 0x00, 0x00, ..] => parser.eat(4), _ => { - return Err( - parser.error(ErrorKind::VersionMismatch(parser.input.try_into().unwrap())) - ) + let bytes = parser.input[..4].try_into().unwrap(); + return Err(parser.error(ErrorKind::VersionMismatch(bytes))); } } @@ -1153,4 +1152,15 @@ mod tests { let mut parser = Parser::new(&bin); let _: Root<'_, _> = unwrap(parser.parse()); } + + #[test] + fn regression_issue_29() { + // .asm.19.asm.195. + let bin: &[_] = b"\x00\x61\x73\x6d\x01\x31\x39\x00\x61\x73\x6d\x01\x31\x39\x35\x01"; + let mut parser = Parser::new(bin); + match parser.parse::>() { + Ok(_) => panic!("unexpected success"), + Err(err) => assert!(matches!(err.kind, ErrorKind::VersionMismatch(_))), + } + } }