3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-04-23 17:15:31 +00:00

Adding overflow checks

Signed-off-by: Nikolaj Bjorner <nbjorner@microsoft.com>
This commit is contained in:
Nikolaj Bjorner 2013-09-02 19:43:22 -07:00
parent fcc351eba6
commit 878905c13c
4 changed files with 30 additions and 6 deletions

View file

@ -29,6 +29,7 @@ Revision History:
#include<memory.h>
#include"memory_manager.h"
#include"hash.h"
#include"z3_exception.h"
// disable warning for constant 'if' expressions.
// these are used heavily in templates.
@ -67,9 +68,14 @@ class vector {
else {
SASSERT(capacity() > 0);
unsigned old_capacity = reinterpret_cast<unsigned *>(m_data)[CAPACITY_IDX];
unsigned old_capacity_T = sizeof(T) * old_capacity + sizeof(unsigned) * 2;
unsigned new_capacity = (3 * old_capacity + 1) >> 1;
unsigned new_capacity_T = sizeof(T) * new_capacity + sizeof(unsigned) * 2;
unsigned size = reinterpret_cast<unsigned *>(m_data)[SIZE_IDX];
unsigned * mem = reinterpret_cast<unsigned*>(memory::allocate(sizeof(T) * new_capacity + sizeof(unsigned) * 2));
if (new_capacity <= old_capacity || new_capacity_T <= old_capacity_T) {
throw default_exception("Overflow encountered when expanding vector");
}
unsigned * mem = reinterpret_cast<unsigned*>(memory::allocate(new_capacity_T));
*mem = new_capacity;
mem ++;
*mem = size;