[llvm] 4c228ee - [YAML IO] Check that mapping doesn't contain duplicating keys
Anton Sidorenko via llvm-commits
llvm-commits at lists.llvm.org
Thu Feb 9 03:46:45 PST 2023
Author: Anton Sidorenko
Date: 2023-02-09T14:45:51+03:00
New Revision: 4c228ee6d40a7cff256f1a680561b6c0155ad704
URL: https://github.com/llvm/llvm-project/commit/4c228ee6d40a7cff256f1a680561b6c0155ad704
DIFF: https://github.com/llvm/llvm-project/commit/4c228ee6d40a7cff256f1a680561b6c0155ad704.diff
LOG: [YAML IO] Check that mapping doesn't contain duplicating keys
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 2ed79cae31edc..fa427f200da20 100644
--- a/llvm/unittests/Support/YAMLIOTest.cpp
+++ b/llvm/unittests/Support/YAMLIOTest.cpp
@@ -105,6 +105,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