From 00cc3caa4d0735698ba373b519b10641ffac8bf5 Mon Sep 17 00:00:00 2001 From: George Rimar Date: Thu, 21 Sep 2017 08:25:59 +0000 Subject: [PATCH] [yaml2obj] - Don't crash on one more invalid document. This fixes one more crash I faced. Testcase contains minimal reduced case. Differential revision: https://reviews.llvm.org/D38082 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@313868 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Support/YAMLTraits.cpp | 14 +++++++++----- test/Object/yaml2obj-invalid.yaml | 4 ++++ 2 files changed, 13 insertions(+), 5 deletions(-) create mode 100644 test/Object/yaml2obj-invalid.yaml diff --git a/lib/Support/YAMLTraits.cpp b/lib/Support/YAMLTraits.cpp index 65eda246a7f..75a2224a772 100644 --- a/lib/Support/YAMLTraits.cpp +++ b/lib/Support/YAMLTraits.cpp @@ -374,18 +374,22 @@ std::unique_ptr Input::createHNodes(Node *N) { auto mapHNode = llvm::make_unique(N); for (KeyValueNode &KVN : *Map) { Node *KeyNode = KVN.getKey(); - ScalarNode *KeyScalar = dyn_cast(KeyNode); - if (!KeyScalar) { - setError(KeyNode, "Map key must be a scalar"); + ScalarNode *Key = dyn_cast(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(Value); if (EC) break; mapHNode->Mapping[KeyStr] = std::move(ValueHNode); diff --git a/test/Object/yaml2obj-invalid.yaml b/test/Object/yaml2obj-invalid.yaml new file mode 100644 index 00000000000..fba4ad81407 --- /dev/null +++ b/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 -- 2.50.0