Bug 1258312 - Make Pickle::Resize infallible. r=jld, a=ritu
authorKan-Ru Chen <kanru@kanru.info>
Wed, 30 Mar 2016 11:01:20 +0800
changeset 323937 be11a112d7e9238394d0a6ab4c74c7b81433a9ce
parent 323936 419d8bfa6a79c016b7e596846b8f67f378cd9e4c
child 323938 f33dbaaf6abd2af0dd3a917c880e759aca2a5c12
push id5913
push userjlund@mozilla.com
push dateMon, 25 Apr 2016 16:57:49 +0000
treeherdermozilla-beta@dcaf0a6fa115 [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersjld, ritu
bugs1258312
milestone47.0a2
Bug 1258312 - Make Pickle::Resize infallible. r=jld, a=ritu 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_);
@@ -515,18 +509,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;
@@ -634,26 +629,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
@@ -248,17 +248,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);