changeset 4641:7f807ca63a01

cppcheck: Fix realloc mistake (3 times). realloc may fail and return NULL, in that case the array contents were lost. Dillo would exit a couple of lines later anyway, but it's better to exit in a clean way.
author Jorge Arellano Cid <jcid@dillo.org>
date Di, 12 Jul 2016 14:40:29 -0400
parents cd5c8221c721
children 48aa85639a33
files lout/container.cc lout/misc.cc
diffstat 2 files changed, 14 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- a/lout/container.cc	Di Jul 12 14:33:32 2016 -0400
+++ b/lout/container.cc	Di Jul 12 14:40:29 2016 -0400
@@ -140,7 +140,10 @@
    if (newPos >= numAlloc) {
       while (newPos >= numAlloc)
          numAlloc *= 2;
-      array = (Object**)realloc(array, numAlloc * sizeof(Object*));
+      void *vp;
+      assert((vp = realloc(array, numAlloc * sizeof(Object*))));
+      if (!vp) exit(-2); // when NDEBUG is defined
+      array = (Object**)vp;
    }
 
    // Insert NULL's into possible gap before new position.
@@ -171,10 +174,13 @@
    else {
       numElements++;
 
-      // Allocated memory has to be increased.
       if (numElements >= numAlloc) {
+         // Allocated memory has to be increased.
          numAlloc *= 2;
-         array = (Object**)realloc(array, numAlloc * sizeof(Object*));
+         void *vp;
+         assert((vp = realloc(array, numAlloc * sizeof(Object*))));
+         if (!vp) exit(-2); // when NDEBUG is defined
+         array = (Object**)vp;
       }
 
       for (int i = numElements - 1; i > pos; i--)
--- a/lout/misc.cc	Di Jul 12 14:33:32 2016 -0400
+++ b/lout/misc.cc	Di Jul 12 14:40:29 2016 -0400
@@ -169,8 +169,11 @@
       int newNumBytes = numBytes;
       while (8 * i >= newNumBytes)
          newNumBytes *= 2;
-      bits =
-         (unsigned char*)realloc(bits, newNumBytes * sizeof(unsigned char));
+
+      void *vp;
+      assert((vp = realloc(bits, newNumBytes * sizeof(unsigned char))));
+      if (!vp) exit(-2); // when NDEBUG is defined
+      bits = (unsigned char*)vp;
       memset(bits + numBytes, 0, newNumBytes - numBytes);
       numBytes = newNumBytes;
    }