From: Alex Lorenz Date: Thu, 21 May 2015 19:45:02 +0000 (+0000) Subject: YAML: Null terminate block scalar's value. X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=802ab073da691f02d1532f7b7393c1795bc88ca6;p=oota-llvm.git 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. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@237942 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/Support/YAMLParser.cpp b/lib/Support/YAMLParser.cpp index 41d446744fe..d55da5ef1e4 100644 --- a/lib/Support/YAMLParser.cpp +++ b/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/unittests/Support/YAMLParserTest.cpp b/unittests/Support/YAMLParserTest.cpp index d3ee8afeb2d..69b354a91d1 100644 --- a/unittests/Support/YAMLParserTest.cpp +++ b/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(Doc.getRoot()); + StringRef Value = + cast(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", "[\" ");