diff options
author | wtc@chromium.org <wtc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-28 21:00:03 +0000 |
---|---|---|
committer | wtc@chromium.org <wtc@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-28 21:00:03 +0000 |
commit | e64ff5ef1675d78a38a251eb98dba2e0c8af61b7 (patch) | |
tree | 1805281e1444b202ee9dd3c845766a6d6d731ddd /base | |
parent | b572562659e2b46d99ee2a049e57f5d3dd28e0a7 (diff) | |
download | chromium_src-e64ff5ef1675d78a38a251eb98dba2e0c8af61b7.zip chromium_src-e64ff5ef1675d78a38a251eb98dba2e0c8af61b7.tar.gz chromium_src-e64ff5ef1675d78a38a251eb98dba2e0c8af61b7.tar.bz2 |
WriteData and BeginWriteData should fail if length is
negative.
Add a unit test for WriteData(NULL, 0).
R=darin
BUG=none
TEST=new unit test should pass
Review URL: http://codereview.chromium.org/159310
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21886 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/pickle.cc | 6 | ||||
-rw-r--r-- | base/pickle_unittest.cc | 15 |
2 files changed, 17 insertions, 4 deletions
diff --git a/base/pickle.cc b/base/pickle.cc index c3df8bc..e491832 100644 --- a/base/pickle.cc +++ b/base/pickle.cc @@ -322,15 +322,15 @@ bool Pickle::WriteString16(const string16& value) { } bool Pickle::WriteData(const char* data, int length) { - return WriteInt(length) && WriteBytes(data, length); + return length >= 0 && WriteInt(length) && WriteBytes(data, length); } char* Pickle::BeginWriteData(int length) { DCHECK_EQ(variable_buffer_offset_, 0U) << "There can only be one variable buffer in a Pickle"; - if (!WriteInt(length)) - return false; + if (length < 0 || !WriteInt(length)) + return NULL; char *data_ptr = BeginWrite(length); if (!data_ptr) diff --git a/base/pickle_unittest.cc b/base/pickle_unittest.cc index 1ddbd22..1a98cd2 100644 --- a/base/pickle_unittest.cc +++ b/base/pickle_unittest.cc @@ -13,7 +13,7 @@ namespace { const int testint = 2093847192; -const std::string teststr("Hello world"); // note non-aligned string length +const std::string teststr("Hello world"); // note non-aligned string length const std::wstring testwstr(L"Hello, world"); const char testdata[] = "AAA\0BBB\0"; const int testdatalen = arraysize(testdata) - 1; @@ -246,3 +246,16 @@ TEST(PickleTest, EvilLengths) { EXPECT_FALSE(big_len.ReadWString(&iter, &wstr)); } +// Check we can write zero bytes of data and 'data' can be NULL. +TEST(PickleTest, ZeroLength) { + Pickle pickle; + EXPECT_TRUE(pickle.WriteData(NULL, 0)); + + void* iter = NULL; + const char* outdata; + int outdatalen; + EXPECT_TRUE(pickle.ReadData(&iter, &outdata, &outdatalen)); + EXPECT_EQ(0, outdatalen); + // We can't assert that outdata is NULL. +} + |