Skip to content
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

Inaccuracies in Decompiled Java Code for Floating Points and Booleans #2295

Open
Lohita9 opened this issue Oct 8, 2024 · 3 comments
Open
Labels
bug Core Issues in jadx-core module

Comments

@Lohita9
Copy link

Lohita9 commented Oct 8, 2024

Issue details

When decompiling the Java code, there are inconsistencies in handling floating-point numbers and boolean operations. The decompiled code introduces unnecessary type casting and operations, which differ from the original code.

Issue 1 - Float Type Casting: Original Java code:

static void checkFloats(float[] floats) {
    Main.assertTrue(floats[0] == -1.5);
    Main.assertTrue(floats[1] == -0.5);
    Main.assertTrue(floats[2] == 0.0);
    Main.assertTrue(floats[3] == 0.5);
    Main.assertTrue(floats[4] == 1.5);
}

Decompiled Java code:

static void checkFloats(float[] floats) {
    Main.assertTrue(((double) floats[0]) == -1.5d);
    Main.assertTrue(((double) floats[1]) == -0.5d);
    Main.assertTrue(((double) floats[2]) == 0.0d);
    Main.assertTrue(((double) floats[3]) == 0.5d);
    Main.assertTrue(((double) floats[4]) == 1.5d);
}

In the decompiled version, unnecessary type casting to (double) was added. This differs from the original code and can lead to precision issues or unwanted behavior.

Issue 2 - Boolean XOR Operator: Original Java code:

static void checkBooleans(boolean[] booleans) {
    Main.assertTrue(booleans[0]);
    Main.assertTrue(booleans[1]);
    Main.assertTrue(!booleans[2]);
    Main.assertTrue(booleans[3]);
    Main.assertTrue(!booleans[4]);
}

Decompiled Java code:

static void checkBooleans(boolean[] booleans) {
    Main.assertTrue(booleans[0]);
    Main.assertTrue(booleans[1]);
    Main.assertTrue(!booleans[2]);
    Main.assertTrue(booleans[3]);
    Main.assertTrue(true ^ booleans[4]);
}

In the decompiled version, true ^ booleans[4] was added unnecessarily, replacing !booleans[4]. This change alters the logic and is not faithful to the original intent.

Expected Behavior: The decompiled code should match the original source code without introducing unnecessary operations, type casting, or logical alterations.

** If you examine the full decompiled and original Array.java, you will see many instances of incorrect code.

Relevant log output or stacktrace

N/A

Provide sample and class/method full name

attachments.zip

Jadx version

latest unstable version

@Lohita9 Lohita9 added bug Core Issues in jadx-core module labels Oct 8, 2024
@jpstotz
Copy link
Collaborator

jpstotz commented Oct 8, 2024

In the decompiled version, true ^ booleans[4] was added unnecessarily, replacing !booleans[4]. This change alters the logic and is not faithful to the original intent.

true ^ booleans[4] (true xor a) is the same as !booleans[4] (!a) thus the logic has not changed. If you look at the smali code you will see that the actual smali code of checkBooleans uses indeed true ^ a in both cases. Therefore Jadx had already simplified the code in one case but not in the other. For code consistency I agree that it would be better to output in both cases ! instead of the true ^ ... version.

In case of checkFloats as far as I can see the generated code matches the smali code and also your java code. The constant numbers you have in your code are of type double and to compare them the Java compiler converts the floats to double. Therefore the only difference is the optional type case of the floats to double.

@Lohita9
Copy link
Author

Lohita9 commented Oct 9, 2024

@jpstotz Here are some additional resources that you can use to compare the decompiled Java code with the original Java code.

@MrIkso
Copy link
Contributor

MrIkso commented Oct 10, 2024

@Lohita9 good test classes. I see Goto test case code not decompiled

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Core Issues in jadx-core module
Projects
None yet
Development

No branches or pull requests

3 participants