summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qarraydata.cpp
diff options
context:
space:
mode:
authorUlf Hermann <ulf.hermann@digia.com>2014-09-19 16:12:24 +0200
committerMarc Mutz <marc.mutz@kdab.com>2014-10-17 10:09:18 +0200
commit880986be2357a1f80827d038d770dc2f80300201 (patch)
tree734cf9684d0b7f1cca65fc3e036e30a108d582f6 /src/corelib/tools/qarraydata.cpp
parent9eb2b25300c21df2abd9b174c1077a377a42fcd1 (diff)
Check for integer overflows in places where qAllocMore is used
Task-number: QTBUG-41230 Change-Id: I5e932c2540c0bd67f13fab3ae20975d459f82c08 Reviewed-by: Thiago Macieira <thiago.macieira@intel.com> Reviewed-by: Marc Mutz <marc.mutz@kdab.com>
Diffstat (limited to 'src/corelib/tools/qarraydata.cpp')
-rw-r--r--src/corelib/tools/qarraydata.cpp16
1 files changed, 14 insertions, 2 deletions
diff --git a/src/corelib/tools/qarraydata.cpp b/src/corelib/tools/qarraydata.cpp
index b6f4b3202b..dc419c3758 100644
--- a/src/corelib/tools/qarraydata.cpp
+++ b/src/corelib/tools/qarraydata.cpp
@@ -85,8 +85,20 @@ QArrayData *QArrayData::allocate(size_t objectSize, size_t alignment,
headerSize += (alignment - Q_ALIGNOF(QArrayData));
// Allocate additional space if array is growing
- if (options & Grow)
- capacity = qAllocMore(int(objectSize * capacity), int(headerSize)) / int(objectSize);
+ if (options & Grow) {
+
+ // Guard against integer overflow when multiplying.
+ if (capacity > std::numeric_limits<size_t>::max() / objectSize)
+ return 0;
+
+ size_t alloc = objectSize * capacity;
+
+ // Make sure qAllocMore won't overflow.
+ if (headerSize > size_t(MaxAllocSize) || alloc > size_t(MaxAllocSize) - headerSize)
+ return 0;
+
+ capacity = qAllocMore(int(alloc), int(headerSize)) / int(objectSize);
+ }
size_t allocSize = headerSize + objectSize * capacity;