diff options
author | cbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-18 15:44:59 +0000 |
---|---|---|
committer | cbentzel@chromium.org <cbentzel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-10-18 15:44:59 +0000 |
commit | 11fe41a6b2cb4229f8ef3ff0bc22fd609fe838e4 (patch) | |
tree | 4d96de4a5d645b319425b2864f4d5700e39c0642 /base/string_util_unittest.cc | |
parent | a2d67740c562caea523f3650c063d75ee4c0a8ac (diff) | |
download | chromium_src-11fe41a6b2cb4229f8ef3ff0bc22fd609fe838e4.zip chromium_src-11fe41a6b2cb4229f8ef3ff0bc22fd609fe838e4.tar.gz chromium_src-11fe41a6b2cb4229f8ef3ff0bc22fd609fe838e4.tar.bz2 |
WriteInto handles length_with_null values of 1 better.
In debug builds on VS2010, the old code triggered a debug assertion in
xstring, since [0] was being accessed and the size of the array was 0.
A |length_with_null| of 1 is considered a valid use of this function, so
an alternate implementation is used which returns the data() pointer.
BUG=None
TEST=net_unittests --gtest_filter=*Digest* on debug on VS2010 works.
Review URL: http://codereview.chromium.org/8143001
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@106062 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base/string_util_unittest.cc')
-rw-r--r-- | base/string_util_unittest.cc | 42 |
1 files changed, 42 insertions, 0 deletions
diff --git a/base/string_util_unittest.cc b/base/string_util_unittest.cc index d449dee..f354a99 100644 --- a/base/string_util_unittest.cc +++ b/base/string_util_unittest.cc @@ -1079,4 +1079,46 @@ TEST(StringUtilTest, ContainsOnlyChars) { EXPECT_FALSE(ContainsOnlyChars("123a", "4321")); } +TEST(StringUtilTest, WriteInto) { + // Validate that WriteInto reserves enough space and + // sizes a string correctly. + std::string buffer; + const char kOriginal[] = "supercali"; + strncpy(WriteInto(&buffer, 1), kOriginal, 0); + EXPECT_STREQ("", buffer.c_str()); + EXPECT_EQ(0u, buffer.size()); + strncpy(WriteInto(&buffer, 2), kOriginal, 1); + EXPECT_STREQ("s", buffer.c_str()); + EXPECT_EQ(1u, buffer.size()); + strncpy(WriteInto(&buffer, 3), kOriginal, 2); + EXPECT_STREQ("su", buffer.c_str()); + EXPECT_EQ(2u, buffer.size()); + strncpy(WriteInto(&buffer, 5001), kOriginal, 5000); + EXPECT_STREQ("supercali", buffer.c_str()); + EXPECT_EQ(5000u, buffer.size()); + strncpy(WriteInto(&buffer, 3), kOriginal, 2); + EXPECT_STREQ("su", buffer.c_str()); + EXPECT_EQ(2u, buffer.size()); + strncpy(WriteInto(&buffer, 1), kOriginal, 0); + EXPECT_STREQ("", buffer.c_str()); + EXPECT_EQ(0u, buffer.size()); + + // Validate that WriteInto returns NULL only when + // |length_with_null| == 1. + EXPECT_TRUE(WriteInto(&buffer, 1) == NULL); + EXPECT_TRUE(WriteInto(&buffer, 2) != NULL); + + // Validate that WriteInto doesn't modify other strings + // when using a Copy-on-Write implementation. + const char kLive[] = "live"; + const char kDead[] = "dead"; + const std::string live = kLive; + std::string dead = live; + strncpy(WriteInto(&dead, 5), kDead, 4); + EXPECT_STREQ(kDead, dead.c_str()); + EXPECT_EQ(4u, dead.size()); + EXPECT_STREQ(kLive, live.c_str()); + EXPECT_EQ(4u, live.size()); +} + } // namespace base |