Skip to content

Commit 3c71a74

Browse files
Merge pull request #551 from mgudemann/update_mp_arith
extend mp_arith bitwise ops with checks for operands sizes/values
2 parents af23092 + d0e572f commit 3c71a74

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

src/util/mp_arith.cpp

+15-8
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ Author: Daniel Kroening, [email protected]
1010

1111
#include <cstdlib>
1212
#include <cctype>
13-
#include <cassert>
1413

1514
#include <sstream>
1615
#include <ostream>
1716
#include <limits>
1817

1918
#include "arith_tools.h"
20-
19+
#include "invariant.h"
2120

2221
typedef BigInt::ullong_t ullong_t; // NOLINT(readability/identifiers)
2322
typedef BigInt::llong_t llong_t; // NOLINT(readability/identifiers)
@@ -192,23 +191,21 @@ const mp_integer binary2integer(const std::string &n, bool is_signed)
192191

193192
mp_integer::ullong_t integer2ulong(const mp_integer &n)
194193
{
195-
assert(n.is_ulong());
194+
PRECONDITION(n.is_ulong());
196195
return n.to_ulong();
197196
}
198197

199198
std::size_t integer2size_t(const mp_integer &n)
200199
{
201-
assert(n>=0);
200+
PRECONDITION(n>=0 && n<=std::numeric_limits<std::size_t>::max());
202201
mp_integer::ullong_t ull=integer2ulong(n);
203-
assert(ull <= std::numeric_limits<std::size_t>::max());
204-
return (std::size_t)ull;
202+
return (std::size_t) ull;
205203
}
206204

207205
unsigned integer2unsigned(const mp_integer &n)
208206
{
209-
assert(n>=0);
207+
PRECONDITION(n>=0 && n<=std::numeric_limits<unsigned>::max());
210208
mp_integer::ullong_t ull=integer2ulong(n);
211-
assert(ull <= std::numeric_limits<unsigned>::max());
212209
return (unsigned)ull;
213210
}
214211

@@ -217,6 +214,7 @@ unsigned integer2unsigned(const mp_integer &n)
217214
/// (currently long long)
218215
mp_integer bitwise_or(const mp_integer &a, const mp_integer &b)
219216
{
217+
PRECONDITION(a.is_ulong() && b.is_ulong());
220218
ullong_t result=a.to_ulong()|b.to_ulong();
221219
return result;
222220
}
@@ -226,6 +224,7 @@ mp_integer bitwise_or(const mp_integer &a, const mp_integer &b)
226224
/// (currently long long)
227225
mp_integer bitwise_and(const mp_integer &a, const mp_integer &b)
228226
{
227+
PRECONDITION(a.is_ulong() && b.is_ulong());
229228
ullong_t result=a.to_ulong()&b.to_ulong();
230229
return result;
231230
}
@@ -235,6 +234,7 @@ mp_integer bitwise_and(const mp_integer &a, const mp_integer &b)
235234
/// (currently long long)
236235
mp_integer bitwise_xor(const mp_integer &a, const mp_integer &b)
237236
{
237+
PRECONDITION(a.is_ulong() && b.is_ulong());
238238
ullong_t result=a.to_ulong()^b.to_ulong();
239239
return result;
240240
}
@@ -244,6 +244,7 @@ mp_integer bitwise_xor(const mp_integer &a, const mp_integer &b)
244244
/// (currently long long)
245245
mp_integer bitwise_neg(const mp_integer &a)
246246
{
247+
PRECONDITION(a.is_ulong());
247248
ullong_t result=~a.to_ulong();
248249
return result;
249250
}
@@ -256,6 +257,7 @@ mp_integer arith_left_shift(
256257
const mp_integer &b,
257258
std::size_t true_size)
258259
{
260+
PRECONDITION(a.is_long() && b.is_ulong());
259261
ullong_t shift=b.to_ulong();
260262
if(shift>true_size && a!=mp_integer(0))
261263
throw "shift value out of range";
@@ -276,6 +278,7 @@ mp_integer arith_right_shift(
276278
const mp_integer &b,
277279
std::size_t true_size)
278280
{
281+
PRECONDITION(a.is_long() && b.is_ulong());
279282
llong_t number=a.to_long();
280283
ullong_t shift=b.to_ulong();
281284
if(shift>true_size)
@@ -295,6 +298,7 @@ mp_integer logic_left_shift(
295298
const mp_integer &b,
296299
std::size_t true_size)
297300
{
301+
PRECONDITION(a.is_long() && b.is_ulong());
298302
ullong_t shift=b.to_ulong();
299303
if(shift>true_size && a!=mp_integer(0))
300304
throw "shift value out of range";
@@ -320,6 +324,7 @@ mp_integer logic_right_shift(
320324
const mp_integer &b,
321325
std::size_t true_size)
322326
{
327+
PRECONDITION(a.is_long() && b.is_ulong());
323328
ullong_t shift=b.to_ulong();
324329
if(shift>true_size)
325330
throw "shift value out of range";
@@ -336,6 +341,7 @@ mp_integer rotate_right(
336341
const mp_integer &b,
337342
std::size_t true_size)
338343
{
344+
PRECONDITION(a.is_ulong() && b.is_ulong());
339345
ullong_t number=a.to_ulong();
340346
ullong_t shift=b.to_ulong();
341347
if(shift>true_size)
@@ -355,6 +361,7 @@ mp_integer rotate_left(
355361
const mp_integer &b,
356362
std::size_t true_size)
357363
{
364+
PRECONDITION(a.is_ulong() && b.is_ulong());
358365
ullong_t number=a.to_ulong();
359366
ullong_t shift=b.to_ulong();
360367
if(shift>true_size)

0 commit comments

Comments
 (0)