Skip to content

Commit

Permalink
Merge pull request capstone-engine#540 from kevemueller/masterCherry
Browse files Browse the repository at this point in the history
Fix NPE when first instruction to disassemble is broken.
  • Loading branch information
aquynh committed Nov 14, 2015
2 parents 14e3b78 + 2ef8ef7 commit 156b45e
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions bindings/java/capstone/Capstone.java
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,8 @@ protected class NativeStruct {
private NativeLongByReference handleRef;
}

private static final CsInsn[] EMPTY_INSN = new CsInsn[0];

protected NativeStruct ns; // for memory retention
private CS cs;
public int arch;
Expand Down Expand Up @@ -428,17 +430,36 @@ public int close() {
return cs.cs_close(ns.handleRef);
}

// disassemble until either no more code, or encounter broken insn.
/**
* Disassemble instructions from @code assumed to be located at @address,
* stop when encountering first broken instruction.
*
* @param code The source machine code bytes.
* @param address The address of the first machine code byte.
* @return the array of successfully disassembled instructions, empty if no instruction could be disassembled.
*/
public CsInsn[] disasm(byte[] code, long address) {
return disasm(code, address, 0);
}

// disassemble maximum @count instructions, or until encounter broken insn.
/**
* Disassemble up to @count instructions from @code assumed to be located at @address,
* stop when encountering first broken instruction.
*
* @param code The source machine code bytes.
* @param address The address of the first machine code byte.
* @param count The maximum number of instructions to disassemble, 0 for no maximum.
* @return the array of successfully disassembled instructions, empty if no instruction could be disassembled.
*/
public CsInsn[] disasm(byte[] code, long address, long count) {
PointerByReference insnRef = new PointerByReference();

NativeLong c = cs.cs_disasm(ns.csh, code, new NativeLong(code.length), address, new NativeLong(count), insnRef);


if (0 == c.intValue()) {
return EMPTY_INSN;
}

Pointer p = insnRef.getValue();
_cs_insn byref = new _cs_insn(p);

Expand Down

0 comments on commit 156b45e

Please sign in to comment.