Skip to content

Commit

Permalink
[DEX] Fix: Proper exception handlers sorting
Browse files Browse the repository at this point in the history
  • Loading branch information
REAndroid committed Nov 25, 2024
1 parent 3cc81f1 commit dc6fe61
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 4 deletions.
4 changes: 4 additions & 0 deletions src/main/java/com/reandroid/dex/ins/CatchAllHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ TypeId getTypeId(){
return null;
}
@Override
public boolean isCatchAll() {
return true;
}
@Override
public SmaliDirective getSmaliDirective(){
return SmaliDirective.CATCH_ALL;
}
Expand Down
5 changes: 5 additions & 0 deletions src/main/java/com/reandroid/dex/ins/CatchTypedHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ public TypeKey getKey(){
public void setKey(TypeKey typeKey){
getTypeUle128().setKey(typeKey);
}
@Override
public boolean isCatchAll() {
return false;
}

Ule128IdItemReference<TypeId> getTypeUle128(){
return typeId;
}
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/com/reandroid/dex/ins/ExceptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import com.reandroid.dex.smali.SmaliRegion;
import com.reandroid.dex.smali.SmaliWriter;
import com.reandroid.dex.smali.model.SmaliCodeExceptionHandler;
import com.reandroid.utils.CompareUtil;
import com.reandroid.utils.HexUtil;
import com.reandroid.utils.ObjectsUtil;
import com.reandroid.utils.collection.ArrayIterator;
Expand Down Expand Up @@ -74,6 +75,9 @@ public TypeKey getKey(){
}
public void setKey(TypeKey typeKey){
}

public abstract boolean isCatchAll();

public boolean isAddressBounded(int address) {
if (address == -1) {
return true;
Expand Down Expand Up @@ -201,6 +205,27 @@ public void removeSelf(){
public boolean isRemoved() {
return getParent() == null;
}
int compareHandler(ExceptionHandler handler) {
if (handler == this) {
return 0;
}
int i = CompareUtil.compare(this.getAddress(), handler.getAddress());
if (i != 0) {
return i;
}
i = CompareUtil.compare(this.isCatchAll(), handler.isCatchAll());
if (i != 0) {
return i;
}
TryItem tryItem1 = getTryItem();
TryItem tryItem2 = handler.getTryItem();
i = CompareUtil.compare(tryItem1.getIndex(), tryItem2.getIndex());
if (i != 0) {
return i;
}
return CompareUtil.compare(tryItem1.getHandlerOffset().getIndex(),
tryItem2.getHandlerOffset().getIndex());
}
public void merge(ExceptionHandler handler){
catchAddress.set(handler.getCatchAddress());
}
Expand Down Expand Up @@ -346,6 +371,12 @@ public boolean isEqualExtraLine(Object obj) {
HandlerLabel label = (HandlerLabel) obj;
return this.getHandler() == label.getHandler();
}

@Override
public int compareLabelName(Label label) {
return getHandler().compareHandler(((HandlerLabel) label).getHandler());
}

@Override
public void appendExtra(SmaliWriter writer) throws IOException {
ExceptionHandler handler = this.getHandler();
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/reandroid/dex/ins/ExceptionLabel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,14 @@

public interface ExceptionLabel extends Label{
ExceptionHandler getHandler();

@Override
default boolean isRemoved() {
return getHandler().isRemoved();
}

@Override
default int compareLabelName(Label label) {
return 0;
}
}
10 changes: 6 additions & 4 deletions src/main/java/com/reandroid/dex/ins/Label.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ default boolean isEqualExtraLine(Object obj) {
@Override
default int compareExtraLine(ExtraLine other) {
int i = ExtraLine.super.compareExtraLine(other);
if(i != 0){
if (i != 0) {
return i;
}
if(!(other instanceof Label)){
return 0;
if (getClass() == other.getClass()) {
return compareLabelName((Label) other);
}
Label label = (Label) other;
return 0;
}
default int compareLabelName(Label label) {
return getLabelName().compareTo(label.getLabelName());
}
}

0 comments on commit dc6fe61

Please sign in to comment.