srctree

Robin Linden parent a7fd50df a767e37d
wasm: Propagate leb128 errors when parsing modules

inlinesplit
wasm/wasm.cpp added: 15, removed: 7, total 8
@@ -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;
 
wasm/wasm.h added: 15, removed: 7, total 8
@@ -130,11 +130,11 @@ struct CodeSection {
};
 
enum class ModuleParseError {
Unknown,
UnexpectedEof,
InvalidMagic,
UnsupportedVersion,
InvalidSectionId,
InvalidSize,
};
 
// https://webassembly.github.io/spec/core/syntax/modules.html
 
wasm/wasm_test.cpp added: 15, removed: 7, total 8
@@ -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", [] {