提交 41cc83db 编写于 作者: F fruiter

core: fix nested try-catch blocks processing

上级 ce7101be
...@@ -101,10 +101,14 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor { ...@@ -101,10 +101,14 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor {
// search dominator blocks in this region (don't need to go deeper) // search dominator blocks in this region (don't need to go deeper)
for (BlockNode dominator : tryBlocksMap.keySet()) { for (BlockNode dominator : tryBlocksMap.keySet()) {
if (region.getSubBlocks().contains(dominator)) { if (region.getSubBlocks().contains(dominator)) {
wrapBlocks(region, dominator); Region newRegion = wrapBlocks(region, dominator);
tryBlocksMap.remove(dominator); tryBlocksMap.remove(dominator);
// if region is modified rerun this method if (newRegion != null) {
leaveRegion(mth, region); // dominator may be moved into new region
leaveRegion(mth, newRegion);
// if region is modified rerun this method
leaveRegion(mth, region);
}
return; return;
} }
} }
...@@ -113,7 +117,7 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor { ...@@ -113,7 +117,7 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor {
/** /**
* Extract all block dominated by 'dominator' to separate region and mark as try/catch block * Extract all block dominated by 'dominator' to separate region and mark as try/catch block
*/ */
private void wrapBlocks(IRegion region, BlockNode dominator) { private Region wrapBlocks(IRegion region, BlockNode dominator) {
Region newRegion = new Region(region); Region newRegion = new Region(region);
TryCatchBlock tb = tryBlocksMap.get(dominator); TryCatchBlock tb = tryBlocksMap.get(dominator);
assert tb != null; assert tb != null;
...@@ -127,7 +131,7 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor { ...@@ -127,7 +131,7 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor {
} }
} }
if (newRegion.getSubBlocks().isEmpty()) { if (newRegion.getSubBlocks().isEmpty()) {
return; return null;
} }
if (DEBUG) { if (DEBUG) {
LOG.debug("ProcessTryCatchRegions mark: {}", newRegion); LOG.debug("ProcessTryCatchRegions mark: {}", newRegion);
...@@ -147,6 +151,8 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor { ...@@ -147,6 +151,8 @@ public class ProcessTryCatchRegions extends AbstractRegionVisitor {
aReg.setParent(newRegion); aReg.setParent(newRegion);
} }
} }
return newRegion;
} }
private boolean isHandlerPath(TryCatchBlock tb, IContainer cont) { private boolean isHandlerPath(TryCatchBlock tb, IContainer cont) {
......
package jadx.tests.internal.trycatch;
import jadx.api.InternalJadxTest;
import jadx.core.dex.nodes.ClassNode;
import org.junit.Test;
import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.not;
import static org.junit.Assert.assertThat;
public class TestNestedTryCatch extends InternalJadxTest {
public static class TestCls {
private void f() {
try {
Thread.sleep(1);
try {
Thread.sleep(2);
} catch (InterruptedException e) {
}
} catch (Exception e) {
}
return;
}
}
@Test
public void test() {
ClassNode cls = getClassNode(TestCls.class);
String code = cls.getCode().toString();
assertThat(code, containsString("try {"));
assertThat(code, containsString("Thread.sleep(1);"));
assertThat(code, containsString("Thread.sleep(2);"));
assertThat(code, containsString("} catch (InterruptedException e) {"));
assertThat(code, containsString("} catch (Exception e2) {"));
assertThat(code, not(containsString("return")));
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册