diff options
| author | Jesse Jones <jesse9jones@gmail.com> | 2012-12-08 21:34:26 -0800 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-12-15 22:38:20 -0800 |
| commit | 6bab226fc59f5bd011e243b5a4ec18adcbefdd58 (patch) | |
| tree | 9e28e62ca2e021fda21dede60b8fa90d114c1529 /src/rt | |
| parent | cf1c3d2da0cb6ebc4c34c122cde3a76523417ed4 (diff) | |
| download | rust-6bab226fc59f5bd011e243b5a4ec18adcbefdd58.tar.gz rust-6bab226fc59f5bd011e243b5a4ec18adcbefdd58.zip | |
Check for realloc failure and bad subscripts
Diffstat (limited to 'src/rt')
| -rw-r--r-- | src/rt/util/array_list.h | 11 |
1 files changed, 9 insertions, 2 deletions
diff --git a/src/rt/util/array_list.h b/src/rt/util/array_list.h index a62a8c5c8db..fed22f8d036 100644 --- a/src/rt/util/array_list.h +++ b/src/rt/util/array_list.h @@ -14,6 +14,7 @@ #include <inttypes.h> #include <stddef.h> +#include <new> /** * A simple, resizable array list. Note that this only works with POD types @@ -69,8 +70,12 @@ array_list<T>::append(T value) { template<typename T> int32_t array_list<T>::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<T>::index_of(T value) const { template<typename T> T & array_list<T>::operator[](size_t index) { + assert(index < size()); return _data[index]; } template<typename T> const T & array_list<T>::operator[](size_t index) const { + assert(index < size()); return _data[index]; } |
