diff --git a/wasm/wasm.cpp b/wasm/wasm.cpp index d18ee632..22ea67ff 100644 --- a/wasm/wasm.cpp +++ b/wasm/wasm.cpp @@ -253,10 +253,12 @@ tl::expected Module::parse_from(std::istream &is) { return tl::unexpected{ModuleParseError::InvalidSectionId}; } - // TODO(robinlinden): Propagate error from leb128-parsing. auto size = Leb128::decode_from(is); if (!size) { - return tl::unexpected{ModuleParseError::Unknown}; + if (size.error() == Leb128ParseError::UnexpectedEof) { + return tl::unexpected{ModuleParseError::UnexpectedEof}; + } + return tl::unexpected{ModuleParseError::InvalidSize}; } std::vector content; diff --git a/wasm/wasm.h b/wasm/wasm.h index d48e2ea2..1eb744d6 100644 --- a/wasm/wasm.h +++ b/wasm/wasm.h @@ -130,11 +130,11 @@ struct CodeSection { }; enum class ModuleParseError { - Unknown, UnexpectedEof, InvalidMagic, UnsupportedVersion, InvalidSectionId, + InvalidSize, }; // https://webassembly.github.io/spec/core/syntax/modules.html diff --git a/wasm/wasm_test.cpp b/wasm/wasm_test.cpp index acc367ea..bdb0c859 100644 --- a/wasm/wasm_test.cpp +++ b/wasm/wasm_test.cpp @@ -395,7 +395,13 @@ int main() { etest::test("missing size", [] { auto wasm_bytes = std::stringstream{"\0asm\1\0\0\0\0"s}; - expect_eq(wasm::Module::parse_from(std::move(wasm_bytes)), tl::unexpected{wasm::ModuleParseError::Unknown}); + expect_eq( + wasm::Module::parse_from(std::move(wasm_bytes)), tl::unexpected{wasm::ModuleParseError::UnexpectedEof}); + }); + + etest::test("invalid size", [] { + auto wasm_bytes = std::stringstream{"\0asm\1\0\0\0\0\x80\x80\x80\x80\x80\x80"s}; + expect_eq(wasm::Module::parse_from(std::move(wasm_bytes)), tl::unexpected{wasm::ModuleParseError::InvalidSize}); }); etest::test("missing content", [] {