diff options
-rw-r--r-- | src/wasm/wasm-binary.cpp | 17 | ||||
-rw-r--r-- | test/duplicated_names.wasm | bin | 0 -> 64 bytes | |||
-rw-r--r-- | test/duplicated_names.wasm.fromBinary | 13 | ||||
-rw-r--r-- | test/duplicated_names_collision.wasm | bin | 0 -> 66 bytes | |||
-rw-r--r-- | test/duplicated_names_collision.wasm.fromBinary | 13 |
5 files changed, 35 insertions, 8 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index 188c2bc17..0fed49729 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -2192,9 +2192,15 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) { continue; } auto num = getU32LEB(); + std::set<Name> usedNames; for (size_t i = 0; i < num; i++) { auto index = getU32LEB(); - auto name = getInlineString(); + auto rawName = getInlineString(); + auto name = rawName; + // De-duplicate names by appending .1, .2, etc. + for (int i = 1; !usedNames.insert(name).second; ++i) { + name = rawName.str + std::string(".") + std::to_string(i); + } // note: we silently ignore errors here, as name section errors // are not fatal. should we warn? auto numFunctionImports = functionImports.size(); @@ -2202,13 +2208,8 @@ void WasmBinaryBuilder::readNames(size_t payloadLen) { functionImports[index]->name = name; } else if (index - numFunctionImports < functions.size()) { functions[index - numFunctionImports]->name = name; - } - } - // disallow duplicate names - std::set<Name> functionNames; - for (auto* func : functions) { - if (!functionNames.insert(func->name).second) { - throw ParseException("duplicate function name: " + std::string(func->name.str)); + } else { + throw ParseException("index out of bounds: " + std::string(name.str)); } } if (pos != subsectionPos + subsectionSize) { diff --git a/test/duplicated_names.wasm b/test/duplicated_names.wasm Binary files differnew file mode 100644 index 000000000..33dfb229f --- /dev/null +++ b/test/duplicated_names.wasm diff --git a/test/duplicated_names.wasm.fromBinary b/test/duplicated_names.wasm.fromBinary new file mode 100644 index 000000000..90eedbaa5 --- /dev/null +++ b/test/duplicated_names.wasm.fromBinary @@ -0,0 +1,13 @@ +(module + (type $0 (func (result i32))) + (func $foo (; 0 ;) (type $0) (result i32) + (i32.const 0) + ) + (func $foo.1 (; 1 ;) (type $0) (result i32) + (i32.const 1) + ) + (func $foo.2 (; 2 ;) (type $0) (result i32) + (i32.const 2) + ) +) + diff --git a/test/duplicated_names_collision.wasm b/test/duplicated_names_collision.wasm Binary files differnew file mode 100644 index 000000000..b742ce07a --- /dev/null +++ b/test/duplicated_names_collision.wasm diff --git a/test/duplicated_names_collision.wasm.fromBinary b/test/duplicated_names_collision.wasm.fromBinary new file mode 100644 index 000000000..a3ab6a1aa --- /dev/null +++ b/test/duplicated_names_collision.wasm.fromBinary @@ -0,0 +1,13 @@ +(module + (type $0 (func (result i32))) + (func $foo (; 0 ;) (type $0) (result i32) + (i32.const 0) + ) + (func $foo.1 (; 1 ;) (type $0) (result i32) + (i32.const 1) + ) + (func $foo.1.1 (; 2 ;) (type $0) (result i32) + (i32.const 2) + ) +) + |