Browse Source

Added assertions to Vector & String indexed access and to shared pointer dereferencing.
Fixed debug build.

Lasse Öörni 14 years ago
parent
commit
e3baa94267

+ 14 - 6
Engine/Container/ArrayPtr.h

@@ -26,6 +26,8 @@
 #include "HashBase.h"
 #include "RefCounted.h"
 
+#include <cassert>
+
 /// Shared array pointer template class. Uses non-intrusive reference counting
 template <class T> class SharedArrayPtr
 {
@@ -95,11 +97,11 @@ public:
     }
     
     /// Point to the array
-    T* operator -> () const { return ptr_; }
+    T* operator -> () const { assert(ptr_); return ptr_; }
     /// Dereference the array
-    T& operator * () const { return *ptr_; }
+    T& operator * () const { assert(ptr_); return *ptr_; }
     /// Subscript the array
-    T& operator [] (const int index) { return ptr_[index]; }
+    T& operator [] (const int index) { assert(ptr_); return ptr_[index]; }
     /// Test for equality with another shared array pointer
     bool operator == (const SharedArrayPtr<T>& rhs) const { return ptr_ == rhs.ptr_; }
     /// Test for inequality with another shared array pointer
@@ -296,19 +298,25 @@ public:
     /// Point to the array
     T* operator -> () const
     {
-        return RawPtr();
+        T* rawPtr = RawPtr();
+        assert(rawPtr);
+        return rawPtr;
     }
     
     /// Dereference the array
     T& operator * () const
     {
-        return *RawPtr();
+        T* rawPtr = RawPtr();
+        assert(rawPtr);
+        return *rawPtr;
     }
     
     /// Subscript the array
     T& operator [] (const int index)
     {
-        return (*RawPtr())[index];
+        T* rawPtr = RawPtr();
+        assert(rawPtr);
+        return (*rawPtr)[index];
     }
     
     /// Test for equality with another weak array pointer

+ 14 - 6
Engine/Container/Ptr.h

@@ -25,6 +25,8 @@
 
 #include "RefCounted.h"
 
+#include <cassert>
+
 /// Shared pointer template class. Can point to an object that derives from RefCounted
 template <class T> class SharedPtr
 {
@@ -88,11 +90,11 @@ public:
     }
     
     /// Point to the object
-    T* operator -> () const { return ptr_; }
+    T* operator -> () const { assert(ptr_); return ptr_; }
     /// Dereference the object
-    T& operator * () const { return *ptr_; }
+    T& operator * () const { assert(ptr_); return *ptr_; }
     /// Subscript the object if applicable
-    T& operator [] (const int index) { return ptr_[index]; }
+    T& operator [] (const int index) { assert(ptr_); return ptr_[index]; }
     /// Test for less than with another shared pointer
     bool operator < (const SharedPtr<T>& rhs) const { return ptr_ < rhs.ptr_; }
     /// Test for equality with another shared pointer
@@ -294,19 +296,25 @@ public:
     /// Point to the object
     T* operator -> () const
     {
-        return RawPtr();
+        T* rawPtr = RawPtr();
+        assert(rawPtr);
+        return rawPtr;
     }
     
     /// Dereference the object
     T& operator * () const
     {
-        return *RawPtr();
+        T* rawPtr = RawPtr();
+        assert(rawPtr);
+        return *rawPtr;
     }
     
     /// Subscript the object if applicable
     T& operator [] (const int index)
     {
-        return (*RawPtr())[index];
+        T* rawPtr = RawPtr();
+        assert(rawPtr);
+        return (*rawPtr)[index];
     }
     
     /// Test for equality with another weak pointer

+ 4 - 4
Engine/Container/StringBase.h

@@ -265,13 +265,13 @@ public:
     }
     
     /// Return char at index
-    char& operator [] (unsigned index) { return buffer_[index]; }
+    char& operator [] (unsigned index) { assert(index < length_); return buffer_[index]; }
     /// Return const char at index
-    const char& operator [] (unsigned index) const { return buffer_[index]; }
+    const char& operator [] (unsigned index) const { assert(index < length_); return buffer_[index]; }
     /// Return char at index
-    char& At(unsigned index) { return buffer_[index]; }
+    char& At(unsigned index) { assert(index < length_); return buffer_[index]; }
     /// Return const char at index
-    const char& At(unsigned index) const { return buffer_[index]; }
+    const char& At(unsigned index) const { assert(index < length_); return buffer_[index]; }
     
     /// Replace all occurrences of a character
     void Replace(char replaceThis, char replaceWith);

+ 9 - 8
Engine/Container/Vector.h

@@ -25,6 +25,7 @@
 
 #include "VectorBase.h"
 
+#include <cassert>
 #include <cstring>
 #include <new>
 
@@ -141,13 +142,13 @@ public:
     }
     
     /// Return element at index
-    T& operator [] (unsigned index) { return Buffer()[index]; }
+    T& operator [] (unsigned index) { assert(index < size_); return Buffer()[index]; }
     /// Return const element at index
-    const T& operator [] (unsigned index) const { return Buffer()[index]; }
+    const T& operator [] (unsigned index) const { assert(index < size_); return Buffer()[index]; }
     /// Return element at index
-    T& At(unsigned index) { return Buffer()[index]; }
+    T& At(unsigned index) { assert(index < size_); return Buffer()[index]; }
     /// Return const element at index
-    const T& At(unsigned index) const { return Buffer()[index]; }
+    const T& At(unsigned index) const { assert(index < size_); return Buffer()[index]; }
 
     /// Add an element at the end
     void Push(const T& value)
@@ -548,13 +549,13 @@ public:
     }
     
     /// Return element at index
-    T& operator [] (unsigned index) { return Buffer()[index]; }
+    T& operator [] (unsigned index) { assert(index < size_); return Buffer()[index]; }
     /// Return const element at index
-    const T& operator [] (unsigned index) const { return Buffer()[index]; }
+    const T& operator [] (unsigned index) const { assert(index < size_); return Buffer()[index]; }
     /// Return element at index
-    T& At(unsigned index) { return Buffer()[index]; }
+    T& At(unsigned index) { assert(index < size_); return Buffer()[index]; }
     /// Return const element at index
-    const T& At(unsigned index) const { return Buffer()[index]; }
+    const T& At(unsigned index) const { assert(index < size_); return Buffer()[index]; }
     
     /// Add an element at the end
     void Push(const T& value)

+ 2 - 1
ThirdParty/kNet/include/kNet/DebugMemoryLeakCheck.h

@@ -23,7 +23,8 @@
 #include <new>
 #include <crtdbg.h>
 
-// include these files beforehand to avoid compilation errors from our operator new redefine.
+// Include these files beforehand to avoid compilation errors from our operator new redefine.
+#include <ios>
 #include "List.h"
 #include "Map.h"
 #include "Set.h"

+ 1 - 1
ThirdParty/kNet/include/kNet/Lockable.h

@@ -22,8 +22,8 @@
 #elif defined(WIN32)
 #include <Windows.h>
 #else
+#include <cassert>
 #include <pthread.h>
-#include <assert.h>
 #endif
 
 #include "PolledTimer.h"