Skip to content

Commit c308d18

Browse files
committed
Value set: avoid potential infinite expression
Encountered during security scanner analysis: a potential case where an infinite series of pointer casts (both explicit and implicit via taking the address of a structure's first member) was accumulated, leading to nontermintion of the VSA algorithm. This is a temporary fix until the underlying cause can be conclusively identified.
1 parent 554f796 commit c308d18

File tree

1 file changed

+29
-1
lines changed

1 file changed

+29
-1
lines changed

src/pointer-analysis/value_set.cpp

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1062,6 +1062,29 @@ void value_sett::get_reference_set(
10621062
dest.push_back(to_expr(it));
10631063
}
10641064

1065+
static void strip_casts(exprt& e, const namespacet& ns, const typet& target_type_raw)
1066+
{
1067+
const auto& target_type=ns.follow(target_type_raw);
1068+
while(true)
1069+
{
1070+
if(e.id()==ID_typecast)
1071+
e=e.op0();
1072+
else if(e.id()==ID_member)
1073+
{
1074+
auto& mem=to_member_expr(e);
1075+
const auto& struct_type=to_struct_type(ns.follow(e.op0().type()));
1076+
if(mem.get_component_name()==struct_type.components()[0].get_name())
1077+
e=e.op0();
1078+
else
1079+
return;
1080+
}
1081+
else
1082+
return;
1083+
if(ns.follow(e.type())==target_type)
1084+
return;
1085+
}
1086+
}
1087+
10651088
/*******************************************************************\
10661089
10671090
Function: value_sett::get_reference_set_rec
@@ -1208,7 +1231,13 @@ void value_sett::get_reference_set_rec(
12081231
{
12091232
// adjust type?
12101233
if(ns.follow(struct_op.type())!=final_object_type)
1234+
{
1235+
// Avoid an infinite loop of casting by stripping typecasts
1236+
// and address-of-first-members first.
1237+
strip_casts(member_expr.op0(),ns,struct_op.type());
1238+
if(ns.follow(member_expr.op0().type())!=ns.follow(struct_op.type()))
12111239
member_expr.op0().make_typecast(struct_op.type());
1240+
}
12121241

12131242
insert(dest, member_expr, o);
12141243
}
@@ -1492,7 +1521,6 @@ void value_sett::assign_rec(
14921521
if(lhs.id()==ID_symbol)
14931522
{
14941523
const irep_idt &identifier=to_symbol_expr(lhs).get_identifier();
1495-
14961524
entryt &e=get_entry(entryt(identifier, suffix), lhs.type(), ns);
14971525

14981526
if(add_to_sets)

0 commit comments

Comments
 (0)