Skip to content

Commit a329ff8

Browse files
committed
Fix return of wrong value in pointer subtraction.
When subtracting pointers, such as in the expression (&a - &b), both a and b need to be of the same type, and the return value is the number of "steps" (as in, pointer increments for the underlying type) for the two types.
1 parent 41f27a9 commit a329ff8

File tree

1 file changed

+13
-1
lines changed

1 file changed

+13
-1
lines changed

src/solvers/smt2_incremental/convert_expr_to_smt.cpp

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -657,11 +657,23 @@ static smt_termt convert_expr_to_smt(
657657
// into an if-else branch that gives proper error handling information.
658658
const bool one_operand_pointer = lhs_is_pointer || rhs_is_pointer;
659659

660-
if(both_operands_bitvector || both_operands_pointers)
660+
if(both_operands_bitvector)
661661
{
662662
return smt_bit_vector_theoryt::subtract(
663663
converted.at(minus.lhs()), converted.at(minus.rhs()));
664664
}
665+
else if(both_operands_pointers)
666+
{
667+
const auto lhs_base_type = to_pointer_type(minus.lhs().type()).base_type();
668+
const auto rhs_base_type = to_pointer_type(minus.rhs().type()).base_type();
669+
INVARIANT(
670+
lhs_base_type == rhs_base_type,
671+
"only pointers of the same object type can be subtracted.");
672+
return smt_bit_vector_theoryt::unsigned_divide(
673+
smt_bit_vector_theoryt::subtract(
674+
converted.at(minus.lhs()), converted.at(minus.rhs())),
675+
pointer_sizes.at(lhs_base_type));
676+
}
665677
else if(one_operand_pointer)
666678
{
667679
UNIMPLEMENTED_FEATURE(

0 commit comments

Comments
 (0)