Skip to content

Simplify byte extract: fix unconditional read from optional values #7376

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 1 commit into from
Nov 23, 2022
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
20 changes: 12 additions & 8 deletions src/util/simplify_expr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1680,15 +1680,19 @@ simplify_exprt::simplify_byte_extract(const byte_extract_exprt &expr)
{
return op_byte_update.value();
}
else if(
el_size.has_value() &&
*el_size <= pointer_offset_bits(op_byte_update.value().type(), ns))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Huh.. wasn't there even a compiler warning on that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was invoking this piece of code:
https://github.com/diffblue/cbmc/blob/8848ad878da46bbcd0cedad634e235e6faf8cb17/src/nonstd/optional.hpp#L997..L1000
which will return false when the optionalt doesn't have a value.

else if(el_size.has_value())
{
auto tmp = expr;
tmp.op() = op_byte_update.value();
tmp.offset() = from_integer(0, expr.offset().type());
const auto update_bits_opt =
pointer_offset_bits(op_byte_update.value().type(), ns);

if(update_bits_opt.has_value() && *el_size <= *update_bits_opt)
{
auto tmp = expr;
tmp.op() = op_byte_update.value();
tmp.offset() = from_integer(0, expr.offset().type());

return changed(simplify_byte_extract(tmp)); // recursive call
return changed(simplify_byte_extract(tmp)); // recursive call
}
}
}

Expand All @@ -1706,7 +1710,7 @@ simplify_exprt::simplify_byte_extract(const byte_extract_exprt &expr)
{
const byte_update_exprt &bu = to_byte_update_expr(expr.op());
const auto update_offset = numeric_cast<mp_integer>(bu.offset());
if(update_offset.has_value())
if(el_size.has_value() && update_offset.has_value())
{
if(
*offset * expr.get_bits_per_byte() + *el_size <=
Expand Down