Navigation Menu

Skip to content

Commit

Permalink
Check for realloc failure and bad subscripts
Browse files Browse the repository at this point in the history
  • Loading branch information
jesse99 authored and brson committed Dec 16, 2012
1 parent cf1c3d2 commit 6bab226
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/rt/util/array_list.h
Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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];
}

Expand Down

0 comments on commit 6bab226

Please sign in to comment.