diff --git a/jadx-core/src/main/java/jadx/core/dex/visitors/regions/RegionMaker.java b/jadx-core/src/main/java/jadx/core/dex/visitors/regions/RegionMaker.java index 390e7aa9886ef4a0b0c8d51ad114b9251bb2eb59..2134ab5c7f3807d3c1f1192c9c4c3c2c84892a36 100644 --- a/jadx-core/src/main/java/jadx/core/dex/visitors/regions/RegionMaker.java +++ b/jadx-core/src/main/java/jadx/core/dex/visitors/regions/RegionMaker.java @@ -28,6 +28,7 @@ import jadx.core.utils.BlockUtils; import jadx.core.utils.ErrorsCounter; import jadx.core.utils.InstructionRemover; import jadx.core.utils.RegionUtils; +import jadx.core.utils.exceptions.JadxOverflowException; import java.util.ArrayList; import java.util.BitSet; @@ -49,8 +50,12 @@ import static jadx.core.utils.BlockUtils.selectOther; public class RegionMaker { private static final Logger LOG = LoggerFactory.getLogger(RegionMaker.class); + // 'dumb' guard to prevent endless loop in regions processing + private static final int REGIONS_LIMIT = 1000 * 1000; + private final MethodNode mth; private BitSet processedBlocks; + private int regionsCount; public RegionMaker(MethodNode mth) { this.mth = mth; @@ -68,6 +73,10 @@ public class RegionMaker { processedBlocks.set(id); } } + regionsCount++; + if (regionsCount > REGIONS_LIMIT) { + throw new JadxOverflowException("Regions count limit reached"); + } Region r = new Region(stack.peekRegion()); BlockNode next = startBlock; diff --git a/jadx-core/src/main/java/jadx/core/dex/visitors/regions/RegionStack.java b/jadx-core/src/main/java/jadx/core/dex/visitors/regions/RegionStack.java index c6f47266f637b122a77645ceae0164db3152c7a9..bdba01e23a2217bee18d9688b29582db4e867204 100644 --- a/jadx-core/src/main/java/jadx/core/dex/visitors/regions/RegionStack.java +++ b/jadx-core/src/main/java/jadx/core/dex/visitors/regions/RegionStack.java @@ -3,6 +3,7 @@ package jadx.core.dex.visitors.regions; import jadx.core.dex.nodes.BlockNode; import jadx.core.dex.nodes.IRegion; import jadx.core.dex.nodes.MethodNode; +import jadx.core.utils.exceptions.JadxOverflowException; import java.util.ArrayDeque; import java.util.Collection; @@ -17,6 +18,8 @@ final class RegionStack { private static final Logger LOG = LoggerFactory.getLogger(RegionStack.class); private static final boolean DEBUG = false; + private static final int REGIONS_STACK_LIMIT = 1000; + static { if (DEBUG) { LOG.debug("Debug enabled for {}", RegionStack.class); @@ -58,8 +61,8 @@ final class RegionStack { public void push(IRegion region) { stack.push(curState); - if (stack.size() > 1000) { - throw new StackOverflowError("Deep code hierarchy"); + if (stack.size() > REGIONS_STACK_LIMIT) { + throw new JadxOverflowException("Regions stack size limit reached"); } curState = curState.copy(); curState.region = region; diff --git a/jadx-core/src/main/java/jadx/core/utils/ErrorsCounter.java b/jadx-core/src/main/java/jadx/core/utils/ErrorsCounter.java index 3a5ef2ee5ba736fa1e34bf2e026b877cdf4cfc52..e991abe36c415d38b5151dc84ac0d7b25e6c53ba 100644 --- a/jadx-core/src/main/java/jadx/core/utils/ErrorsCounter.java +++ b/jadx-core/src/main/java/jadx/core/utils/ErrorsCounter.java @@ -5,6 +5,7 @@ import jadx.core.dex.attributes.IAttributeNode; import jadx.core.dex.attributes.nodes.JadxErrorAttr; import jadx.core.dex.nodes.ClassNode; import jadx.core.dex.nodes.MethodNode; +import jadx.core.utils.exceptions.JadxOverflowException; import java.util.HashSet; import java.util.Set; @@ -32,9 +33,9 @@ public class ErrorsCounter { errorsCount++; if (e != null) { - if (e.getClass() == StackOverflowError.class) { + if (e.getClass() == JadxOverflowException.class) { // don't print full stack trace - e = new StackOverflowError(e.getMessage()); + e = new JadxOverflowException(e.getMessage()); LOG.error(msg); } else { LOG.error(msg, e); diff --git a/jadx-core/src/main/java/jadx/core/utils/exceptions/JadxOverflowException.java b/jadx-core/src/main/java/jadx/core/utils/exceptions/JadxOverflowException.java new file mode 100644 index 0000000000000000000000000000000000000000..3e7ebc14065a77a6840c8f594864dfd376778f59 --- /dev/null +++ b/jadx-core/src/main/java/jadx/core/utils/exceptions/JadxOverflowException.java @@ -0,0 +1,7 @@ +package jadx.core.utils.exceptions; + +public class JadxOverflowException extends JadxRuntimeException { + public JadxOverflowException(String message) { + super(message); + } +}