diff options
author | Jorge Arellano Cid <jcid@dillo.org> | 2016-07-12 14:40:29 -0400 |
---|---|---|
committer | Jorge Arellano Cid <jcid@dillo.org> | 2016-07-12 14:40:29 -0400 |
commit | 0856f155988da7dfc10eee25157525466ab32f20 (patch) | |
tree | c3faec980753c032e10ef8d4d6403239a512f53a | |
parent | f487829c3f7edf56e666d0be79a979b6af46a9bc (diff) |
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.
-rw-r--r-- | lout/container.cc | 12 | ||||
-rw-r--r-- | lout/misc.cc | 7 |
2 files changed, 14 insertions, 5 deletions
diff --git a/lout/container.cc b/lout/container.cc index 366a58fa..48b353a6 100644 --- a/lout/container.cc +++ b/lout/container.cc @@ -140,7 +140,10 @@ void Vector::put(Object *newElement, int newPos) 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 @@ void Vector::insert(Object *newElement, int pos) 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--) diff --git a/lout/misc.cc b/lout/misc.cc index 9b333c93..bffc68f7 100644 --- a/lout/misc.cc +++ b/lout/misc.cc @@ -169,8 +169,11 @@ void BitSet::set(int i, bool val) 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; } |