Commit c6066999 authored by Chen Bill's avatar Chen Bill Committed by GitHub

bounds checking in effect_set, effect_set_v (#598)

parent 301ae504
...@@ -19,11 +19,12 @@ bool effect_sort_id(const effect* e1, const effect* e2); ...@@ -19,11 +19,12 @@ bool effect_sort_id(const effect* e1, const effect* e2);
struct effect_set { struct effect_set {
void add_item(effect* peffect) { void add_item(effect* peffect) {
if(count >= 64) return; if (count >= 64)
return;
container[count++] = peffect; container[count++] = peffect;
} }
void remove_item(int index) { void remove_item(int index) {
if(index >= count) if (index < 0 || index >= count)
return; return;
if(index == count - 1) { if(index == count - 1) {
--count; --count;
...@@ -45,9 +46,11 @@ struct effect_set { ...@@ -45,9 +46,11 @@ struct effect_set {
std::sort(container.begin(), container.begin() + count, effect_sort_id); std::sort(container.begin(), container.begin() + count, effect_sort_id);
} }
effect* const& get_last() const { effect* const& get_last() const {
assert(count);
return container[count - 1]; return container[count - 1];
} }
effect*& get_last() { effect*& get_last() {
assert(count);
return container[count - 1]; return container[count - 1];
} }
effect* const& operator[] (int index) const { effect* const& operator[] (int index) const {
...@@ -72,7 +75,7 @@ struct effect_set_v { ...@@ -72,7 +75,7 @@ struct effect_set_v {
container.push_back(peffect); container.push_back(peffect);
} }
void remove_item(int index) { void remove_item(int index) {
if(index >= (int)container.size()) if (index < 0 || index >= (int)container.size())
return; return;
container.erase(container.begin() + index); container.erase(container.begin() + index);
} }
...@@ -89,9 +92,11 @@ struct effect_set_v { ...@@ -89,9 +92,11 @@ struct effect_set_v {
std::sort(container.begin(), container.begin() + count, effect_sort_id); std::sort(container.begin(), container.begin() + count, effect_sort_id);
} }
effect* const& get_last() const { effect* const& get_last() const {
assert(container.size());
return container.back(); return container.back();
} }
effect*& get_last() { effect*& get_last() {
assert(container.size());
return container.back(); return container.back();
} }
effect* const& operator[] (int index) const { effect* const& operator[] (int index) const {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment