From 6bab226fc59f5bd011e243b5a4ec18adcbefdd58 Mon Sep 17 00:00:00 2001 From: Jesse Jones Date: Sat, 8 Dec 2012 21:34:26 -0800 Subject: [PATCH] Check for realloc failure and bad subscripts --- src/rt/util/array_list.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/rt/util/array_list.h b/src/rt/util/array_list.h index a62a8c5c8db71..fed22f8d03602 100644 --- a/src/rt/util/array_list.h +++ b/src/rt/util/array_list.h @@ -14,6 +14,7 @@ #include #include +#include /** * A simple, resizable array list. Note that this only works with POD types @@ -69,8 +70,12 @@ array_list::append(T value) { template int32_t array_list::push(T value) { if (_size == _capacity) { - _capacity = _capacity * 2; - _data = (T *) realloc(_data, _capacity * sizeof(T)); + size_t new_capacity = _capacity * 2; + void* buffer = realloc(_data, new_capacity * sizeof(T)); + if (buffer == NULL) + throw std::bad_alloc(); + _data = (T *) buffer; + _capacity = new_capacity; } _data[_size ++] = value; return _size - 1; @@ -115,11 +120,13 @@ array_list::index_of(T value) const { template T & array_list::operator[](size_t index) { + assert(index < size()); return _data[index]; } template const T & array_list::operator[](size_t index) const { + assert(index < size()); return _data[index]; }