(svn r12235) [NoAI] -Fix: don't segfault if you do a Next() on an empty list (tnx Progman) noai
authortruebrain
Sun, 24 Feb 2008 22:05:07 +0000
branchnoai
changeset 9752 bd87e54186f2
parent 9751 556f25efbc54
child 9753 7209db94ad12
(svn r12235) [NoAI] -Fix: don't segfault if you do a Next() on an empty list (tnx Progman)
[NoAI] -Fix: warn people if they forget Begin() before Next()/HasNext()
src/ai/api/ai_abstractlist.cpp
src/ai/api/ai_abstractlist.hpp
--- a/src/ai/api/ai_abstractlist.cpp	Sun Feb 24 21:15:12 2008 +0000
+++ b/src/ai/api/ai_abstractlist.cpp	Sun Feb 24 22:05:07 2008 +0000
@@ -3,6 +3,7 @@
 /** @file ai_abstractlist.cpp Implementation of AIAbstractList */
 
 #include "ai_abstractlist.hpp"
+#include "../../debug.h"
 
 /**
  * Base class for any AIAbstractList sorter.
@@ -46,6 +47,7 @@
 	AIAbstractListSorterValueAscending(AIAbstractList *list)
 	{
 		this->list = list;
+		this->bucket_list = NULL;
 	}
 
 	int32 Begin()
@@ -60,7 +62,7 @@
 
 	int32 Next()
 	{
-		if (this->list->buckets.empty()) return 0;
+		if (this->list->buckets.empty() || this->bucket_list == NULL) return 0;
 
 		this->bucket_list_iter++;
 		if (this->bucket_list_iter == this->bucket_list->end()) {
@@ -74,7 +76,7 @@
 
 	bool HasNext()
 	{
-		if (this->list->buckets.empty()) return false;
+		if (this->list->buckets.empty() || this->bucket_list == NULL) return false;
 
 		return this->bucket_iter != this->list->buckets.end() && this->bucket_list_iter != this->bucket_list->end();
 	}
@@ -93,6 +95,7 @@
 	AIAbstractListSorterValueDescending(AIAbstractList *list)
 	{
 		this->list = list;
+		this->bucket_list = NULL;
 	}
 
 	int32 Begin()
@@ -107,7 +110,7 @@
 
 	int32 Next()
 	{
-		if (this->list->buckets.empty()) return 0;
+		if (this->list->buckets.empty() || this->bucket_list == NULL) return 0;
 
 		this->bucket_list_iter++;
 		if (this->bucket_list_iter == this->bucket_list->rend()) {
@@ -121,7 +124,7 @@
 
 	bool HasNext()
 	{
-		if (this->list->buckets.empty()) return false;
+		if (this->list->buckets.empty() || this->bucket_list == NULL) return false;
 
 		return this->bucket_iter != this->list->buckets.rend() && this->bucket_list_iter != this->bucket_list->rend();
 	}
@@ -209,6 +212,7 @@
 	this->sorter         = new AIAbstractListSorterValueDescending(this);
 	this->sorter_type    = SORT_BY_VALUE;
 	this->sort_ascending = false;
+	this->initialized    = false;
 }
 
 AIAbstractList::~AIAbstractList()
@@ -246,11 +250,16 @@
 
 int32 AIAbstractList::Begin()
 {
+	this->initialized = true;
 	return this->sorter->Begin();
 }
 
 int32 AIAbstractList::Next()
 {
+	if (this->initialized == false) {
+		DEBUG(ai, 0, "ERROR: sNext() is invalid as Begin() is never called");
+		return false;
+	}
 	return this->sorter->Next();
 }
 
@@ -261,6 +270,10 @@
 
 bool AIAbstractList::HasNext()
 {
+	if (this->initialized == false) {
+		DEBUG(ai, 0, "ERROR: HasNext() is invalid as Begin() is never called");
+		return false;
+	}
 	return this->sorter->HasNext();
 }
 
--- a/src/ai/api/ai_abstractlist.hpp	Sun Feb 24 21:15:12 2008 +0000
+++ b/src/ai/api/ai_abstractlist.hpp	Sun Feb 24 22:05:07 2008 +0000
@@ -26,6 +26,7 @@
 	AIAbstractListSorter *sorter;
 	SorterType sorter_type;
 	bool sort_ascending;
+	bool initialized;
 
 public:
 	typedef std::set<int32> AIItemList;               ///< The list of items inside the bucket