diff --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp --- a/llvm/lib/Support/YAMLTraits.cpp +++ b/llvm/lib/Support/YAMLTraits.cpp @@ -435,6 +435,11 @@ // Copy string to permanent storage KeyStr = StringStorage.str().copy(StringAllocator); } + if (mapHNode->Mapping.count(KeyStr)) + // From YAML spec: "The content of a mapping node is an unordered set of + // key/value node pairs, with the restriction that each of the keys is + // unique." + setError(KeyNode, Twine("duplicated mapping key '") + KeyStr); auto ValueHNode = createHNodes(Value); if (EC) break; diff --git a/llvm/test/tools/yaml2obj/map_duplicate_key.test b/llvm/test/tools/yaml2obj/map_duplicate_key.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/yaml2obj/map_duplicate_key.test @@ -0,0 +1,11 @@ +# RUN: not yaml2obj %s 2>&1 | FileCheck %s + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + Type: ET_REL + ABIVersion: 0x05 + +# CHECK: error: duplicated mapping key 'Type diff --git a/llvm/test/tools/yaml2obj/map_duplicate_key_2.test b/llvm/test/tools/yaml2obj/map_duplicate_key_2.test new file mode 100644 --- /dev/null +++ b/llvm/test/tools/yaml2obj/map_duplicate_key_2.test @@ -0,0 +1,15 @@ +# RUN: not yaml2obj %s 2>&1 | FileCheck %s + +--- !ELF +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + ABIVersion: 0x05 +FileHeader: + Class: ELFCLASS64 + Data: ELFDATA2LSB + Type: ET_REL + ABIVersion: 0x05 + +# CHECK: error: duplicated mapping key 'FileHeader