From 216a07a7827ae0e18237c350282fe298b0c3df02 Mon Sep 17 00:00:00 2001 From: Tyler Rockwood Date: Fri, 26 Apr 2024 18:55:04 +0000 Subject: [PATCH] wasm/parser: remove magic number warnings Signed-off-by: Tyler Rockwood --- src/v/wasm/parser/parser.cc | 80 +++++++++++++++++++++++-------------- 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/src/v/wasm/parser/parser.cc b/src/v/wasm/parser/parser.cc index 7861779edf66..78d9410c0c38 100644 --- a/src/v/wasm/parser/parser.cc +++ b/src/v/wasm/parser/parser.cc @@ -200,8 +200,8 @@ parse_signature_types(iobuf_parser_base* parser, size_t max) { function_signature parse_signature(iobuf_parser_base* parser) { auto magic = parser->consume_type(); - // NOLINTNEXTLINE(cppcoreguidelines-avoid-magic-numbers) - if (magic != 0x60) { + constexpr uint8_t signature_start_magic_byte = 0x60; + if (magic != signature_start_magic_byte) { throw parse_exception( fmt::format("function type magic mismatch: {}", magic)); } @@ -240,35 +240,42 @@ declaration::global parse_global_type(iobuf_parser_base* parser) { } void skip_global_constexpr(iobuf_parser_base* parser) { - auto opcode = parser->consume_type(); - // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers) + enum class op : uint8_t { + global_get = 0x23, + i32_const = 0x41, + i64_const = 0x42, + f32_const = 0x43, + f64_const = 0x44, + ref_null = 0xD0, + }; + auto opcode = parser->consume_type(); switch (opcode) { - case 0x23: // global.get - case 0x41: // i32.const + case op::global_get: + case op::i32_const: std::ignore = leb128::decode(parser); break; - case 0x42: // i64.const + case op::i64_const: std::ignore = leb128::decode(parser); break; - case 0x43: // f32.const + case op::f32_const: std::ignore = parser->consume_type(); break; - case 0x44: // f64.const + case op::f64_const: std::ignore = parser->consume_type(); break; - case 0xD0: // ref.null + case op::ref_null: std::ignore = parse_ref_type(parser); break; default: - throw parse_exception( - fmt::format("unimplemented global opcode: {}", opcode)); + throw parse_exception(fmt::format( + "unimplemented global opcode: {}", static_cast(opcode))); } auto end = parser->consume_type(); - if (end != 0x0B) { + constexpr uint8_t end_expression_marker = 0x0B; + if (end != end_expression_marker) { throw parse_exception( fmt::format("expected end of global initalizer, got: {}", end)); } - // NOLINTEND(cppcoreguidelines-avoid-magic-numbers) } class module_extractor { @@ -335,38 +342,52 @@ class module_extractor { } // The size of this section auto size = leb128::decode(_parser); - // NOLINTBEGIN(cppcoreguidelines-avoid-magic-numbers) - switch (id) { - case 0x00: // Custom section + enum class section : uint8_t { + custom = 0x00, + type = 0x01, + import = 0x02, + function = 0x03, + table = 0x04, + memory = 0x05, + global = 0x06, + exprt = 0x07, // export + start = 0x08, + element = 0x09, + data_count = 0x0C, + code = 0x0A, + data = 0x0B, + }; + switch (static_cast
(id)) { + case section::custom: // Skip over custom sections _parser->skip(size); break; - case 0x01: // type section + case section::type: parse_signature_section(); break; - case 0x02: // import section + case section::import: parse_import_section(); break; - case 0x03: // function section + case section::function: parse_function_decl_section(); break; - case 0x04: // table section + case section::table: parse_table_section(); break; - case 0x05: // memory section + case section::memory: parse_memories_section(); break; - case 0x06: // global section + case section::global: parse_globals_section(); break; - case 0x07: // export section + case section::exprt: parse_export_section(); break; - case 0x08: // start section - case 0x09: // element section - case 0x0C: // data count section - case 0x0A: // code section - case 0x0B: // data section + case section::start: + case section::element: + case section::data_count: + case section::code: + case section::data: // Since we don't need the information from these sections at the // moment, we can skip them. _parser->skip(size); @@ -374,7 +395,6 @@ class module_extractor { default: throw parse_exception(fmt::format("unknown section id: {}", id)); } - // NOLINTEND(cppcoreguidelines-avoid-magic-numbers) } void parse_signature_section() {