Chromium Code Reviews| Index: src/wasm/module-decoder.cc |
| diff --git a/src/wasm/module-decoder.cc b/src/wasm/module-decoder.cc |
| index 4653e1d44bd7b53b145afb4bc8fbf8843231f6af..086788f4e8ccf8a271fe180ead88119b35bc62b5 100644 |
| --- a/src/wasm/module-decoder.cc |
| +++ b/src/wasm/module-decoder.cc |
| @@ -78,18 +78,17 @@ class ModuleDecoder : public Decoder { |
| // Decode the module sections. |
| while (pc_ < limit_) { |
| TRACE("DecodeSection\n"); |
| - uint8_t section_u8 = consume_u8("section"); |
| + int length; |
| + uint32_t section_bytes = consume_u32v(&length, "section size"); |
|
titzer
2016/03/07 08:45:33
Should the section size also include the string si
JF
2016/03/09 02:38:57
Agreed, fixed.
|
| - if (section_u8 >= kMaxModuleSectionCode) { |
| + WasmSectionDeclCode section = consume_section_name(); |
| + if (section == kMaxModuleSectionCode) { |
| // Skip unknown section. |
| - int length; |
| - uint32_t section_bytes = consume_u32v(&length, "section size"); |
| consume_bytes(section_bytes); |
| continue; |
| } |
| // Each section should appear at most once. |
| - auto section = static_cast<WasmSectionDeclCode>(section_u8); |
| CheckForPreviousSection(sections, section, false); |
| sections[section] = true; |
| @@ -521,6 +520,35 @@ class ModuleDecoder : public Decoder { |
| return consume_offset(name ? name : "string"); |
| } |
| + // Reads a section name. |
| + WasmSectionDeclCode consume_section_name() { |
| + static const char* sections[] = { |
| +#define F(enumerator, string) string, |
| + FOR_EACH_WASM_SECTION_TYPE(F) |
| +#undef F |
| + }; |
| + static uint8_t sizes[] { |
| +#define F(enumerator, string) sizeof(string), |
| + FOR_EACH_WASM_SECTION_TYPE(F) |
| +#undef F |
| + }; |
| + static_assert( |
| + sizeof(sections) / sizeof(sections[0]) == kMaxModuleSectionCode, |
| + "expected enum WasmSectionDeclCode to be monotonic from 0"); |
| + int length; |
| + uint32_t string_bytes = consume_u32v(&length, "section module name"); |
| + const byte* start = pc_; |
| + consume_bytes(string_bytes); |
| + if (failed()) return kMaxModuleSectionCode; |
| + for (size_t i = 0; i != kMaxModuleSectionCode; ++i) { |
| + if (sizes[i] == string_bytes && |
| + 0 == memcmp(sections[i], start, string_bytes)) { |
| + return (WasmSectionDeclCode)i; |
| + } |
| + } |
| + return kMaxModuleSectionCode; |
| + } |
| + |
| // Reads a single 8-bit integer, interpreting it as a local type. |
| LocalType consume_local_type() { |
| byte val = consume_u8("local type"); |