@@ -41,6 +41,10 @@ bool simplify_exprt::simplify_bswap(bswap_exprt &expr)
41
41
42
42
// put back together, but backwards
43
43
mp_integer new_value=0 ;
44
+
45
+ // May I suggest to move this out of the loop and instead check that at least as many elements are contained in bytes?
46
+ // this invariant protects against someone modifying the loop
47
+ // expression - a bit pointless for it to be outside the loop.
44
48
for (std::size_t bit = 0 ; bit < width; bit += bits_per_byte)
45
49
{
46
50
INVARIANT (
@@ -858,27 +862,22 @@ bool simplify_exprt::simplify_bitwise(exprt &expr)
858
862
bool simplify_exprt::simplify_extractbit (exprt &expr)
859
863
{
860
864
PRECONDITION (expr.id () == ID_extractbit);
861
- auto &extractbit_expr = to_extractbit_expr (expr);
865
+ const auto &extractbit_expr = to_extractbit_expr (expr);
862
866
863
867
const typet &src_type = extractbit_expr.src ().type ();
864
868
865
869
if (!is_bitvector_type (src_type))
866
870
return true ;
867
871
868
- std::size_t src_bit_width = to_bitvector_type (src_type).get_width ();
872
+ const std::size_t src_bit_width = to_bitvector_type (src_type).get_width ();
869
873
870
- if (extractbit_expr.index ().id () != ID_constant)
871
- {
872
- return true ;
873
- }
874
-
875
- auto index_converted_to_int =
874
+ const auto index_converted_to_int =
876
875
numeric_cast<mp_integer>(extractbit_expr.index ());
877
876
if (!index_converted_to_int.has_value ())
878
877
{
879
878
return true ;
880
879
}
881
- mp_integer index_as_int = index_converted_to_int.has_value ();
880
+ const mp_integer index_as_int = index_converted_to_int.value ();
882
881
if (!extractbit_expr.src ().is_constant ())
883
882
return true ;
884
883
@@ -888,12 +887,15 @@ bool simplify_exprt::simplify_extractbit(exprt &expr)
888
887
const irep_idt &src_value =
889
888
to_constant_expr (extractbit_expr.src ()).get_value ();
890
889
891
- if (src_value.size () != src_bit_width)
890
+
891
+ std::string src_value_as_string=id2string (src_value);
892
+
893
+ if (src_value_as_string.size () != src_bit_width)
892
894
return true ;
893
895
894
- bool bit =
895
- (id2string (src_value)
896
- [src_bit_width - numeric_cast_v<std::size_t >(index_as_int) - 1 ] == ' 1' );
896
+ const bool bit =
897
+ (src_value_as_string
898
+ [src_bit_width - numeric_cast_v<std::size_t >(index_as_int) - 1 ]== ' 1' );
897
899
898
900
expr.make_bool (bit);
899
901
0 commit comments