Skip to content

Avoid infinite recursion in has_subtype #2079

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 29 additions & 20 deletions src/util/expr_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ Author: Daniel Kroening, [email protected]

#include "expr_util.h"

#include <unordered_set>
#include "expr.h"
#include "expr_iterator.h"
#include "fixedbv.h"
Expand Down Expand Up @@ -140,28 +141,36 @@ bool has_subtype(
const std::function<bool(const typet &)> &pred,
const namespacet &ns)
{
if(pred(type))
return true;
else if(type.id() == ID_symbol)
return has_subtype(ns.follow(type), pred, ns);
else if(type.id() == ID_c_enum_tag)
return has_subtype(ns.follow_tag(to_c_enum_tag_type(type)), pred, ns);
else if(type.id() == ID_struct || type.id() == ID_union)
{
const struct_union_typet &struct_union_type = to_struct_union_type(type);
std::vector<std::reference_wrapper<const typet>> stack;
std::unordered_set<typet, irep_hash> visited;

for(const auto &comp : struct_union_type.components())
if(has_subtype(comp.type(), pred, ns))
return true;
}
// do not look into pointer subtypes as this could cause unbounded recursion
else if(type.id() == ID_array || type.id() == ID_vector)
return has_subtype(type.subtype(), pred, ns);
else if(type.has_subtypes())
const auto push_if_not_visited = [&](const typet &t) {
if(visited.insert(t).second)
stack.emplace_back(t);
};

push_if_not_visited(type);
while(!stack.empty())
{
for(const auto &subtype : type.subtypes())
if(has_subtype(subtype, pred, ns))
return true;
const typet &top = stack.back().get();
stack.pop_back();

if(pred(top))
return true;
else if(top.id() == ID_symbol)
push_if_not_visited(ns.follow(top));
else if(top.id() == ID_c_enum_tag)
push_if_not_visited(ns.follow_tag(to_c_enum_tag_type(top)));
else if(top.id() == ID_struct || top.id() == ID_union)
{
for(const auto &comp : to_struct_union_type(top).components())
push_if_not_visited(comp.type());
}
else
{
for(const auto &subtype : top.subtypes())
push_if_not_visited(subtype);
}
}

return false;
Expand Down