leibnitz27 / cfr

This is the public repository for the CFR Java decompiler
https://www.benf.org/other/cfr
MIT License
1.94k stars 249 forks source link

Malformed ternary is emitted for narrowing conversion of bitwise AND, OR, XOR result #216

Closed Marcono1234 closed 3 years ago

Marcono1234 commented 3 years ago

CFR version

CFR 0.151-SNAPSHOT (b79446a)

Exists in 00f0a883 as well, so was not introduced by recent changes.

Compiler

javac 11.0.5

Description

When decompiling a bitwise AND (&), OR (|) or XOR (^) whose result is cast to a smaller type (narrowing conversion), CFR emits a malformed ternary expression which tries to apply the bitwise operator with an integer variable and true / false as operands.

Example

Source:

int byteAnd1() {
    int b = 0;
    b = (byte) (b & 1);

    return b;
}

Decompiled output:

int byteAnd1() {
    byte by = 0;
    by = (byte)(by & true ? 1 : 0);
    return by;
}

Extensive tests are provided by https://github.com/leibnitz27/cfr_tests/pull/8

leibnitz27 commented 3 years ago

CFR suffers from this more than 'trusting' decompilers - because the whole 'I have no idea if you're a short, int, byte or bool' thing can be real fun - you're definitely right though - those cases are gross - the ternary can be useful as a fixup, but it shouldn't be applied that early......

leibnitz27 commented 3 years ago

Thanks for the excellent test set btw.