diff --git a/llvm/lib/Support/YAMLParser.cpp b/llvm/lib/Support/YAMLParser.cpp --- a/llvm/lib/Support/YAMLParser.cpp +++ b/llvm/lib/Support/YAMLParser.cpp @@ -773,7 +773,7 @@ IsSimpleKeyAllowed = true; Failed = false; std::unique_ptr InputBufferOwner = - MemoryBuffer::getMemBuffer(Buffer); + MemoryBuffer::getMemBuffer(Buffer, /*RequiresNullTerminator=*/false); SM.AddNewSourceBuffer(std::move(InputBufferOwner), SMLoc()); } @@ -1036,7 +1036,7 @@ } void Scanner::skipComment() { - if (*Current != '#') + if (Current == End || *Current != '#') return; while (true) { // This may skip more than one byte, thus Column is only incremented @@ -1051,7 +1051,7 @@ void Scanner::scanToNextToken() { while (true) { - while (*Current == ' ' || *Current == '\t') { + while (Current != End && (*Current == ' ' || *Current == '\t')) { skip(1); } @@ -1286,7 +1286,7 @@ && wasEscaped(Start + 1, Current)); } else { skip(1); - while (true) { + while (Current != End) { // Skip a ' followed by another '. if (Current + 1 < End && *Current == '\'' && *(Current + 1) == '\'') { skip(2); @@ -1334,11 +1334,11 @@ unsigned LeadingBlanks = 0; assert(Indent >= -1 && "Indent must be >= -1 !"); unsigned indent = static_cast(Indent + 1); - while (true) { + while (Current != End) { if (*Current == '#') break; - while (!isBlankOrBreak(Current)) { + while (Current != End && !isBlankOrBreak(Current)) { if ( FlowLevel && *Current == ':' && !(isBlankOrBreak(Current + 1) || *(Current + 1) == ',')) { setError("Found unexpected ':' while scanning a plain scalar", Current); @@ -1407,10 +1407,10 @@ } bool Scanner::scanAliasOrAnchor(bool IsAlias) { - StringRef::iterator Start = Current; - unsigned ColStart = Column; skip(1); - while(true) { + unsigned ColStart = Column; + StringRef::iterator Start = Current; + while(Current != End) { if ( *Current == '[' || *Current == ']' || *Current == '{' || *Current == '}' || *Current == ',' diff --git a/llvm/unittests/Support/YAMLIOTest.cpp b/llvm/unittests/Support/YAMLIOTest.cpp --- a/llvm/unittests/Support/YAMLIOTest.cpp +++ b/llvm/unittests/Support/YAMLIOTest.cpp @@ -3101,3 +3101,80 @@ EXPECT_FALSE(yin2.setCurrentDocument()); EXPECT_TRUE(yin2.error()); } + +TEST(YAMLIO, TestScannerNoNullEmpty) { + std::vector str{}; + Input yin(llvm::StringRef(str.data(), str.size())); + yin.setCurrentDocument(); + EXPECT_FALSE(yin.error()); +} + +TEST(YAMLIO, TestScannerNoNullSequenceOfNull) { + std::vector str{'-'}; + Input yin(llvm::StringRef(str.data(), str.size())); + yin.setCurrentDocument(); + EXPECT_FALSE(yin.error()); +} + +TEST(YAMLIO, TestScannerNoNullSimpleSequence) { + std::vector str{'-', ' ', 'a'}; + Input yin(llvm::StringRef(str.data(), str.size())); + yin.setCurrentDocument(); + EXPECT_FALSE(yin.error()); +} + +TEST(YAMLIO, TestScannerNoNullUnbalancedMap) { + std::vector str{'{'}; + Input yin(llvm::StringRef(str.data(), str.size())); + yin.setCurrentDocument(); + EXPECT_TRUE(yin.error()); +} + +TEST(YAMLIO, TestScannerNoNullEmptyMap) { + std::vector str{'{', '}'}; + Input yin(llvm::StringRef(str.data(), str.size())); + yin.setCurrentDocument(); + EXPECT_FALSE(yin.error()); +} + +TEST(YAMLIO, TestScannerNoNullUnbalancedSequence) { + std::vector str{'['}; + Input yin(llvm::StringRef(str.data(), str.size())); + yin.setCurrentDocument(); + EXPECT_TRUE(yin.error()); +} + +TEST(YAMLIO, TestScannerNoNullEmptySequence) { + std::vector str{'[', ']'}; + Input yin(llvm::StringRef(str.data(), str.size())); + yin.setCurrentDocument(); + EXPECT_FALSE(yin.error()); +} + +TEST(YAMLIO, TestScannerNoNullScalarUnbalancedDoubleQuote) { + std::vector str{'-', ' ', '"'}; + Input yin(llvm::StringRef(str.data(), str.size())); + yin.setCurrentDocument(); + EXPECT_TRUE(yin.error()); +} + +TEST(YAMLIO, TestScannerNoNullScalarUnbalancedSingleQuote) { + std::vector str{'-', ' ', '\''}; + Input yin(llvm::StringRef(str.data(), str.size())); + yin.setCurrentDocument(); + EXPECT_TRUE(yin.error()); +} + +TEST(YAMLIO, TestScannerNoNullEmptyAlias) { + std::vector str{'-', ' ', '&'}; + Input yin(llvm::StringRef(str.data(), str.size())); + yin.setCurrentDocument(); + EXPECT_TRUE(yin.error()); +} + +TEST(YAMLIO, TestScannerNoNullEmptyAnchor) { + std::vector str{'-', ' ', '*'}; + Input yin(llvm::StringRef(str.data(), str.size())); + yin.setCurrentDocument(); + EXPECT_TRUE(yin.error()); +}