[llvm] r237942 - YAML: Null terminate block scalar's value.

Alex Lorenz arphaman at gmail.com
Thu May 21 12:45:02 PDT 2015


Author: arphaman
Date: Thu May 21 14:45:02 2015
New Revision: 237942

URL: http://llvm.org/viewvc/llvm-project?rev=237942&view=rev
Log:
YAML: Null terminate block scalar's value.

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. 

Modified:
    llvm/trunk/lib/Support/YAMLParser.cpp
    llvm/trunk/unittests/Support/YAMLParserTest.cpp

Modified: llvm/trunk/lib/Support/YAMLParser.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Support/YAMLParser.cpp?rev=237942&r1=237941&r2=237942&view=diff
==============================================================================
--- llvm/trunk/lib/Support/YAMLParser.cpp (original)
+++ llvm/trunk/lib/Support/YAMLParser.cpp Thu May 21 14:45:02 2015
@@ -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);

Modified: llvm/trunk/unittests/Support/YAMLParserTest.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/unittests/Support/YAMLParserTest.cpp?rev=237942&r1=237941&r2=237942&view=diff
==============================================================================
--- llvm/trunk/unittests/Support/YAMLParserTest.cpp (original)
+++ llvm/trunk/unittests/Support/YAMLParserTest.cpp Thu May 21 14:45:02 2015
@@ -157,6 +157,18 @@ TEST(YAMLParser, ParsesBlockLiteralScala
   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", "[\"   ");





More information about the llvm-commits mailing list