Skip to content

Commit

Permalink
fix(gui): resolve cast exception for variable reference (#1489)
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed May 21, 2022
1 parent bd8a44c commit e07332d
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 15 deletions.
9 changes: 6 additions & 3 deletions jadx-core/src/main/java/jadx/api/JadxDecompiler.java
Original file line number Diff line number Diff line change
Expand Up @@ -683,10 +683,13 @@ private JavaVariable resolveVarRef(ICodeInfo codeInfo, VarRef varRef) {
throw new JadxRuntimeException("Missing code info for resolve VarRef: " + varRef);
}
ICodeAnnotation varNodeAnn = codeInfo.getCodeMetadata().getAt(varRef.getRefPos());
if (varNodeAnn == null) {
return null;
if (varNodeAnn != null && varNodeAnn.getAnnType() == ICodeAnnotation.AnnType.DECLARATION) {
ICodeNodeRef nodeRef = ((NodeDeclareRef) varNodeAnn).getNode();
if (nodeRef.getAnnType() == ICodeAnnotation.AnnType.VAR) {
return resolveVarNode((VarNode) nodeRef);
}
}
return (JavaVariable) getJavaNodeByCodeAnnotation(codeInfo, varNodeAnn);
return null;
}

List<JavaNode> convertNodes(Collection<? extends ICodeNodeRef> nodesList) {
Expand Down
16 changes: 13 additions & 3 deletions jadx-core/src/main/java/jadx/api/JavaClass.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import org.jetbrains.annotations.ApiStatus;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import jadx.api.metadata.ICodeAnnotation;
import jadx.api.metadata.ICodeAnnotation.AnnType;
import jadx.api.metadata.ICodeNodeRef;
import jadx.core.dex.attributes.AFlag;
import jadx.core.dex.attributes.AType;
Expand All @@ -22,6 +26,7 @@
import jadx.core.dex.nodes.MethodNode;

public final class JavaClass implements JavaNode {
private static final Logger LOG = LoggerFactory.getLogger(JavaClass.class);

private final JadxDecompiler decompiler;
private final ClassNode cls;
Expand Down Expand Up @@ -196,12 +201,17 @@ public List<Integer> getUsePlacesFor(ICodeInfo codeInfo, JavaNode javaNode) {
List<Integer> result = new ArrayList<>();
for (Map.Entry<Integer, ICodeAnnotation> entry : map.entrySet()) {
ICodeAnnotation ann = entry.getValue();
if (ann.getAnnType() == ICodeAnnotation.AnnType.DECLARATION) {
// ignore declarations
AnnType annType = ann.getAnnType();
if (annType == AnnType.DECLARATION || annType == AnnType.OFFSET) {
// ignore declarations and offset annotations
continue;
}
// ignore declarations
JavaNode annNode = rootDec.getJavaNodeByCodeAnnotation(codeInfo, ann);
if (javaNode.equals(annNode)) {
if (annNode == null && LOG.isDebugEnabled()) {
LOG.debug("Failed to resolve code annotation, cls: {}, pos: {}, ann: {}", this, entry.getKey(), ann);
}
if (Objects.equals(annNode, javaNode)) {
result.add(entry.getKey());
}
}
Expand Down
4 changes: 0 additions & 4 deletions jadx-gui/src/main/java/jadx/gui/ui/dialog/UsageDialog.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ private void processUsage(JavaNode searchNode, JavaClass topUseClass) {
JadxDecompiler decompiler = mainWindow.getWrapper().getDecompiler();
List<Integer> usePositions = topUseClass.getUsePlacesFor(codeInfo, searchNode);
for (int pos : usePositions) {
if (searchNode.getTopParentClass().equals(topUseClass) && pos == searchNode.getDefPos()) {
// skip declaration
continue;
}
String line = CodeUtils.getLineForPos(code, pos);
if (line.startsWith("import ")) {
continue;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
public class DiskCodeCache implements ICodeCache {
private static final Logger LOG = LoggerFactory.getLogger(DiskCodeCache.class);

private static final int DATA_FORMAT_VERSION = 7;
private static final int DATA_FORMAT_VERSION = 8;

private final Path srcDir;
private final Path metaDir;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,14 @@ public class VarRefAdapter extends BaseDataAdapter<VarRef> {
@Override
public void write(DataOutput out, VarRef value) throws IOException {
int refPos = value.getRefPos();
if (refPos == 0) {
throw new RuntimeException("Variable refPos is zero: " + value);
if (refPos <= 0) {
throw new RuntimeException("Variable refPos is invalid: " + value);
}
out.writeShort(refPos);
out.writeInt(refPos);
}

@Override
public VarRef read(DataInput in) throws IOException {
return VarRef.fromPos(in.readShort());
return VarRef.fromPos(in.readInt());
}
}

0 comments on commit e07332d

Please sign in to comment.