Skip to content

Commit 0255dd3

Browse files
committed
Byte extract with negative offset must not fail invariant
The code has provisions for handling negative offsets in a byte extract, but then tried to convert only parts of index to an unsigned number: the overall index is known to be non-negative in this branch, but the offset alone may still be negative.
1 parent 3cb60fc commit 0255dd3

File tree

3 files changed

+23
-1
lines changed

3 files changed

+23
-1
lines changed

regression/cbmc/byte_extract1/main.c

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
#include <assert.h>
2+
#include <string.h>
3+
4+
int main()
5+
{
6+
int x;
7+
int *p = (char *)&x - 2;
8+
int y;
9+
memcpy(&y, p, sizeof(int));
10+
assert(0);
11+
}
+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=10$
5+
^SIGNAL=0$
6+
^VERIFICATION FAILED$
7+
--
8+
Invariant check failed
9+
--
10+
This example previously failed the invariant "mp_integer should be convertible
11+
to target integral type".

src/solvers/flattening/boolbv_byte_extract.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ bvt boolbvt::convert_byte_extract(const byte_extract_exprt &expr)
121121
if(offset + i < 0 || offset + i >= op_bv.size())
122122
bv[i]=prop.new_variable();
123123
else
124-
bv[i] = op_bv[numeric_cast_v<std::size_t>(offset) + i];
124+
bv[i] = op_bv[numeric_cast_v<std::size_t>(offset + i)];
125125
}
126126
else
127127
{

0 commit comments

Comments
 (0)