Index: src/wasm/module-decoder.cc |
diff --git a/src/wasm/module-decoder.cc b/src/wasm/module-decoder.cc |
index 48f59cefa82bdf1d293da3eb529d4244e7dc2485..63846cc0e19ec5a5f30afc7a548307a0a72ec20d 100644 |
--- a/src/wasm/module-decoder.cc |
+++ b/src/wasm/module-decoder.cc |
@@ -358,6 +358,31 @@ 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); |
+ const byte* base = start_; |
+ auto cmp = [base](const WasmExport& a, const WasmExport& b) { |
ahaas
2016/06/16 09:16:13
I was confused by the name "cmp" because I expecte
Clemens Hammacher
2016/06/16 12:16:10
I renamed it to cmp_less.
|
+ // 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); |
+ auto it = sorted_exports.begin(); |
+ WasmExport* last = &*it++; |
+ for (auto end = sorted_exports.end(); it != end; last = &*it++) { |
+ 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: |