summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--base/pickle.cc19
-rw-r--r--base/pickle.h6
2 files changed, 15 insertions, 10 deletions
diff --git a/base/pickle.cc b/base/pickle.cc
index 356d5df..0fb1d9c 100644
--- a/base/pickle.cc
+++ b/base/pickle.cc
@@ -309,21 +309,20 @@ char* Pickle::BeginWriteData(int length) {
return data_ptr;
}
-void Pickle::TrimWriteData(int new_length) {
+void Pickle::TrimWriteData(int length) {
DCHECK(variable_buffer_offset_ != 0);
- // Fetch the the variable buffer size
- int* cur_length = reinterpret_cast<int*>(
+ VariableLengthBuffer *buffer = reinterpret_cast<VariableLengthBuffer*>(
reinterpret_cast<char*>(header_) + variable_buffer_offset_);
- if (new_length < 0 || new_length > *cur_length) {
- NOTREACHED() << "Invalid length in TrimWriteData.";
- return;
- }
+ DCHECK_GE(buffer->length, length);
- // Update the payload size and variable buffer size
- header_->payload_size -= (*cur_length - new_length);
- *cur_length = new_length;
+ int old_length = buffer->length;
+ int trimmed_bytes = old_length - length;
+ if (trimmed_bytes > 0) {
+ header_->payload_size -= trimmed_bytes;
+ buffer->length = length;
+ }
}
bool Pickle::Resize(size_t new_capacity) {
diff --git a/base/pickle.h b/base/pickle.h
index da2a485..7c4a007 100644
--- a/base/pickle.h
+++ b/base/pickle.h
@@ -239,6 +239,12 @@ class Pickle {
static const int kPayloadUnit;
private:
+ // A buffer of variable length; used internally
+ struct VariableLengthBuffer {
+ int length;
+ char data; // This is variable length.
+ };
+
Header* header_;
size_t header_size_; // Supports extra data between header and payload.
// Allocation size of payload (or -1 if allocation is const).