[PATCH] D38082: [yaml2obj] - Don't crash on one more invalid document.

George Rimar via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Sep 20 07:55:24 PDT 2017


grimar created this revision.

This fixes one more crash I faced.
Testcase contains minimal reduced case.

Note: there is also https://reviews.llvm.org/D38039 which refactors whole `createHNodes` method
to make it more readable, it is independent improvement though.


https://reviews.llvm.org/D38082

Files:
  lib/Support/YAMLTraits.cpp
  test/Object/yaml2obj-invalid.yaml


Index: test/Object/yaml2obj-invalid.yaml
===================================================================
--- test/Object/yaml2obj-invalid.yaml
+++ test/Object/yaml2obj-invalid.yaml
@@ -0,0 +1,4 @@
+AAA: | BBB
+
+# RUN: not yaml2obj %s 2>&1 | FileCheck %s
+# CHECK: Map value must not be empty
Index: lib/Support/YAMLTraits.cpp
===================================================================
--- lib/Support/YAMLTraits.cpp
+++ lib/Support/YAMLTraits.cpp
@@ -374,18 +374,22 @@
     auto mapHNode = llvm::make_unique<MapHNode>(N);
     for (KeyValueNode &KVN : *Map) {
       Node *KeyNode = KVN.getKey();
-      ScalarNode *KeyScalar = dyn_cast<ScalarNode>(KeyNode);
-      if (!KeyScalar) {
-        setError(KeyNode, "Map key must be a scalar");
+      ScalarNode *Key = dyn_cast<ScalarNode>(KeyNode);
+      Node *Value = KVN.getValue();
+      if (!Key || !Value) {
+        if (!Key)
+          setError(KeyNode, "Map key must be a scalar");
+        if (!Value)
+          setError(KeyNode, "Map value must not be empty");
         break;
       }
       StringStorage.clear();
-      StringRef KeyStr = KeyScalar->getValue(StringStorage);
+      StringRef KeyStr = Key->getValue(StringStorage);
       if (!StringStorage.empty()) {
         // Copy string to permanent storage
         KeyStr = StringStorage.str().copy(StringAllocator);
       }
-      auto ValueHNode = this->createHNodes(KVN.getValue());
+      auto ValueHNode = this->createHNodes(Key);
       if (EC)
         break;
       mapHNode->Mapping[KeyStr] = std::move(ValueHNode);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D38082.115999.patch
Type: text/x-patch
Size: 1560 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170920/c32b839a/attachment.bin>


More information about the llvm-commits mailing list