Skip to content

Commit 42c6746

Browse files
committed
Fix typo in simplify_byte_extract
As the comment says, only simplify when byte_extract_id() matches the current configuration (and not on a mismatch!). Added a unit test as it isn't entirely obvious what regression tests may produce such expressions.
1 parent 7f002fa commit 42c6746

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

src/util/simplify_expr.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1722,9 +1722,9 @@ bool simplify_exprt::simplify_byte_extract(byte_extract_exprt &expr)
17221722
// byte extract of full object is object
17231723
// don't do any of the following if endianness doesn't match, as
17241724
// bytes need to be swapped
1725-
if(offset==0 &&
1726-
base_type_eq(expr.type(), expr.op().type(), ns) &&
1727-
byte_extract_id()!=expr.id())
1725+
if(
1726+
offset == 0 && base_type_eq(expr.type(), expr.op().type(), ns) &&
1727+
byte_extract_id() == expr.id())
17281728
{
17291729
exprt tmp=expr.op();
17301730
expr.swap(tmp);

unit/util/simplify_expr.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
#include <testing-utils/catch.hpp>
1010

1111
#include <util/arith_tools.h>
12+
#include <util/byte_operators.h>
1213
#include <util/c_types.h>
14+
#include <util/cmdline.h>
1315
#include <util/config.h>
1416
#include <util/namespace.h>
1517
#include <util/pointer_predicates.h>
@@ -59,3 +61,24 @@ TEST_CASE("Simplify const pointer offset")
5961
REQUIRE(!to_integer(simp, offset_value));
6062
REQUIRE(offset_value==1234);
6163
}
64+
65+
TEST_CASE("Simplify byte extract")
66+
{
67+
// this test does require a proper architecture to be set so that byte extract
68+
// uses adequate endianness
69+
cmdlinet cmdline;
70+
config.set(cmdline);
71+
72+
symbol_tablet symbol_table;
73+
namespacet ns(symbol_table);
74+
75+
// byte-extracting type T at offset 0 from an object of type T yields the
76+
// object
77+
symbol_exprt s("foo", size_type());
78+
byte_extract_exprt be(
79+
byte_extract_id(), s, from_integer(0, index_type()), size_type());
80+
81+
exprt simp = simplify_expr(be, ns);
82+
83+
REQUIRE(simp == s);
84+
}

0 commit comments

Comments
 (0)