diff --git a/src/lol/base/array.h b/src/lol/base/array.h index 9c17f415..33996097 100644 --- a/src/lol/base/array.h +++ b/src/lol/base/array.h @@ -18,6 +18,8 @@ #if !defined __LOL_BASE_ARRAY_H__ #define __LOL_BASE_ARRAY_H__ +#include + #include #include @@ -118,21 +120,29 @@ public: inline Element& operator[](int n) { + /* Allow array[0] even if size is zero so that people can + * always use &array[0] to get a pointer to the data. */ + ASSERT(n >= 0); + ASSERT(n < m_count || (!n && !m_count)); return m_data[n]; } inline Element const& operator[](int n) const { + ASSERT(n >= 0); + ASSERT(n < m_count || (!n && !m_count)); return m_data[n]; } inline Element& Last() { + ASSERT(m_count > 0); return m_data[m_count - 1]; } inline Element const& Last() const { + ASSERT(m_count > 0); return m_data[m_count - 1]; } @@ -158,11 +168,14 @@ public: inline void Pop() { + ASSERT(m_count > 0); Remove(m_count - 1, 1); } void Remove(int pos, int todelete = 1) { + ASSERT(pos >= 0); + ASSERT(pos + todelete <= m_count); for (int i = pos; i + todelete < m_count; i++) m_data[i] = m_data[i + todelete]; for (int i = m_count - todelete; i < m_count; i++) @@ -172,6 +185,7 @@ public: void Resize(int count, Element e = Element()) { + ASSERT(count > 0); Reserve(count); /* Too many elements? Remove them. */ diff --git a/src/lol/base/string.h b/src/lol/base/string.h index 6b15d0c8..1ecbfe88 100644 --- a/src/lol/base/string.h +++ b/src/lol/base/string.h @@ -17,6 +17,7 @@ #if !defined __LOL_BASE_STRING_H__ #define __LOL_BASE_STRING_H__ +#include #include #include @@ -41,6 +42,7 @@ public: : Super() { using namespace std; + ASSERT(str); Resize((int)strlen(str)); memcpy(&(*this)[0], str, Count() + 1); } @@ -49,9 +51,10 @@ public: : Super() { using namespace std; + ASSERT(str); Resize(count); memcpy(&(*this)[0], str, count); - (*this)[count] = '\0'; + ((Super &)*this)[count] = '\0'; } inline String(String const &s) @@ -61,21 +64,27 @@ public: inline char &operator [](int n) { + ASSERT(n >= 0); + ASSERT(n < Count() || (!n && !Count())); return ((Super &)*this)[n]; } inline char const &operator [](int n) const { + ASSERT(n >= 0); + ASSERT(n < Count() || (!n && !Count())); return ((Super const &)*this)[n]; } inline char &Last() { + ASSERT(Count() > 0); return (*this)[Count() - 1]; } inline char const &Last() const { + ASSERT(Count() > 0); return (*this)[Count() - 1]; } @@ -86,6 +95,7 @@ public: void Resize(int count) { + ASSERT(count >= 0, "count = %d", count); ((Super &)*this).Resize(count + 1); ((Super &)*this).Last() = '\0'; }