diff --git a/src/solvers/flattening/boolbv_index.cpp b/src/solvers/flattening/boolbv_index.cpp index ca66fb2c697..f9bf25e6a3a 100644 --- a/src/solvers/flattening/boolbv_index.cpp +++ b/src/solvers/flattening/boolbv_index.cpp @@ -61,16 +61,17 @@ bvt boolbvt::convert_index(const index_exprt &expr) } // Must have a finite size - mp_integer array_size; - if(to_integer(array_type.size(), array_size)) - throw "failed to convert array size"; - - // see if the index address is constant - // many of these are compacted by simplify_expr - // but variable location writes will block this - mp_integer index_value; - if(!to_integer(index, index_value)) - return convert_index(array, index_value); + mp_integer array_size = numeric_cast_v(array_type.size()); + { + // see if the index address is constant + // many of these are compacted by simplify_expr + // but variable location writes will block this + auto maybe_index_value = numeric_cast(index); + if(maybe_index_value.has_value()) + { + return convert_index(array, maybe_index_value.value()); + } + } // Special case : arrays of one thing (useful for constants) // TODO : merge with ACTUAL_ARRAY_HACK so that ranges of the same @@ -115,12 +116,10 @@ bvt boolbvt::convert_index(const index_exprt &expr) binary_relation_exprt lower_bound( from_integer(0, index.type()), ID_le, index); + CHECK_RETURN(lower_bound.lhs().is_not_nil()); binary_relation_exprt upper_bound( index, ID_lt, from_integer(array_size, index.type())); - - if(lower_bound.lhs().is_nil() || - upper_bound.rhs().is_nil()) - throw "number conversion failed (2)"; + CHECK_RETURN(upper_bound.rhs().is_not_nil()); and_exprt range_condition(lower_bound, upper_bound); implies_exprt implication(range_condition, value_equality); @@ -172,11 +171,12 @@ bvt boolbvt::convert_index(const index_exprt &expr) for(mp_integer i=0; i(array_size * width)); // TODO: maybe a shifter-like construction would be better // Would be a lot more compact but propagate worse @@ -229,9 +227,7 @@ bvt boolbvt::convert_index(const index_exprt &expr) for(mp_integer i=0; i0); + DATA_INVARIANT( + array_size > 0, + "non-positive array sizes are forbidden in goto programs"); for(mp_integer i=0; i