question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Incorrect for loop decompilation

See original GitHub issue

CFR version

a913e06

Compiler

JDK 14, or what ever special compiler IntelliJ uses

Description

The code doesn’t produce the same result after decompilation:

 public static void main(String[] args) {
    int n = 15;
    for (float i = 0; i < n; i++, i*= 2, System.out.println(i), i -= 2, i *= 1.2, i += 0.5) {
    }
  }

prints

2.0
3.0
5.4
11.16
24.984

Decompiled code:

    public static void main(String[] args) {
        int n = 15;
        for (float i = 0.0f; i < (float)n; i -= 2.0f) {
            i += 1.0f;
            System.out.println(i *= 2.0f);
            i = (float)((double)i * 1.2);
            i = (float)((double)i + 0.5);
        }
    }

prints

2.0
3.8000002
8.120001
18.488003

Edit: Not an IDE bug

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
leibnitz27commented, Jun 24, 2020

Yes, it’s a decompiler issue. I can’t speak for the others, as CFR shares 0 heritage with them, but what’s happening for me is that the accounting on the -2 mutation is wrong, at a guess.

Good spot.

0reactions
leibnitz27commented, Jun 24, 2020

It’s the difference between the * and the - looking like (due to the double casts) they can be treated as mutations or not that does it. (slightly different accounting paths.)

Fun one, will try to get to that soonish.

Read more comments on GitHub >

github_iconTop Results From Across the Web

[core] incorrect decompilation for loops like while(++i < XX ... - GitHub
Describe error Hi. I have found several decompilation failures that the decompilation may silently change the semantics of the source code.
Read more >
Incorrect decompilation for group assignment in |for| loop
The new behavior looks correct. The group assignment stays inside the loop now. Status: NEW → RESOLVED. Closed: 11 years ago. Resolution: ---...
Read more >
Disable optimization of for loops in Hex Rays Decompiler
Are there any good ways of splitting up a very long for loop or if statement in IDA into larger chunks to make...
Read more >
Bartosz Adamczewski on Twitter: "When we decompile this code we ...
When we decompile this code we see that it uses a single register to handle the entire loop and emits a bounds checking...
Read more >
How does Reflector decompile code? - Stack Overflow
A common technique for implementing decompilation is to use something called "Interval Analysis" for identifying the extent of loops.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found