YAML: Null terminate block scalar's value.
authorAlex Lorenz <arphaman@gmail.com>
Thu, 21 May 2015 19:45:02 +0000 (19:45 +0000)
committerAlex Lorenz <arphaman@gmail.com>
Thu, 21 May 2015 19:45:02 +0000 (19:45 +0000)
The commit null terminates the string value in the `yaml::BlockScalarNode`
class.

This change is motivated by the initial MIR serialization commit (r237708)
that I reverted in r237730 because the LLVM IR source from the block
scalar node wasn't terminated by a null character and thus the buildbots
failed on one testcase sometimes. This change enables me to recommit
the reverted commit.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237942 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Support/YAMLParser.cpp
unittests/Support/YAMLParserTest.cpp

index 41d446744fe93dfbcc5a91a812dbfead70b1ea57..d55da5ef1e4a649c8051ca2f5dab314553d3ed21 100644 (file)
@@ -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);
index d3ee8afeb2db2551b1fcee4da8799a70fe3a2513..69b354a91d11e04c226989c71b3ea2269c7e0ccb 100644 (file)
@@ -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", "[\"   ");