OSDN Git Service

assume malloc() does not fail in QVarLengthArray<T>::reallocData()
authorIvailo Monev <xakepa10@gmail.com>
Tue, 30 May 2023 06:09:51 +0000 (09:09 +0300)
committerIvailo Monev <xakepa10@gmail.com>
Tue, 30 May 2023 06:09:51 +0000 (09:09 +0300)
even when malloc() is called with 0 as size it returns non-null pointer
(some random garbage)

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
src/core/tools/qvarlengtharray.h

index 92da592..c0eca94 100644 (file)
@@ -206,23 +206,18 @@ Q_OUTOFLINE_TEMPLATE void QVarLengthArray<T, Prealloc>::reallocData(int asize, i
     if (aalloc != a) {
         ptr = static_cast<T *>(malloc(aalloc * sizeof(T)));
         Q_CHECK_PTR(ptr);
-        if (ptr) {
-            s = 0;
-            a = aalloc;
-
-            if (QTypeInfo<T>::isStatic) {
-                // copy all the old elements
-                while (s < copySize) {
-                    new (ptr+s) T(*(oldPtr+s));
-                    (oldPtr+s)->~T();
-                    s++;
-                }
-            } else {
-                memcpy(ptr, oldPtr, copySize * sizeof(T));
+        s = 0;
+        a = aalloc;
+
+        if (QTypeInfo<T>::isStatic) {
+            // copy all the old elements
+            while (s < copySize) {
+                new (ptr+s) T(*(oldPtr+s));
+                (oldPtr+s)->~T();
+                s++;
             }
         } else {
-            ptr = oldPtr;
-            return;
+            memcpy(ptr, oldPtr, copySize * sizeof(T));
         }
     }
     s = copySize;