summaryrefslogtreecommitdiff
path: root/src/misc
diff options
context:
space:
mode:
authormichi_cc <michi_cc@openttd.org>2011-09-02 20:54:51 +0000
committermichi_cc <michi_cc@openttd.org>2011-09-02 20:54:51 +0000
commitf227e90c248b37fe50c9e48a08a1bb976c782e9f (patch)
tree08d88d27ff581ebc89f0f16acb35d156ad6a84be /src/misc
parent65637d89411e96dee5ee9fc2e8a7b3805c4162a2 (diff)
downloadopenttd-f227e90c248b37fe50c9e48a08a1bb976c782e9f.tar.xz
(svn r22875) -Codechange: Add some asserts and checks to better prevent overflow of the argument to malloc. (monoid)
Diffstat (limited to 'src/misc')
-rw-r--r--src/misc/binaryheap.hpp2
-rw-r--r--src/misc/blob.hpp1
-rw-r--r--src/misc/fixedsizearray.hpp3
3 files changed, 6 insertions, 0 deletions
diff --git a/src/misc/binaryheap.hpp b/src/misc/binaryheap.hpp
index 76f93371e..55730a2ce 100644
--- a/src/misc/binaryheap.hpp
+++ b/src/misc/binaryheap.hpp
@@ -204,6 +204,8 @@ public:
FORCEINLINE void Include(T *new_item)
{
if (this->IsFull()) {
+ assert(this->capacity < UINT_MAX / 2);
+
this->capacity *= 2;
this->data = ReallocT<T*>(this->data, this->capacity + 1);
}
diff --git a/src/misc/blob.hpp b/src/misc/blob.hpp
index bd83904ea..94459a365 100644
--- a/src/misc/blob.hpp
+++ b/src/misc/blob.hpp
@@ -260,6 +260,7 @@ public:
if (Capacity() >= new_size) return;
/* calculate minimum block size we need to allocate
* and ask allocation policy for some reasonable block size */
+ assert(new_size < SIZE_MAX - header_size - tail_reserve);
new_size = AllocPolicy(header_size + new_size + tail_reserve);
/* allocate new block and setup header */
diff --git a/src/misc/fixedsizearray.hpp b/src/misc/fixedsizearray.hpp
index 8b82373fa..a6f4d0c42 100644
--- a/src/misc/fixedsizearray.hpp
+++ b/src/misc/fixedsizearray.hpp
@@ -53,6 +53,9 @@ public:
/** Default constructor. Preallocate space for items and header, then initialize header. */
FixedSizeArray()
{
+ /* Ensure the size won't overflow. */
+ assert_compile(C < (SIZE_MAX - HeaderSize) / Tsize);
+
/* allocate block for header + items (don't construct items) */
data = (T*)((MallocT<byte>(HeaderSize + C * Tsize)) + HeaderSize);
SizeRef() = 0; // initial number of items