fix: improve exception handlers checks (#2086)

This commit is contained in:
Skylot 2024-07-27 21:08:16 +01:00
parent 33bcbfec41
commit 04a454094b
No known key found for this signature in database
GPG Key ID: 47A4975761262B6A
4 changed files with 18 additions and 11 deletions

View File

@ -226,6 +226,10 @@ public final class BlockNode extends AttrNode implements IBlock, Comparable<Bloc
return contains(AFlag.RETURN);
}
public boolean isMthExitBlock() {
return contains(AFlag.MTH_EXIT_BLOCK);
}
public boolean isEmpty() {
return instructions.isEmpty();
}

View File

@ -627,7 +627,7 @@ public class BlockExceptionHandler {
continue;
}
for (TryCatchBlockAttr tcb : tryBlocks) {
if (tcb.getHandlers().contains(handlerBlock)) {
if (tcb.getHandlers().contains(eh)) {
notProcessed = false;
break;
}

View File

@ -445,8 +445,9 @@ public class RegionMaker {
List<BlockNode> simplePath = BlockUtils.buildSimplePath(exit);
if (!simplePath.isEmpty()) {
BlockNode lastBlock = simplePath.get(simplePath.size() - 1);
if (lastBlock.contains(AFlag.RETURN)
|| lastBlock.getSuccessors().isEmpty()) {
if (lastBlock.isMthExitBlock()
|| lastBlock.isReturnBlock()
|| mth.isPreExitBlock(lastBlock)) {
return false;
}
}

View File

@ -10,14 +10,13 @@ import org.junit.jupiter.api.Test;
import jadx.tests.api.SmaliTest;
import static jadx.tests.api.utils.assertj.JadxAssertions.assertThat;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.MatcherAssert.assertThat;
@SuppressWarnings("CommentedOutCode")
public class TestUnreachableCatch2 extends SmaliTest {
@SuppressWarnings({ "unused", "DataFlowIssue" })
public static class UnusedExceptionHandlers1 implements AutoCloseable {
public static void doSomething(final Object unused1, final Object[] array, final Object o1,
public static void test(final Object unused1, final Object[] array, final Object o1,
final Object o2, final Object unused2) {
for (final Object item : array) {
ByteBuffer buffer = null;
@ -25,10 +24,10 @@ public class TestUnreachableCatch2 extends SmaliTest {
try (final FileInputStream fis = new FileInputStream(u.getFilename())) {
final FileChannel fileChannel = fis.getChannel();
buffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, 42);
} catch (final IOException e) {
} catch (IOException e) {
// ignore
}
} catch (final IOException e) {
} catch (IOException e) {
// ignore
}
}
@ -50,9 +49,12 @@ public class TestUnreachableCatch2 extends SmaliTest {
@Test
public void test() {
// TODO: result code not compilable because of 'break' after 'throw'
// TODO: result code not compilable because 'try' block split into 2 block and 'fis' var become
// uninitialized
disableCompilation();
String code = getClassNode(UnusedExceptionHandlers1.class).getCode().toString();
assertThat(code, containsString("IOException"));
assertThat(getClassNode(UnusedExceptionHandlers1.class))
.code()
.doesNotContain("break;")
.countString(2, "} catch (IOException e");
}
}