From 581908b9e5386c01961e44572fc14376b4db32af Mon Sep 17 00:00:00 2001 From: tamlin-mike Date: Thu, 14 Jul 2016 10:18:07 +0200 Subject: [PATCH] Add safety checks to CPHItemList (+ minor reformatting) --- src/xrPhysics/PHItemList.h | 36 ++++++++++++++++++++---------------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/xrPhysics/PHItemList.h b/src/xrPhysics/PHItemList.h index 7edf8bfe89b..820e971ddcd 100644 --- a/src/xrPhysics/PHItemList.h +++ b/src/xrPhysics/PHItemList.h @@ -1,7 +1,8 @@ #ifndef PH_ITEM_LIST_H #define PH_ITEM_LIST_H +#include "xrCore/xrDebug_macros.h" // for pragma todo. Remove once resolved. /* -#define DECLARE_PHLIST_ITEM(class_name) public:\ +#define DECLARE_PHLIST_ITEM(class_name) public:\ class CPHListItem\ {\ friend class CPHItemList;\ @@ -21,7 +22,7 @@ friend class CPHItemStack; \ u16 stack_pos; -//#define TPI(item) ((T::CPHListItem*)item) +//#define TPI(item) ((T::CPHListItem*)item) template class CPHItemList @@ -33,8 +34,7 @@ class CPHItemList u16 size; public: - class iterator; - typedef class iterator + class iterator { T* my_ptr; @@ -49,24 +49,27 @@ class CPHItemList u16 count() { return size; } void push_back(T* item) { + VERIFY2(size < 65535, "CPHItemList overflow"); *(last_tome) = item; item->tome = last_tome; last_tome = &((item)->next); item->next = 0; size++; } - void move_items(CPHItemList& sourse_list) + void move_items(CPHItemList& source_list) { - if (!sourse_list.first_next) + if (!source_list.first_next) return; - *(last_tome) = sourse_list.first_next; - sourse_list.first_next->tome = last_tome; - last_tome = sourse_list.last_tome; - size = size + sourse_list.size; - sourse_list.empty(); + VERIFY2(size + source_list.size < 65535, "CPHItemList overflow"); + *(last_tome) = source_list.first_next; + source_list.first_next->tome= last_tome; + last_tome = source_list.last_tome; + size += source_list.size; + source_list.empty(); } void erase(iterator i) { + VERIFY2(size != 0, "CPHItemList underflow"); T* item = *i; T* next = item->next; *(item->tome) = next; @@ -96,10 +99,11 @@ class CPHItemStack : public CPHItemList CPHItemList::push_back(item); } }; -#define DEFINE_PHITEM_LIST(T, N, I) \ - typedef CPHItemList N; \ +#define DEFINE_PHITEM_LIST(T, N, I)\ + typedef CPHItemList N;\ typedef CPHItemList::iterator I; -#define DEFINE_PHITEM_STACK(T, N, I) \ - typedef CPHItemStack N; \ +#define DEFINE_PHITEM_STACK(T, N, I)\ + typedef CPHItemStack N;\ typedef CPHItemStack::iterator I; -#endif + +#endif // PH_ITEM_LIST_H