From 82acfbfe86e798e260738f0b25e6b61117f35541 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Fri, 8 Aug 2014 13:58:00 +0000 Subject: [PATCH] Fix bug 20125 - clang-format segfaults on bad config. The problem was in unchecked dyn_cast inside of Input::createHNodes. Patch by Roman Kashitsyn! git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@215205 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/YAMLTraits.cpp | 7 ++++++- unittests/Support/YAMLIOTest.cpp | 7 +++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/Support/YAMLTraits.cpp b/lib/Support/YAMLTraits.cpp index 5212624f0cd..526667fc59e 100644 --- a/lib/Support/YAMLTraits.cpp +++ b/lib/Support/YAMLTraits.cpp @@ -326,7 +326,12 @@ Input::HNode *Input::createHNodes(Node *N) { } else if (MappingNode *Map = dyn_cast(N)) { MapHNode *mapHNode = new MapHNode(N); for (KeyValueNode &KVN : *Map) { - ScalarNode *KeyScalar = dyn_cast(KVN.getKey()); + Node *KeyNode = KVN.getKey(); + ScalarNode *KeyScalar = dyn_cast(KeyNode); + if (!KeyScalar) { + setError(KeyNode, "Map key must be a scalar"); + break; + } StringStorage.clear(); StringRef KeyStr = KeyScalar->getValue(StringStorage); if (!StringStorage.empty()) { diff --git a/unittests/Support/YAMLIOTest.cpp b/unittests/Support/YAMLIOTest.cpp index 8aed98012f1..074e27f8318 100644 --- a/unittests/Support/YAMLIOTest.cpp +++ b/unittests/Support/YAMLIOTest.cpp @@ -84,6 +84,13 @@ TEST(YAMLIO, TestMapRead) { } } +TEST(YAMLIO, TestMalformedMapRead) { + FooBar doc; + Input yin("{foo: 3; bar: 5}", nullptr, suppressErrorMessages); + yin >> doc; + EXPECT_TRUE(!!yin.error()); +} + // // Test the reading of a yaml sequence of mappings // -- 2.34.1