 Chromium Code Reviews
 Chromium Code Reviews Issue 2065043002:
  [wasm] Check for duplicate export names  (Closed) 
  Base URL: https://chromium.googlesource.com/v8/v8.git@master
    
  
    Issue 2065043002:
  [wasm] Check for duplicate export names  (Closed) 
  Base URL: https://chromium.googlesource.com/v8/v8.git@master| Index: src/wasm/module-decoder.cc | 
| diff --git a/src/wasm/module-decoder.cc b/src/wasm/module-decoder.cc | 
| index 48f59cefa82bdf1d293da3eb529d4244e7dc2485..388868ed0f039bfef22daeee6e02126d5b65af86 100644 | 
| --- a/src/wasm/module-decoder.cc | 
| +++ b/src/wasm/module-decoder.cc | 
| @@ -358,6 +358,32 @@ class ModuleDecoder : public Decoder { | 
| exp->func_index = consume_func_index(module, &func); | 
| exp->name_offset = consume_string(&exp->name_length, true); | 
| } | 
| + // Check for duplicate exports. | 
| + if (ok() && module->export_table.size() > 1) { | 
| + std::vector<WasmExport> sorted_exports(module->export_table); | 
| 
titzer
2016/06/14 22:52:06
Can't we just do that when creating the exports ob
 
Clemens Hammacher
2016/06/15 07:48:09
We cannot report a validation error at that stage,
 
rossberg
2016/06/15 08:37:16
Yes, validation is required to flag it. You cannot
 | 
| + const byte* base = start_; | 
| + auto cmp = [base](const WasmExport& a, const WasmExport& b) { | 
| + // Return true if a < b. | 
| + uint32_t len = a.name_length; | 
| + if (len != b.name_length) return len < b.name_length; | 
| + return memcmp(base + a.name_offset, base + b.name_offset, len) < | 
| + 0; | 
| + }; | 
| + std::stable_sort(sorted_exports.begin(), sorted_exports.end(), cmp); | 
| 
rossberg
2016/06/15 08:53:58
Instead of sorting the whole thing, would it be fa
 
Clemens Hammacher
2016/06/15 09:20:29
AFAIK sorting and scanning is the fastest way to c
 | 
| + auto it = sorted_exports.begin(); | 
| + WasmExport* last = &*it++; | 
| + for (auto end = sorted_exports.end(); it != end; | 
| + last = &*it, ++it) { | 
| 
rossberg
2016/06/15 08:53:58
Nit: superfluous newline?
 
Clemens Hammacher
2016/06/15 09:20:29
Nope, does not fit :)
But I will change it to "las
 | 
| + DCHECK(!cmp(*it, *last)); | 
| + if (!cmp(*last, *it)) { | 
| + const byte* pc = start_ + it->name_offset; | 
| + error(pc, pc, | 
| + "Duplicate export name '%.*s' for functions %d and %d", | 
| + it->name_length, pc, last->func_index, it->func_index); | 
| + break; | 
| + } | 
| + } | 
| + } | 
| break; | 
| } | 
| case WasmSection::Code::Max: |