summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorfrosch <frosch@openttd.org>2011-09-03 12:45:45 +0000
committerfrosch <frosch@openttd.org>2011-09-03 12:45:45 +0000
commit5ccd091fd1bdf9a92b2424bba2dd63b372df0639 (patch)
tree88e87ee24d5ee0cb11b5783b1378dbaee75f9caa /src
parent275a394bc1a83898e31ea2c30b19b1a9a37c2bc5 (diff)
downloadopenttd-5ccd091fd1bdf9a92b2424bba2dd63b372df0639.tar.xz
(svn r22881) -Fix (r22875): GCC warnings on 64bit systems.
Diffstat (limited to 'src')
-rw-r--r--src/core/alloc_func.hpp29
-rw-r--r--src/pathfinder/npf/queue.cpp2
-rw-r--r--src/stdafx.h1
3 files changed, 28 insertions, 4 deletions
diff --git a/src/core/alloc_func.hpp b/src/core/alloc_func.hpp
index 7bd09b004..4ba8c5f53 100644
--- a/src/core/alloc_func.hpp
+++ b/src/core/alloc_func.hpp
@@ -23,6 +23,29 @@ void NORETURN MallocError(size_t size);
void NORETURN ReallocError(size_t size);
/**
+ * Checks whether allocating memory would overflow size_t.
+ *
+ * @param element_size Size of the structure to allocate.
+ * @param num_elements Number of elements to allocate.
+ */
+static inline void CheckAllocationConstraints(size_t element_size, size_t num_elements)
+{
+ if (num_elements > SIZE_MAX / element_size) MallocError(SIZE_MAX);
+}
+
+/**
+ * Checks whether allocating memory would overflow size_t.
+ *
+ * @tparam T Structure to allocate.
+ * @param num_elements Number of elements to allocate.
+ */
+template <typename T>
+static inline void CheckAllocationConstraints(size_t num_elements)
+{
+ CheckAllocationConstraints(sizeof(T), num_elements);
+}
+
+/**
* Simplified allocation function that allocates the specified number of
* elements of the given type. It also explicitly casts it to the requested
* type.
@@ -43,7 +66,7 @@ static FORCEINLINE T *MallocT(size_t num_elements)
if (num_elements == 0) return NULL;
/* Ensure the size does not overflow. */
- if (num_elements > SIZE_MAX / sizeof(T)) MallocError(SIZE_MAX);
+ CheckAllocationConstraints<T>(num_elements);
T *t_ptr = (T*)malloc(num_elements * sizeof(T));
if (t_ptr == NULL) MallocError(num_elements * sizeof(T));
@@ -100,7 +123,7 @@ static FORCEINLINE T *ReallocT(T *t_ptr, size_t num_elements)
}
/* Ensure the size does not overflow. */
- if (num_elements > SIZE_MAX / sizeof(T)) MallocError(SIZE_MAX);
+ CheckAllocationConstraints<T>(num_elements);
t_ptr = (T*)realloc(t_ptr, num_elements * sizeof(T));
if (t_ptr == NULL) ReallocError(num_elements * sizeof(T));
@@ -109,7 +132,7 @@ static FORCEINLINE T *ReallocT(T *t_ptr, size_t num_elements)
/** alloca() has to be called in the parent function, so define AllocaM() as a macro */
#define AllocaM(T, num_elements) \
- ((num_elements) > SIZE_MAX / sizeof(T) && (MallocError(SIZE_MAX), NULL), \
+ (CheckAllocationConstraints<T>(num_elements), \
(T*)alloca((num_elements) * sizeof(T)))
#endif /* ALLOC_FUNC_HPP */
diff --git a/src/pathfinder/npf/queue.cpp b/src/pathfinder/npf/queue.cpp
index b12c59d6c..f964ece24 100644
--- a/src/pathfinder/npf/queue.cpp
+++ b/src/pathfinder/npf/queue.cpp
@@ -235,7 +235,7 @@ void Hash::Init(Hash_HashProc *hash, uint num_buckets)
uint i;
/* Ensure the size won't overflow. */
- assert(num_buckets < SIZE_MAX / (sizeof(*this->buckets) + sizeof(*this->buckets_in_use)));
+ CheckAllocationConstraints(sizeof(*this->buckets) + sizeof(*this->buckets_in_use), num_buckets);
this->hash = hash;
this->size = 0;
diff --git a/src/stdafx.h b/src/stdafx.h
index c50a93eba..bee855169 100644
--- a/src/stdafx.h
+++ b/src/stdafx.h
@@ -339,6 +339,7 @@ assert_compile(sizeof(uint64) == 8);
assert_compile(sizeof(uint32) == 4);
assert_compile(sizeof(uint16) == 2);
assert_compile(sizeof(uint8) == 1);
+assert_compile(SIZE_MAX >= UINT32_MAX);
#ifndef M_PI_2
#define M_PI_2 1.57079632679489661923