Skip to content

Commit 448ff24

Browse files
author
Thomas Kiley
committed
Use mp_integer for calculation
This removes the possibility for the calculation itself to overflow
1 parent 12d94f1 commit 448ff24

File tree

2 files changed

+6
-7
lines changed

2 files changed

+6
-7
lines changed

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,6 @@ static std::string architecture_string(T value, const char *s)
122122
std::string(s) + "=" + std::to_string(value) + ";\n";
123123
}
124124

125-
using max_alloc_sizet = uint64_t;
126125
/// The maximum allocation size is determined by the number of bits that
127126
/// are left in the pointer of width \p pointer_width.
128127
///
@@ -137,7 +136,7 @@ using max_alloc_sizet = uint64_t;
137136
/// \param pointer_width: The width of the pointer
138137
/// \param object_bits : The number of bits used to represent the ID
139138
/// \return The size in bytes of the maximum allocation supported.
140-
static max_alloc_sizet
139+
static mp_integer
141140
max_malloc_size(std::size_t pointer_width, std::size_t object_bits)
142141
{
143142
PRECONDITION(pointer_width >= 1);
@@ -148,9 +147,7 @@ max_malloc_size(std::size_t pointer_width, std::size_t object_bits)
148147
// but also down to -allocation_size, therefore the size is allowable
149148
// is number of bits, less the signed bit.
150149
const auto bits_for_positive_offset = offset_bits - 1;
151-
PRECONDITION(
152-
bits_for_positive_offset < std::numeric_limits<max_alloc_sizet>::digits);
153-
return ((max_alloc_sizet)1) << (max_alloc_sizet)bits_for_positive_offset;
150+
return ((mp_integer)1) << (mp_integer)bits_for_positive_offset;
154151
}
155152

156153
void ansi_c_internal_additions(std::string &code)
@@ -195,7 +192,7 @@ void ansi_c_internal_additions(std::string &code)
195192
"int " CPROVER_PREFIX "malloc_failure_mode_assert_then_assume="+
196193
std::to_string(config.ansi_c.malloc_failure_mode_assert_then_assume)+";\n"
197194
CPROVER_PREFIX "size_t " CPROVER_PREFIX "max_malloc_size="+
198-
std::to_string(max_malloc_size(config.ansi_c.pointer_width, config
195+
integer2string(max_malloc_size(config.ansi_c.pointer_width, config
199196
.bv_encoding.object_bits))+";\n"
200197

201198
// this is ANSI-C

unit/ansi-c/max_malloc_size.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@ TEST_CASE(
3333

3434
SECTION("Max allocation size overflow")
3535
{
36-
REQUIRE_THROWS_AS(max_malloc_size(128, 63), invariant_failedt);
3736
}
3837

3938
SECTION("Valid allocation size configurations")
@@ -44,5 +43,8 @@ TEST_CASE(
4443
// Here we use 4 bits for the object ID, leaving 3 for the offset
4544
REQUIRE(max_malloc_size(8, 4) == 8);
4645
REQUIRE(max_malloc_size(128, 64) == 9223372036854775808ull /*2^63*/);
46+
REQUIRE(
47+
max_malloc_size(128, 63) == string2integer("18446744073709551616"
48+
/*2^64*/));
4749
}
4850
}

0 commit comments

Comments
 (0)