Skip to content

Commit f31f801

Browse files
committed
Change "invalid table index" to validation error
1 parent 945d6c4 commit f31f801

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

lib/fizzy/parser.cpp

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -341,8 +341,14 @@ inline parser_result<Element> parse(const uint8_t* pos, const uint8_t* end)
341341
{
342342
TableIdx table_index;
343343
std::tie(table_index, pos) = leb128u_decode<uint32_t>(pos, end);
344+
345+
// TODO: The check should be table_index < num_of_tables (0 or 1),
346+
// but access to the module is needed.
344347
if (table_index != 0)
345-
throw parser_error{"unexpected tableidx value " + std::to_string(table_index)};
348+
{
349+
throw validation_error{
350+
"invalid table index " + std::to_string(table_index) + " (only table 0 is allowed)"};
351+
}
346352

347353
ConstantExpression offset;
348354
// Offset expression is required to have i32 result value
@@ -588,7 +594,10 @@ std::unique_ptr<const Module> parse(bytes_view input)
588594
}
589595

590596
if (!module->elementsec.empty() && !module->has_table())
591-
throw validation_error{"element section encountered without a table section"};
597+
{
598+
throw validation_error{
599+
"invalid table index 0 (element section encountered without a table section)"};
600+
}
592601

593602
const auto total_func_count = module->get_function_count();
594603

test/unittests/parser_test.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -930,7 +930,8 @@ TEST(parser, element_section_tableidx_nonzero)
930930
const auto section_contents = bytes{0x01, 0x01, 0x41, 0x01, 0x0b, 0x01, 0x00};
931931
const auto bin = bytes{wasm_prefix} + make_section(9, section_contents);
932932

933-
EXPECT_THROW_MESSAGE(parse(bin), parser_error, "unexpected tableidx value 1");
933+
EXPECT_THROW_MESSAGE(
934+
parse(bin), validation_error, "invalid table index 1 (only table 0 is allowed)");
934935
}
935936

936937
TEST(parser, element_section_invalid_initializer)
@@ -945,8 +946,8 @@ TEST(parser, element_section_no_table_section)
945946
{
946947
const auto wasm =
947948
bytes{wasm_prefix} + make_section(9, make_vec({"0041000b"_bytes + make_vec({"00"_bytes})}));
948-
EXPECT_THROW_MESSAGE(
949-
parse(wasm), validation_error, "element section encountered without a table section");
949+
EXPECT_THROW_MESSAGE(parse(wasm), validation_error,
950+
"invalid table index 0 (element section encountered without a table section)");
950951
}
951952

952953
TEST(parser, code_section_empty)

0 commit comments

Comments
 (0)