[llvm] r288014 - Improve error handling in YAML parsing

Mehdi Amini via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 27 20:44:13 PST 2016


Author: mehdi_amini
Date: Sun Nov 27 22:44:13 2016
New Revision: 288014

URL: http://llvm.org/viewvc/llvm-project?rev=288014&view=rev
Log:
Improve error handling in YAML parsing

Some scanner errors were not checked and reported by the parser.

Fix PR30934

Patch by: Serge Guelton <serge.guelton at telecom-bretagne.eu>

Differential Revision: https://reviews.llvm.org/D26419

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

Modified: llvm/trunk/include/llvm/Support/YAMLTraits.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/Support/YAMLTraits.h?rev=288014&r1=288013&r2=288014&view=diff
==============================================================================
--- llvm/trunk/include/llvm/Support/YAMLTraits.h (original)
+++ llvm/trunk/include/llvm/Support/YAMLTraits.h Sun Nov 27 22:44:13 2016
@@ -1085,6 +1085,7 @@ private:
   void scalarString(StringRef &, bool) override;
   void blockScalarString(StringRef &) override;
   void setError(const Twine &message) override;
+  bool hasError() const;
   bool canElideEmptySequence() override;
 
   class HNode {

Modified: llvm/trunk/lib/Support/YAMLTraits.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/YAMLTraits.cpp?rev=288014&r1=288013&r2=288014&view=diff
==============================================================================
--- llvm/trunk/lib/Support/YAMLTraits.cpp (original)
+++ llvm/trunk/lib/Support/YAMLTraits.cpp Sun Nov 27 22:44:13 2016
@@ -112,7 +112,7 @@ bool Input::mapTag(StringRef Tag, bool D
 }
 
 void Input::beginMapping() {
-  if (EC)
+  if (hasError())
     return;
   // CurrentNode can be null if the document is empty.
   MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
@@ -124,7 +124,7 @@ void Input::beginMapping() {
 bool Input::preflightKey(const char *Key, bool Required, bool, bool &UseDefault,
                          void *&SaveInfo) {
   UseDefault = false;
-  if (EC)
+  if (hasError())
     return false;
 
   // CurrentNode is null for empty documents, which is an error in case required
@@ -159,7 +159,7 @@ void Input::postflightKey(void *saveInfo
 }
 
 void Input::endMapping() {
-  if (EC)
+  if (hasError())
     return;
   // CurrentNode can be null if the document is empty.
   MapHNode *MN = dyn_cast_or_null<MapHNode>(CurrentNode);
@@ -196,7 +196,7 @@ void Input::endSequence() {
 }
 
 bool Input::preflightElement(unsigned Index, void *&SaveInfo) {
-  if (EC)
+  if (hasError())
     return false;
   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
     SaveInfo = CurrentNode;
@@ -213,7 +213,7 @@ void Input::postflightElement(void *Save
 unsigned Input::beginFlowSequence() { return beginSequence(); }
 
 bool Input::preflightFlowElement(unsigned index, void *&SaveInfo) {
-  if (EC)
+  if (hasError())
     return false;
   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
     SaveInfo = CurrentNode;
@@ -271,7 +271,7 @@ bool Input::beginBitSetScalar(bool &DoCl
 }
 
 bool Input::bitSetMatch(const char *Str, bool) {
-  if (EC)
+  if (hasError())
     return false;
   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
     unsigned Index = 0;
@@ -293,7 +293,7 @@ bool Input::bitSetMatch(const char *Str,
 }
 
 void Input::endBitSetScalar() {
-  if (EC)
+  if (hasError())
     return;
   if (SequenceHNode *SQ = dyn_cast<SequenceHNode>(CurrentNode)) {
     assert(BitValuesUsed.size() == SQ->Entries.size());
@@ -321,6 +321,8 @@ void Input::setError(HNode *hnode, const
   this->setError(hnode->_node, message);
 }
 
+bool Input::hasError() const { return EC || Strm->failed(); }
+
 void Input::setError(Node *node, const Twine &message) {
   Strm->printError(node, message);
   EC = make_error_code(errc::invalid_argument);
@@ -342,7 +344,7 @@ std::unique_ptr<Input::HNode> Input::cre
     auto SQHNode = llvm::make_unique<SequenceHNode>(N);
     for (Node &SN : *SQ) {
       auto Entry = this->createHNodes(&SN);
-      if (EC)
+      if (hasError())
         break;
       SQHNode->Entries.push_back(std::move(Entry));
     }
@@ -363,7 +365,7 @@ std::unique_ptr<Input::HNode> Input::cre
         KeyStr = StringStorage.str().copy(StringAllocator);
       }
       auto ValueHNode = this->createHNodes(KVN.getValue());
-      if (EC)
+      if (hasError())
         break;
       mapHNode->Mapping[KeyStr] = std::move(ValueHNode);
     }

Modified: llvm/trunk/unittests/Support/YAMLIOTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/YAMLIOTest.cpp?rev=288014&r1=288013&r2=288014&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/YAMLIOTest.cpp (original)
+++ llvm/trunk/unittests/Support/YAMLIOTest.cpp Sun Nov 27 22:44:13 2016
@@ -2368,3 +2368,11 @@ TEST(YAMLIO, TestMapWithContext) {
             out);
   out.clear();
 }
+
+TEST(YAMLIO, InvalidInput) {
+  // polluting 1 value in the sequence
+  Input yin("---\n- foo:  3\n  bar:  5\n1\n- foo:  3\n  bar:  5\n...\n");
+  std::vector<FooBar> Data;
+  yin >> Data;
+  EXPECT_TRUE((bool)yin.error());
+}




More information about the llvm-commits mailing list