diff options
author | jar@chromium.org <jar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-07 23:13:35 +0000 |
---|---|---|
committer | jar@chromium.org <jar@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-08-07 23:13:35 +0000 |
commit | b944f615bc7ae063961a4bcfdc2008aba1fe5472 (patch) | |
tree | 8abccab1489642727433ac0b0d2dc3104581a7fc /base | |
parent | 098be86b419e5ba34ca800e1d5b2b0b0cbedf8d4 (diff) | |
download | chromium_src-b944f615bc7ae063961a4bcfdc2008aba1fe5472.zip chromium_src-b944f615bc7ae063961a4bcfdc2008aba1fe5472.tar.gz chromium_src-b944f615bc7ae063961a4bcfdc2008aba1fe5472.tar.bz2 |
Add short circuit case for self assignment, and add virtual destructor
Pickle is a base class of Message, which has a virtual destructor, so
it is better to have Pickle have a virtual destructor as well.
Add more defensive code in the case of self assignment.
**IF** self assignment was used, and the target was read-only,
then the existing code would have a memory fault.
I don't think this is currently possible in use cases, but
the defensize code is much better. (per suggestion from CPU
on last CL).
BUG=17027
BUG=17088
r=cpu
Review URL: http://codereview.chromium.org/159800
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@22826 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'base')
-rw-r--r-- | base/pickle.cc | 9 | ||||
-rw-r--r-- | base/pickle.h | 2 |
2 files changed, 8 insertions, 3 deletions
diff --git a/base/pickle.cc b/base/pickle.cc index 31bf5b7..0be89c2 100644 --- a/base/pickle.cc +++ b/base/pickle.cc @@ -65,6 +65,10 @@ Pickle::~Pickle() { } Pickle& Pickle::operator=(const Pickle& other) { + if (this == &other) { + NOTREACHED(); + return *this; + } if (capacity_ == kCapacityReadOnly) { header_ = NULL; capacity_ = 0; @@ -74,9 +78,10 @@ Pickle& Pickle::operator=(const Pickle& other) { header_ = NULL; header_size_ = other.header_size_; } - bool resized = Resize(header_size_ + other.header_->payload_size); + bool resized = Resize(other.header_size_ + other.header_->payload_size); CHECK(resized); // Realloc failed. - memcpy(header_, other.header_, header_size_ + other.header_->payload_size); + memcpy(header_, other.header_, + other.header_size_ + other.header_->payload_size); variable_buffer_offset_ = other.variable_buffer_offset_; return *this; } diff --git a/base/pickle.h b/base/pickle.h index 5b70729..9760483 100644 --- a/base/pickle.h +++ b/base/pickle.h @@ -31,7 +31,7 @@ // class Pickle { public: - ~Pickle(); + virtual ~Pickle(); // Initialize a Pickle object using the default header size. Pickle(); |