Skip to content

Commit

Permalink
fix: add missing null check in codegen for classes generated by jadx
Browse files Browse the repository at this point in the history
  • Loading branch information
skylot committed Oct 14, 2024
1 parent b872ffd commit 8f3cc3e
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 16 deletions.
30 changes: 16 additions & 14 deletions jadx-cli/src/main/java/jadx/cli/JadxCLI.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,28 @@ public class JadxCLI {
private static final Logger LOG = LoggerFactory.getLogger(JadxCLI.class);

public static void main(String[] args) {
int result = 0;
int result = 1;
try {
result = execute(args);
} catch (JadxArgsValidateException e) {
LOG.error("Incorrect arguments: {}", e.getMessage());
result = 1;
} catch (Throwable e) {
LOG.error("Process error:", e);
result = 1;
} finally {
System.exit(result);
}
}

public static int execute(String[] args) {
JadxCLIArgs jadxArgs = new JadxCLIArgs();
if (jadxArgs.processArgs(args)) {
return processAndSave(jadxArgs);
try {
JadxCLIArgs jadxArgs = new JadxCLIArgs();
if (jadxArgs.processArgs(args)) {
return processAndSave(jadxArgs);
}
return 0;
} catch (JadxArgsValidateException e) {
LOG.error("Incorrect arguments: {}", e.getMessage());
return 1;
} catch (Throwable e) {
LOG.error("Process error:", e);
return 1;
}
return 0;
}

private static int processAndSave(JadxCLIArgs cliArgs) {
Expand All @@ -66,11 +68,11 @@ private static int processAndSave(JadxCLIArgs cliArgs) {
if (errorsCount != 0) {
jadx.printErrorsReport();
LOG.error("finished with errors, count: {}", errorsCount);
} else {
LOG.info("done");
return 1;
}
LOG.info("done");
return 0;
}
return 0;
}

private static void initCodeWriterProvider(JadxArgs jadxArgs) {
Expand Down
32 changes: 32 additions & 0 deletions jadx-cli/src/test/java/jadx/cli/TestInput.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import java.util.stream.Collectors;
import java.util.stream.Stream;

import org.assertj.core.api.Condition;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import org.slf4j.Logger;
Expand All @@ -36,6 +37,22 @@ public void testHelp() {
assertThat(result).isEqualTo(0);
}

@Test
public void testApkInput() throws Exception {
int result = JadxCLI.execute(buildArgs(List.of(), "samples/small.apk"));
assertThat(result).isEqualTo(0);
List<Path> resultFiles = collectAllFilesInDir(testDir);
printFiles(resultFiles);
assertThat(resultFiles)
.describedAs("check output files")
.map(p -> p.getFileName().toString())
.haveExactly(2, new Condition<>(f -> f.endsWith(".java"), "java classes"))
.haveExactly(9, new Condition<>(f -> f.endsWith(".xml"), "xml resources"))
.haveExactly(1, new Condition<>(f -> f.equals("classes.dex"), "dex"))
.haveExactly(1, new Condition<>(f -> f.equals("AndroidManifest.xml"), "manifest"))
.hasSize(13);
}

@Test
public void testDexInput() throws Exception {
decompile("samples/hello.dex");
Expand Down Expand Up @@ -110,11 +127,26 @@ private String[] buildArgs(List<String> options, String... inputSamples) throws
return args.toArray(new String[0]);
}

private void printFiles(List<Path> files) {
LOG.info("Output files (count: {}):", files.size());
for (Path file : files) {
LOG.info(" {}", testDir.relativize(file));
}
}

private static List<Path> collectJavaFilesInDir(Path dir) throws IOException {
PathMatcher javaMatcher = dir.getFileSystem().getPathMatcher("glob:**.java");
return collectFilesInDir(dir, javaMatcher);
}

private static List<Path> collectAllFilesInDir(Path dir) throws IOException {
try (Stream<Path> pathStream = Files.walk(dir)) {
return pathStream
.filter(Files::isRegularFile)
.collect(Collectors.toList());
}
}

private static List<Path> collectFilesInDir(Path dir, PathMatcher matcher) throws IOException {
try (Stream<Path> pathStream = Files.walk(dir)) {
return pathStream
Expand Down
Binary file added jadx-cli/src/test/resources/samples/small.apk
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,9 @@ public static void addInputFileInfo(ICodeWriter code, ClassNode cls) {
String inputFileName = cls.getClsData().getInputFileName();
if (inputFileName != null) {
ClassNode declCls = cls.getDeclaringClass();
if (declCls != null && inputFileName.equals(declCls.getClsData().getInputFileName())) {
if (declCls != null
&& declCls.getClsData() != null
&& inputFileName.equals(declCls.getClsData().getInputFileName())) {
// don't add same comment for inner classes
return;
}
Expand Down
7 changes: 6 additions & 1 deletion jadx-core/src/main/java/jadx/core/dex/nodes/ClassNode.java
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,12 @@ protected void getDisassembledCode(SimpleCodeWriter code) {
}
}

public IClassData getClsData() {
/**
* Low level class data access.
*
* @return null for classes generated by jadx
*/
public @Nullable IClassData getClsData() {
return clsData;
}

Expand Down

0 comments on commit 8f3cc3e

Please sign in to comment.