c++capstone

How to make capstone produce the last line of binary input?


Today I tried to start with capstone. Following their example here I wanted to start working with the library. Unfortunately capstone does not produce the last line of the asm instructions that it disassembles. It only produces an empty line.

Code:

#include <stdio.h>
#include <inttypes.h>
#include <string>
#include <iostream>

#include <capstone/capstone.h>
int main(void)
{
    csh handle;
    cs_insn *insn;
    size_t count;
    const uint8_t CODE[] = {0x55,0x48,0x8b,0x05,0xb8,0x13,0x00,0x00};
    if (cs_open(CS_ARCH_X86, CS_MODE_64, &handle) != CS_ERR_OK)
        return -1;
    count = cs_disasm_ex(handle, CODE, sizeof(CODE)-1, 0x1000, 0, &insn);
    if (count > 0) {
        size_t j;
        for (j = 0; j < count; j++) {
            printf("0x%" PRIx64":\t%s\t\t%s\n", insn[j].address, insn[j].mnemonic,
            insn[j].op_str);
        }

        cs_free(insn, count);
        } else
            printf("ERROR: Failed to disassemble given code!\n");
        cs_close(&handle);

        return 0;
}

Instead of getting

0x1000: push        rbp
0x1001: mov     rax, qword ptr [rip + 0x13b8]

I get

0x1000: push        rbp

where the last line is printed, but empty.

Can someone help me resolve this?


Solution

  • Shouldn't you do

    cs_disasm_ex(handle, CODE, sizeof(CODE), 0x1000, 0, &insn);
    

    instead of

    cs_disasm_ex(handle, CODE, sizeof(CODE)-1, 0x1000, 0, &insn);
    

    ?