Bug 1258312 - Make Pickle::Resize infallible r=jld

MozReview-Commit-ID: AfAxXOwOoq1

--HG--
extra : rebase_source : 2abbcda5792d969b7730512b6ea69a99c59b3182
This commit is contained in:
Kan-Ru Chen 2016-03-30 11:01:20 +08:00
parent 313721942c
commit c8fc5be7d5
2 changed files with 8 additions and 16 deletions

View File

@ -147,10 +147,7 @@ Pickle::Pickle(const Pickle& other)
capacity_(0),
variable_buffer_offset_(other.variable_buffer_offset_) {
uint32_t payload_size = header_size_ + other.header_->payload_size;
bool resized = Resize(payload_size);
if (!resized) {
NS_ABORT_OOM(payload_size);
}
Resize(payload_size);
memcpy(header_, other.header_, payload_size);
}
@ -175,10 +172,7 @@ Pickle& Pickle::operator=(const Pickle& other) {
header_ = NULL;
header_size_ = other.header_size_;
}
bool resized = Resize(other.header_size_ + other.header_->payload_size);
if (!resized) {
NS_ABORT_OOM(other.header_size_ + other.header_->payload_size);
}
Resize(other.header_size_ + other.header_->payload_size);
memcpy(header_, other.header_, header_size_ + other.header_->payload_size);
variable_buffer_offset_ = other.variable_buffer_offset_;
return *this;
@ -505,8 +499,9 @@ char* Pickle::BeginWrite(uint32_t length, uint32_t alignment) {
uint32_t new_size = offset + padding + AlignInt(length);
uint32_t needed_size = header_size_ + new_size;
if (needed_size > capacity_ && !Resize(std::max(capacity_ * 2, needed_size)))
return NULL;
if (needed_size > capacity_) {
Resize(std::max(capacity_ * 2, needed_size));
}
DCHECK(intptr_t(header_) % alignment == 0);
@ -616,16 +611,13 @@ void Pickle::TrimWriteData(int new_length) {
*cur_length = new_length;
}
bool Pickle::Resize(uint32_t new_capacity) {
void Pickle::Resize(uint32_t new_capacity) {
new_capacity = ConstantAligner<kPayloadUnit>::align(new_capacity);
void* p = realloc(header_, new_capacity);
if (!p)
return false;
void* p = moz_xrealloc(header_, new_capacity);
header_ = reinterpret_cast<Header*>(p);
capacity_ = new_capacity;
return true;
}
// static

View File

@ -251,7 +251,7 @@ class Pickle {
// the header: new_capacity = sizeof(Header) + desired_payload_capacity.
// A realloc() failure will cause a Resize failure... and caller should check
// the return result for true (i.e., successful resizing).
bool Resize(uint32_t new_capacity);
void Resize(uint32_t new_capacity);
// Round 'bytes' up to the next multiple of 'alignment'. 'alignment' must be
// a power of 2.