Skip to content

FunC incorrectly optimizes expressions with operators: *, -, <= #1657

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
jeshecdom opened this issue May 9, 2025 · 0 comments
Open

FunC incorrectly optimizes expressions with operators: *, -, <= #1657

jeshecdom opened this issue May 9, 2025 · 0 comments

Comments

@jeshecdom
Copy link

Based on tact-lang/tact#3019

Code to reproduce

int calc(int x) {
    return ((0 * (- x)) <= 0);
}

() main() {
    throw_if(16, calc(-115792089237316195423570985008687907853269984665640564039457584007913129639936));
}

Output Fift

// automatically generated from `func_test.fc` 
DECLPROC calc
DECLPROC main
calc PROC:<{
  DROP
  TRUE
}>
main PROC:<{
  256 PUSHNEGPOW2
  calc CALLDICT
  16 THROWIF
}>

Actual behavior when executed

calc always returns true.

Expected behavior when executed

Exit code 4, since - x should produce an overflow when x = -115792089237316195423570985008687907853269984665640564039457584007913129639936 (i.e., x = MIN_INT).

Side note 1: We can produce a related but different expression that also gets mapped into TRUE by replacing the subexpression - x for other expression that also produces overflows and the subexpression 0 for other expression that is equal to 0. For example, the following expressions also get transformed into TRUE in Fift, but they should produce error code 4 on the provided value for x:

  • (~(-1) * (-1 * x)) <= 0 for x = MIN_INT.
  • ((1 & (~ 1)) * (x / -1)) <= 0 for x = MIN_INT.
  • ((x & 0) * (x * 2)) <= 0 for x = MAX_INT (i.e., x = 115792089237316195423570985008687907853269984665640564039457584007913129639935).
  • ((x * 0) * (x + 1)) <= 0 for x = MAX_INT.

Side note 2: The related expression (0 * (- x)) == 0 DOES get translated to Fift in the expected way:

calc PROC:<{
  NEGATE
  0 MULCONST
  0 EQINT
}>
@JohnnyGoGoGo JohnnyGoGoGo closed this as not planned Won't fix, can't repro, duplicate, stale May 9, 2025
@EmelyanenkoK EmelyanenkoK reopened this May 12, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants