leibnitz27 / cfr

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

Loose catch block and behaviour change #224

Open nitram84 opened 3 years ago

nitram84 commented 3 years ago

CFR version

CFR 0.151-SNAPSHOT (5d12bbb) and CFR 0.150, so this is not a regression of recent changes

Compiler

openjdk8, openjdk11

Description

Decompiling the provided example produces illegal java code and has behaviour changes. This is probably another failing finally example.

Example

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamReader;

public class CfrTest1 {
  public static void readXML(final String filename) {
    XMLStreamReader reader = null;
    try (InputStream in = new FileInputStream(filename)) {
      final XMLInputFactory factory = XMLInputFactory.newInstance();
      reader = factory.createXMLStreamReader(in);
    } catch (final XMLStreamException ex) {
      System.out.println("error creating xml stream reader");
    } catch (final IOException ex) {
      System.out.println("error");
    } finally {
      doSomething(reader);
    }
  }

  private static void doSomething(XMLStreamReader reader) {
  }
}

This example can be used for the cfr test suite.

werame commented 2 years ago

I can confirm this happens to me too with some commercial code (that I cannot share here to IP reasons). Using CFR 0.152 that's included with Recaf. The class having this issue is just a Java 7 class. But it runs fine in all Javas from 12 to 16, so I don't think there's any bug in the class itself.

CFR adds this comment:

    /*
     * Loose catch block
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     */

But then Recaf also complains:

image

(Sadly, Recaf doesn't support copy text for their error log.)

Col-E commented 2 years ago

But then Recaf also complains:

This is JavaParser's error output telling you what part of the code its failing to parse into an AST from the input text. That's part of an interactive menu which lets you click on the items to jump to the described error.

Using the demo code provided here:

image

The unreachable block is illegally formatted, hence the error.