Skip to content

Soundness: integer / and % use Z3's Euclidean semantics, not Java's, for negative operands #564

In review

Description

@CatarinaGamboa

Impact: wrong programs pass verification (unsound) when integer division or remainder has a negative operand.

Description
Java's / on integers rounds toward zero, and % takes the sign of the dividend. Z3's integer div/mod, which TranslatorToZ3.makeDiv/makeMod use, are Euclidean: the remainder is never negative.

Java Z3
-7 / 2 -3 -4
-7 % 2 -1 1

Minimal reproducer

import liquidjava.specification.Refinement;

public class Div {
    public static void main(String[] args) {
        @Refinement("_ == -4") int q = -7 / 2;   // Java gives -3: should be rejected
        @Refinement("_ == 1") int r = -7 % 2;    // Java gives -1: should be rejected
    }
}

Result on main: Correct! Passed Verification.

Expected
Both lines are rejected. / and % in refinements and in code should follow Java's semantics for all signs (truncation toward zero, remainder with the dividend's sign). Division by zero stays as it is today.

Found while looking at #536. That report itself is not a bug, since its program really is wrong, but it led here.

Activity

  1. added
    bug
    on Nov 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions