Bug 1185115 - Ensure we have enough memory to allocate required capacity. r=kentuckyfriedtakahe, a=lmandel
authorJean-Yves Avenard <jyavenard@mozilla.com>
Mon, 27 Jul 2015 11:27:34 -0400
changeset 275477 535b6bb28d8008b6d890758b9f4d9bea652d8be4
parent 275476 4a07d8bd97c8bf7dc0681534ca39a045197e8837
child 275478 df25d99a85a2430b8b95bfdfb4e57f1cd3bee0e1
push id863
push userraliiev@mozilla.com
push dateMon, 03 Aug 2015 13:22:43 +0000
treeherdermozilla-release@f6321b14228d [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewerskentuckyfriedtakahe, lmandel
bugs1185115
milestone40.0
Bug 1185115 - Ensure we have enough memory to allocate required capacity. r=kentuckyfriedtakahe, a=lmandel
media/libstagefright/system/core/libutils/VectorImpl.cpp
--- a/media/libstagefright/system/core/libutils/VectorImpl.cpp
+++ b/media/libstagefright/system/core/libutils/VectorImpl.cpp
@@ -11,26 +11,31 @@
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 
 #define LOG_TAG "Vector"
 
+#include <limits.h>
 #include <string.h>
 #include <stdlib.h>
 #include <stdio.h>
 
 #include <cutils/log.h>
 
 #include <utils/Errors.h>
 #include <utils/SharedBuffer.h>
 #include <utils/VectorImpl.h>
 
+#if !defined(SSIZE_MAX)
+#define SSIZE_MAX ((ssize_t)(SIZE_MAX/2))
+#endif
+
 /*****************************************************************************/
 
 
 namespace stagefright {
 
 // ----------------------------------------------------------------------------
 
 const size_t kMinVectorCapacity = 4;
@@ -320,22 +325,23 @@ const void* VectorImpl::itemLocation(siz
             return reinterpret_cast<const char*>(buffer) + index*mItemSize;
         }
     }
     return 0;
 }
 
 ssize_t VectorImpl::setCapacity(size_t new_capacity)
 {
-    size_t current_capacity = capacity();
-    ssize_t amount = new_capacity - size();
-    if (amount <= 0) {
+    if (new_capacity <= size()) {
         // we can't reduce the capacity
-        return current_capacity;
-    } 
+        return capacity();
+    }
+    if (new_capacity >= (SSIZE_MAX / mItemSize)) {
+        return NO_MEMORY;
+    }
     SharedBuffer* sb = SharedBuffer::alloc(new_capacity * mItemSize);
     if (sb) {
         void* array = sb->data();
         _do_copy(array, mStorage, size());
         release_storage();
         mStorage = const_cast<void*>(array);
     } else {
         return NO_MEMORY;