Bug 1258312 - Make Pickle::Resize infallible r=jld
authorKan-Ru Chen <kanru@kanru.info>
Wed, 30 Mar 2016 11:01:20 +0800
changeset 291847 17030bff31b48f7cf992f7164d5f9769ba0fa4ef
parent 291846 5b1175e474a7fc65883f2940c80467fc68ce9aa3
child 291848 9f0609cb275d1a0bb2916886e3f5b4c3644d9837
push id74697
push userkchen@mozilla.com
push dateWed, 06 Apr 2016 08:48:51 +0000
treeherdermozilla-inbound@9f0609cb275d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjld
bugs1258312
milestone48.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1258312 - Make Pickle::Resize infallible r=jld MozReview-Commit-ID: AfAxXOwOoq1
ipc/chromium/src/base/pickle.cc
ipc/chromium/src/base/pickle.h
--- a/ipc/chromium/src/base/pickle.cc
+++ b/ipc/chromium/src/base/pickle.cc
@@ -142,20 +142,17 @@ Pickle::Pickle(const char* data, int dat
 }
 
 Pickle::Pickle(const Pickle& other)
     : header_(NULL),
       header_size_(other.header_size_),
       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);
 }
 
 Pickle::Pickle(Pickle&& other)
   : header_(other.header_),
     header_size_(other.header_size_),
     capacity_(other.capacity_),
     variable_buffer_offset_(other.variable_buffer_offset_) {
@@ -170,20 +167,17 @@ Pickle::~Pickle() {
 }
 
 Pickle& Pickle::operator=(const Pickle& other) {
   if (header_size_ != other.header_size_ && capacity_ != kCapacityReadOnly) {
     free(header_);
     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;
 }
 
 Pickle& Pickle::operator=(Pickle&& other) {
   std::swap(header_, other.header_);
   std::swap(header_size_, other.header_size_);
@@ -500,18 +494,19 @@ char* Pickle::BeginWrite(uint32_t length
   DCHECK(alignment % 4 == 0) << "Must be at least 32-bit aligned!";
 
   // write at an alignment-aligned offset from the beginning of the header
   uint32_t offset = AlignInt(header_->payload_size);
   uint32_t padding = (header_size_ + offset) %  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);
 
 #ifdef ARCH_CPU_64_BITS
   DCHECK_LE(length, std::numeric_limits<uint32_t>::max());
 #endif
 
   char* buffer = payload() + offset;
@@ -611,26 +606,23 @@ void Pickle::TrimWriteData(int new_lengt
     return;
   }
 
   // Update the payload size and variable buffer size
   header_->payload_size -= (*cur_length - 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
 const char* Pickle::FindNext(uint32_t header_size,
                              const char* start,
                              const char* end) {
   DCHECK(header_size == AlignInt(header_size));
   DCHECK(header_size <= static_cast<memberAlignmentType>(kPayloadUnit));
--- a/ipc/chromium/src/base/pickle.h
+++ b/ipc/chromium/src/base/pickle.h
@@ -246,17 +246,17 @@ class Pickle {
   // is padded. Should be paired with BeginWrite, but it does not necessarily
   // have to be called after the data is written.
   void EndWrite(char* dest, int length);
 
   // Resize the capacity, note that the input value should include the size of
   // 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.
   template<uint32_t alignment> struct ConstantAligner {
     static uint32_t align(int bytes) {
       static_assert((alignment & (alignment - 1)) == 0,
 			"alignment must be a power of two");
       return (bytes + (alignment - 1)) & ~static_cast<uint32_t>(alignment - 1);