Skip to content

Commit 9a4c18e

Browse files
author
Thomas Kiley
committed
Also check for pointers whose width is too small
This ensures the other preconditions don't pass spuriously with a zero width pointer
1 parent 6c458d9 commit 9a4c18e

File tree

2 files changed

+6
-0
lines changed

2 files changed

+6
-0
lines changed

src/ansi-c/ansi_c_internal_additions.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ using max_alloc_sizet = uint64_t;
133133
static max_alloc_sizet
134134
max_malloc_size(std::size_t pointer_width, std::size_t object_bits)
135135
{
136+
PRECONDITION(pointer_width >= 1);
136137
PRECONDITION(object_bits < pointer_width - 1);
137138
PRECONDITION(object_bits >= 1);
138139
const auto bits_for_offset = pointer_width - object_bits - 1;

unit/ansi-c/max_malloc_size.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ TEST_CASE(
2828
REQUIRE_THROWS_AS(max_malloc_size(4, 0), invariant_failedt);
2929
}
3030

31+
SECTION("Not enough bits in the pointer")
32+
{
33+
REQUIRE_THROWS_AS(max_malloc_size(0, 0), invariant_failedt);
34+
}
35+
3136
SECTION("Max allocation size overflow")
3237
{
3338
REQUIRE_THROWS_AS(max_malloc_size(128, 63), invariant_failedt);

0 commit comments

Comments
 (0)