Skip to content

Commit c632625

Browse files
authored
Merge pull request #3305 from tautschnig/expr2bits
to_bitvector_type must not be used on enum types
2 parents f08e23b + 2e8d9d4 commit c632625

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

regression/cbmc/enum7/main.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include <assert.h>
2+
3+
enum E
4+
{
5+
A = 1,
6+
B = 2
7+
};
8+
9+
int main()
10+
{
11+
enum E array[2] = {A, B};
12+
unsigned *p = array;
13+
assert(*p == 1);
14+
return 0;
15+
}

regression/cbmc/enum7/test.desc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.c
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/util/simplify_expr.cpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1558,25 +1558,34 @@ optionalt<std::string> simplify_exprt::expr2bits(
15581558

15591559
if(expr.id()==ID_constant)
15601560
{
1561+
const auto &value = to_constant_expr(expr).get_value();
1562+
15611563
if(type.id()==ID_unsignedbv ||
15621564
type.id()==ID_signedbv ||
1563-
type.id()==ID_c_enum ||
1564-
type.id()==ID_c_enum_tag ||
15651565
type.id()==ID_floatbv ||
15661566
type.id()==ID_fixedbv)
15671567
{
15681568
const auto width = to_bitvector_type(type).get_width();
1569-
const auto &bvrep = to_constant_expr(expr).get_value();
15701569

15711570
endianness_mapt map(type, little_endian, ns);
15721571

15731572
std::string result(width, ' ');
15741573

15751574
for(std::string::size_type i = 0; i < width; ++i)
1576-
result[map.map_bit(i)] = get_bvrep_bit(bvrep, width, i) ? '1' : '0';
1575+
result[map.map_bit(i)] = get_bvrep_bit(value, width, i) ? '1' : '0';
15771576

15781577
return result;
15791578
}
1579+
else if(type.id() == ID_c_enum_tag)
1580+
{
1581+
const typet &c_enum_type = ns.follow_tag(to_c_enum_tag_type(type));
1582+
return expr2bits(constant_exprt(value, c_enum_type), little_endian);
1583+
}
1584+
else if(type.id() == ID_c_enum)
1585+
{
1586+
return expr2bits(
1587+
constant_exprt(value, to_c_enum_type(type).subtype()), little_endian);
1588+
}
15801589
}
15811590
else if(expr.id()==ID_union)
15821591
{

0 commit comments

Comments
 (0)