3
0
Fork 0
mirror of https://github.com/Z3Prover/z3 synced 2025-06-06 06:03:23 +00:00

fix assorted undefined behaviors caught by clang

Signed-off-by: Nuno Lopes <nlopes@microsoft.com>
This commit is contained in:
Nuno Lopes 2015-05-23 11:45:12 +01:00
parent 25a29bf5b0
commit c577ab361b
9 changed files with 81 additions and 21 deletions

View file

@ -316,6 +316,7 @@ func_decl::func_decl(symbol const & name, unsigned arity, sort * const * domain,
decl(AST_FUNC_DECL, name, info), decl(AST_FUNC_DECL, name, info),
m_arity(arity), m_arity(arity),
m_range(range) { m_range(range) {
if (arity != 0)
memcpy(const_cast<sort **>(get_domain()), domain, sizeof(sort *) * arity); memcpy(const_cast<sort **>(get_domain()), domain, sizeof(sort *) * arity);
} }
@ -378,7 +379,9 @@ quantifier::quantifier(bool forall, unsigned num_decls, sort * const * decl_sort
memcpy(const_cast<sort **>(get_decl_sorts()), decl_sorts, sizeof(sort *) * num_decls); memcpy(const_cast<sort **>(get_decl_sorts()), decl_sorts, sizeof(sort *) * num_decls);
memcpy(const_cast<symbol*>(get_decl_names()), decl_names, sizeof(symbol) * num_decls); memcpy(const_cast<symbol*>(get_decl_names()), decl_names, sizeof(symbol) * num_decls);
if (num_patterns != 0)
memcpy(const_cast<expr **>(get_patterns()), patterns, sizeof(expr *) * num_patterns); memcpy(const_cast<expr **>(get_patterns()), patterns, sizeof(expr *) * num_patterns);
if (num_no_patterns != 0)
memcpy(const_cast<expr **>(get_no_patterns()), no_patterns, sizeof(expr *) * num_no_patterns); memcpy(const_cast<expr **>(get_no_patterns()), no_patterns, sizeof(expr *) * num_no_patterns);
} }

View file

@ -542,6 +542,7 @@ namespace polynomial {
increase_capacity(sz * 2); increase_capacity(sz * 2);
SASSERT(sz < m_capacity); SASSERT(sz < m_capacity);
m_ptr->m_size = sz; m_ptr->m_size = sz;
if (sz == 0) return;
memcpy(m_ptr->m_powers, pws, sizeof(power) * sz); memcpy(m_ptr->m_powers, pws, sizeof(power) * sz);
} }

View file

@ -308,6 +308,7 @@ namespace smt {
simple_justification(r, num_lits, lits), simple_justification(r, num_lits, lits),
m_num_eqs(num_eqs) { m_num_eqs(num_eqs) {
m_eqs = new (r) enode_pair[num_eqs]; m_eqs = new (r) enode_pair[num_eqs];
if (num_eqs != 0)
memcpy(m_eqs, eqs, sizeof(enode_pair) * num_eqs); memcpy(m_eqs, eqs, sizeof(enode_pair) * num_eqs);
DEBUG_CODE({ DEBUG_CODE({
for (unsigned i = 0; i < num_eqs; i++) { for (unsigned i = 0; i < num_eqs; i++) {

View file

@ -23,7 +23,7 @@ Revision History:
// I'm using Bob Jenkin's hash function. // I'm using Bob Jenkin's hash function.
// http://burtleburtle.net/bob/hash/doobs.html // http://burtleburtle.net/bob/hash/doobs.html
unsigned string_hash(const char * str, unsigned length, unsigned init_value) { unsigned string_hash(const char * str, unsigned length, unsigned init_value) {
register unsigned a, b, c, len; unsigned a, b, c, len;
/* Set up the internal state */ /* Set up the internal state */
len = length; len = length;

View file

@ -19,9 +19,9 @@ Revision History:
#include<sstream> #include<sstream>
#include"inf_int_rational.h" #include"inf_int_rational.h"
inf_int_rational inf_int_rational::m_zero(0); inf_int_rational inf_int_rational::m_zero;
inf_int_rational inf_int_rational::m_one(1); inf_int_rational inf_int_rational::m_one;
inf_int_rational inf_int_rational::m_minus_one(-1); inf_int_rational inf_int_rational::m_minus_one;
std::string inf_int_rational::to_string() const { std::string inf_int_rational::to_string() const {
if (m_second == 0) { if (m_second == 0) {
@ -39,3 +39,22 @@ std::string inf_int_rational::to_string() const {
return s.str(); return s.str();
} }
void initialize_inf_int_rational() {
inf_int_rational::init();
}
void inf_int_rational::init() {
m_zero.m_first = rational::zero();
m_one.m_first = rational::one();
m_minus_one.m_first = rational::minus_one();
}
void finalize_inf_int_rational() {
inf_int_rational::finalize();
}
void inf_int_rational::finalize() {
m_zero.~inf_int_rational();
m_one.~inf_int_rational();
m_minus_one.~inf_int_rational();
}

View file

@ -33,6 +33,8 @@ class inf_int_rational {
rational m_first; rational m_first;
int m_second; int m_second;
public: public:
static void init(); // called from rational::initialize() only
static void finalize(); // called from rational::finalize() only
unsigned hash() const { unsigned hash() const {
return m_first.hash() ^ (static_cast<unsigned>(m_second) + 1); return m_first.hash() ^ (static_cast<unsigned>(m_second) + 1);
@ -272,7 +274,7 @@ class inf_int_rational {
if (r.m_second >= 0) { if (r.m_second >= 0) {
return r.m_first; return r.m_first;
} }
return r.m_first - rational(1); return r.m_first - rational::one();
} }
return floor(r.m_first); return floor(r.m_first);
@ -283,7 +285,7 @@ class inf_int_rational {
if (r.m_second <= 0) { if (r.m_second <= 0) {
return r.m_first; return r.m_first;
} }
return r.m_first + rational(1); return r.m_first + rational::one();
} }
return ceil(r.m_first); return ceil(r.m_first);

View file

@ -18,9 +18,9 @@ Revision History:
--*/ --*/
#include"inf_rational.h" #include"inf_rational.h"
inf_rational inf_rational::m_zero(0); inf_rational inf_rational::m_zero;
inf_rational inf_rational::m_one(1); inf_rational inf_rational::m_one;
inf_rational inf_rational::m_minus_one(-1); inf_rational inf_rational::m_minus_one;
inf_rational inf_mult(inf_rational const& r1, inf_rational const& r2) inf_rational inf_mult(inf_rational const& r1, inf_rational const& r2)
{ {
@ -128,7 +128,7 @@ inf_rational inf_power(inf_rational const& r, unsigned n)
// 0 will work. // 0 will work.
} }
else if (r.m_first.is_zero()) { else if (r.m_first.is_zero()) {
result.m_first = rational(-1); result.m_first = rational::minus_one();
} }
else if (r.m_first.is_pos()) { else if (r.m_first.is_pos()) {
result.m_first = rational(r.m_first - r.m_first/rational(2)).expt(n); result.m_first = rational(r.m_first - r.m_first/rational(2)).expt(n);
@ -152,7 +152,7 @@ inf_rational sup_power(inf_rational const& r, unsigned n)
result.m_first = r.m_first.expt(n); result.m_first = r.m_first.expt(n);
} }
else if (r.m_first.is_zero() || (n == 0)) { else if (r.m_first.is_zero() || (n == 0)) {
result.m_first = rational(1); result.m_first = rational::one();
} }
else if (r.m_first.is_pos() || is_even) { else if (r.m_first.is_pos() || is_even) {
result.m_first = rational(r.m_first + r.m_first/rational(2)).expt(n); result.m_first = rational(r.m_first + r.m_first/rational(2)).expt(n);
@ -177,3 +177,23 @@ inf_rational sup_root(inf_rational const& r, unsigned n)
// use r. // use r.
return r; return r;
} }
void initialize_inf_rational() {
inf_rational::init();
}
void inf_rational::init() {
m_zero.m_first = rational::zero();
m_one.m_first = rational::one();
m_minus_one.m_first = rational::minus_one();
}
void finalize_inf_rational() {
inf_rational::finalize();
}
void inf_rational::finalize() {
m_zero.~inf_rational();
m_one.~inf_rational();
m_minus_one.~inf_rational();
}

View file

@ -33,6 +33,8 @@ class inf_rational {
rational m_first; rational m_first;
rational m_second; rational m_second;
public: public:
static void init(); // called from rational::initialize() only
static void finalize(); // called from rational::finalize() only
unsigned hash() const { unsigned hash() const {
return m_first.hash() ^ (m_second.hash()+1); return m_first.hash() ^ (m_second.hash()+1);
@ -82,7 +84,7 @@ class inf_rational {
explicit inf_rational(rational const& r, bool pos_inf): explicit inf_rational(rational const& r, bool pos_inf):
m_first(r), m_first(r),
m_second(pos_inf?rational(1):rational(-1)) m_second(pos_inf ? rational::one() : rational::minus_one())
{} {}
inf_rational(rational const& r): inf_rational(rational const& r):
@ -313,7 +315,7 @@ class inf_rational {
if (r.m_second.is_nonneg()) { if (r.m_second.is_nonneg()) {
return r.m_first; return r.m_first;
} }
return r.m_first - rational(1); return r.m_first - rational::one();
} }
return floor(r.m_first); return floor(r.m_first);
@ -324,7 +326,7 @@ class inf_rational {
if (r.m_second.is_nonpos()) { if (r.m_second.is_nonpos()) {
return r.m_first; return r.m_first;
} }
return r.m_first + rational(1); return r.m_first + rational::one();
} }
return ceil(r.m_first); return ceil(r.m_first);

View file

@ -29,9 +29,9 @@ rational rational::m_one;
rational rational::m_minus_one; rational rational::m_minus_one;
vector<rational> rational::m_powers_of_two; vector<rational> rational::m_powers_of_two;
void mk_power_up_to(vector<rational> & pws, unsigned n) { static void mk_power_up_to(vector<rational> & pws, unsigned n) {
if (pws.empty()) { if (pws.empty()) {
pws.push_back(rational(1)); pws.push_back(rational::one());
} }
unsigned sz = pws.size(); unsigned sz = pws.size();
rational curr = pws[sz - 1]; rational curr = pws[sz - 1];
@ -53,16 +53,28 @@ rational rational::power_of_two(unsigned k) {
return result; return result;
} }
// in inf_rational.cpp
void initialize_inf_rational();
void finalize_inf_rational();
// in inf_int_rational.cpp
void initialize_inf_int_rational();
void finalize_inf_int_rational();
void rational::initialize() { void rational::initialize() {
if (!g_mpq_manager) { if (!g_mpq_manager) {
g_mpq_manager = alloc(synch_mpq_manager); g_mpq_manager = alloc(synch_mpq_manager);
m().set(m_zero.m_val, 0); m().set(m_zero.m_val, 0);
m().set(m_one.m_val, 1); m().set(m_one.m_val, 1);
m().set(m_minus_one.m_val, -1); m().set(m_minus_one.m_val, -1);
initialize_inf_rational();
initialize_inf_int_rational();
} }
} }
void rational::finalize() { void rational::finalize() {
finalize_inf_rational();
finalize_inf_int_rational();
m_powers_of_two.finalize(); m_powers_of_two.finalize();
m_zero.~rational(); m_zero.~rational();
m_one.~rational(); m_one.~rational();