summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--llvm/lib/Support/YAMLParser.cpp3
-rw-r--r--llvm/unittests/Support/YAMLParserTest.cpp12
2 files changed, 14 insertions, 1 deletions
diff --git a/llvm/lib/Support/YAMLParser.cpp b/llvm/lib/Support/YAMLParser.cpp
index 41d446744fe..d55da5ef1e4 100644
--- a/llvm/lib/Support/YAMLParser.cpp
+++ b/llvm/lib/Support/YAMLParser.cpp
@@ -2379,7 +2379,8 @@ parse_property:
, T.Range);
case Token::TK_BlockScalar: {
getNext();
- StringRef StrCopy = StringRef(T.Value).copy(NodeAllocator);
+ StringRef NullTerminatedStr(T.Value.c_str(), T.Value.length() + 1);
+ StringRef StrCopy = NullTerminatedStr.copy(NodeAllocator).drop_back();
return new (NodeAllocator)
BlockScalarNode(stream.CurrentDoc, AnchorInfo.Range.substr(1),
TagInfo.Range, StrCopy, T.Range);
diff --git a/llvm/unittests/Support/YAMLParserTest.cpp b/llvm/unittests/Support/YAMLParserTest.cpp
index d3ee8afeb2d..69b354a91d1 100644
--- a/llvm/unittests/Support/YAMLParserTest.cpp
+++ b/llvm/unittests/Support/YAMLParserTest.cpp
@@ -157,6 +157,18 @@ TEST(YAMLParser, ParsesBlockLiteralScalars) {
ExpectParseError("Long leading space line", "test: |\n \n Test\n");
}
+TEST(YAMLParser, NullTerminatedBlockScalars) {
+ SourceMgr SM;
+ yaml::Stream Stream("test: |\n Hello\n World\n", SM);
+ yaml::Document &Doc = *Stream.begin();
+ yaml::MappingNode *Map = cast<yaml::MappingNode>(Doc.getRoot());
+ StringRef Value =
+ cast<yaml::BlockScalarNode>(Map->begin()->getValue())->getValue();
+
+ EXPECT_EQ(Value, "Hello\nWorld\n");
+ EXPECT_EQ(Value.data()[Value.size()], '\0');
+}
+
TEST(YAMLParser, HandlesEndOfFileGracefully) {
ExpectParseError("In string starting with EOF", "[\"");
ExpectParseError("In string hitting EOF", "[\" ");
OpenPOWER on IntegriCloud