[llvm] r215205 - Fix bug 20125 - clang-format segfaults on bad config.

Rafael Espindola rafael.espindola at gmail.com
Fri Aug 8 06:58:00 PDT 2014


Author: rafael
Date: Fri Aug  8 08:58:00 2014
New Revision: 215205

URL: http://llvm.org/viewvc/llvm-project?rev=215205&view=rev
Log:
Fix bug 20125 - clang-format segfaults on bad config.

The problem was in unchecked dyn_cast inside of Input::createHNodes.
Patch by Roman Kashitsyn!

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

Modified: llvm/trunk/lib/Support/YAMLTraits.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/YAMLTraits.cpp?rev=215205&r1=215204&r2=215205&view=diff
==============================================================================
--- llvm/trunk/lib/Support/YAMLTraits.cpp (original)
+++ llvm/trunk/lib/Support/YAMLTraits.cpp Fri Aug  8 08:58:00 2014
@@ -326,7 +326,12 @@ Input::HNode *Input::createHNodes(Node *
   } else if (MappingNode *Map = dyn_cast<MappingNode>(N)) {
     MapHNode *mapHNode = new MapHNode(N);
     for (KeyValueNode &KVN : *Map) {
-      ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KVN.getKey());
+      Node *KeyNode = KVN.getKey();
+      ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
+      if (!KeyScalar) {
+        setError(KeyNode, "Map key must be a scalar");
+        break;
+      }
       StringStorage.clear();
       StringRef KeyStr = KeyScalar->getValue(StringStorage);
       if (!StringStorage.empty()) {

Modified: llvm/trunk/unittests/Support/YAMLIOTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/YAMLIOTest.cpp?rev=215205&r1=215204&r2=215205&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/YAMLIOTest.cpp (original)
+++ llvm/trunk/unittests/Support/YAMLIOTest.cpp Fri Aug  8 08:58:00 2014
@@ -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
 //





More information about the llvm-commits mailing list