Index: include/llvm/Support/YAMLTraits.h =================================================================== --- include/llvm/Support/YAMLTraits.h +++ include/llvm/Support/YAMLTraits.h @@ -956,7 +956,8 @@ inline typename llvm::enable_if_c::value,Input &>::type operator>>(Input &yin, T &docMap) { - yin.setCurrentDocument(); + if (!yin.setCurrentDocument()) + return yin; yamlize(yin, docMap, true); return yin; } @@ -967,7 +968,8 @@ inline typename llvm::enable_if_c::value,Input &>::type operator>>(Input &yin, T &docSeq) { - yin.setCurrentDocument(); + if (!yin.setCurrentDocument()) + return yin; yamlize(yin, docSeq, true); return yin; } Index: lib/Support/YAMLTraits.cpp =================================================================== --- lib/Support/YAMLTraits.cpp +++ lib/Support/YAMLTraits.cpp @@ -66,6 +66,12 @@ bool Input::setCurrentDocument() { if (DocIterator != Strm->end()) { Node *N = DocIterator->getRoot(); + if (!N) { + assert(Strm->failed() && "Root is NULL iff parsing failed"); + EC = make_error_code(errc::invalid_argument); + return false; + } + if (isa(N)) { // Empty files are allowed and ignored ++DocIterator; Index: unittests/Support/YAMLIOTest.cpp =================================================================== --- unittests/Support/YAMLIOTest.cpp +++ unittests/Support/YAMLIOTest.cpp @@ -58,12 +58,23 @@ // TEST(YAMLIO, TestMapRead) { FooBar doc; - Input yin("---\nfoo: 3\nbar: 5\n...\n"); - yin >> doc; + { + Input yin("---\nfoo: 3\nbar: 5\n...\n"); + yin >> doc; - EXPECT_FALSE(yin.error()); - EXPECT_EQ(doc.foo, 3); - EXPECT_EQ(doc.bar,5); + EXPECT_FALSE(yin.error()); + EXPECT_EQ(doc.foo, 3); + EXPECT_EQ(doc.bar,5); + } + + { + Input yin("{foo: 3, bar: 5}"); + yin >> doc; + + EXPECT_FALSE(yin.error()); + EXPECT_EQ(doc.foo, 3); + EXPECT_EQ(doc.bar,5); + } } @@ -1297,3 +1308,22 @@ EXPECT_TRUE(yin.error()); } +// +// Test error handling with a malformed object +// +TEST(YAMLIO, TestMalformedMap) { + FooBar doc; + { + Input yin("{foo:3, bar: 5}"); + yin.setDiagHandler(suppressErrorMessages); + yin >> doc; + EXPECT_TRUE(yin.error()); + } + + { + Input yin("---\nfoo:3\nbar: 5\n...\n"); + yin.setDiagHandler(suppressErrorMessages); + yin >> doc; + EXPECT_TRUE(yin.error()); + } +}