[llvm] 388d679 - Recommit [YAML IO] Check that mapping doesn't contain duplicating keys

Anton Sidorenko via llvm-commits llvm-commits at lists.llvm.org
Mon Feb 13 06:52:17 PST 2023


Author: Anton Sidorenko
Date: 2023-02-13T17:45:07+03:00
New Revision: 388d679c1dfa7c0cb5c9ee6cd0b8592c3c3d09ef

URL: https://github.com/llvm/llvm-project/commit/388d679c1dfa7c0cb5c9ee6cd0b8592c3c3d09ef
DIFF: https://github.com/llvm/llvm-project/commit/388d679c1dfa7c0cb5c9ee6cd0b8592c3c3d09ef.diff

LOG: Recommit [YAML IO] Check that mapping doesn't contain duplicating keys

The revert reason is fixed in D143727 (test changes).

According to YAML specification keys must be unique for a mapping node:
"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".

Differential Revision: https://reviews.llvm.org/D140474

Added: 
    

Modified: 
    llvm/lib/Support/YAMLTraits.cpp
    llvm/unittests/Support/YAMLIOTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/YAMLTraits.cpp b/llvm/lib/Support/YAMLTraits.cpp
index 4eb0b3afd5630..0273059e7c27f 100644
--- a/llvm/lib/Support/YAMLTraits.cpp
+++ b/llvm/lib/Support/YAMLTraits.cpp
@@ -435,6 +435,11 @@ std::unique_ptr<Input::HNode> Input::createHNodes(Node *N) {
         // 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/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp
index f282d23dc500b..40846f52b80b8 100644
--- a/llvm/unittests/Support/YAMLIOTest.cpp
+++ b/llvm/unittests/Support/YAMLIOTest.cpp
@@ -114,6 +114,16 @@ TEST(YAMLIO, TestMalformedMapRead) {
   EXPECT_TRUE(!!yin.error());
 }
 
+TEST(YAMLIO, TestMapDuplicatedKeysRead) {
+  auto testDiagnostic = [](const llvm::SMDiagnostic &Error, void *) {
+    EXPECT_EQ(Error.getMessage(), "duplicated mapping key 'foo'");
+  };
+  FooBar doc;
+  Input yin("{foo: 3, bar: 5, foo: 4}", nullptr, testDiagnostic);
+  yin >> doc;
+  EXPECT_TRUE(!!yin.error());
+}
+
 //
 // Test the reading of a yaml sequence of mappings
 //


        


More information about the llvm-commits mailing list