Skip to content

Commit

Permalink
wasm: Propagate leb128 errors when parsing modules
Browse files Browse the repository at this point in the history
  • Loading branch information
robinlinden committed Oct 31, 2023
1 parent a7fd50d commit a767e37
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 4 additions & 2 deletions wasm/wasm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,12 @@ tl::expected<Module, ModuleParseError> Module::parse_from(std::istream &is) {
return tl::unexpected{ModuleParseError::InvalidSectionId};
}

// TODO(robinlinden): Propagate error from leb128-parsing.
auto size = Leb128<std::uint32_t>::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<std::uint8_t> content;
Expand Down
2 changes: 1 addition & 1 deletion wasm/wasm.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,11 +130,11 @@ struct CodeSection {
};

enum class ModuleParseError {
Unknown,
UnexpectedEof,
InvalidMagic,
UnsupportedVersion,
InvalidSectionId,
InvalidSize,
};

// https://webassembly.github.io/spec/core/syntax/modules.html
Expand Down
8 changes: 7 additions & 1 deletion wasm/wasm_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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", [] {
Expand Down

0 comments on commit a767e37

Please sign in to comment.