Skip to content

Commit

Permalink
cs_open() should return error on invalid mode
Browse files Browse the repository at this point in the history
  • Loading branch information
aquynh committed Jan 21, 2014
1 parent 7ad216c commit 53fc5c1
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 2 deletions.
4 changes: 4 additions & 0 deletions arch/AArch64/AArch64Module.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ void enable_arm64() {}

static cs_err init(cs_struct *ud)
{
// verify if requested mode is valid
if (ud->mode & ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM | CS_MODE_BIG_ENDIAN))
return CS_ERR_MODE;

MCRegisterInfo *mri = cs_mem_malloc(sizeof(*mri));

AArch64_init(mri);
Expand Down
5 changes: 5 additions & 0 deletions arch/ARM/ARMModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,11 @@

static cs_err init(cs_struct *ud)
{
// verify if requested mode is valid
if (ud->mode & ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_ARM |
CS_MODE_THUMB | CS_MODE_BIG_ENDIAN))
return CS_ERR_MODE;

MCRegisterInfo *mri = cs_mem_malloc(sizeof(*mri));

ARM_init(mri);
Expand Down
5 changes: 5 additions & 0 deletions arch/Mips/MipsModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ void enable_mips() {};

static cs_err init(cs_struct *ud)
{
// verify if requested mode is valid
if (ud->mode & ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 |
CS_MODE_MICRO | CS_MODE_N64 | CS_MODE_BIG_ENDIAN))
return CS_ERR_MODE;

MCRegisterInfo *mri = cs_mem_malloc(sizeof(*mri));

Mips_init(mri);
Expand Down
5 changes: 5 additions & 0 deletions arch/PowerPC/PPCModule.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ void enable_powerpc() {};

static cs_err init(cs_struct *ud)
{
// verify if requested mode is valid
if (ud->mode & ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 |
CS_MODE_BIG_ENDIAN))
return CS_ERR_MODE;

MCRegisterInfo *mri = cs_mem_malloc(sizeof(*mri));

PPC_init(mri);
Expand Down
4 changes: 4 additions & 0 deletions arch/X86/X86Module.c
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@ void enable_x86() {};

static cs_err init(cs_struct *ud)
{
// verify if requested mode is valid
if (ud->mode & ~(CS_MODE_LITTLE_ENDIAN | CS_MODE_32 | CS_MODE_64 | CS_MODE_16))
return CS_ERR_MODE;

// by default, we use Intel syntax
ud->printer = X86_Intel_printInst;
ud->printer_info = NULL;
Expand Down
7 changes: 6 additions & 1 deletion cs.c
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,12 @@ cs_err cs_open(cs_arch arch, cs_mode mode, csh *handle)
// by default, do not break instruction into details
ud->detail = CS_OPT_OFF;

arch_init[ud->arch](ud);
cs_err err = arch_init[ud->arch](ud);
if (err) {
cs_mem_free(ud);
*handle = 0;
return err;
}

*handle = (uintptr_t)ud;

Expand Down
2 changes: 1 addition & 1 deletion include/capstone.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ typedef enum cs_err {
CS_ERR_MEM, // Out-Of-Memory error: cs_open(), cs_disasm_ex()
CS_ERR_ARCH, // Unsupported architecture: cs_open()
CS_ERR_HANDLE, // Invalid handle: cs_op_count(), cs_op_index()
CS_ERR_CSH, // Invalid csh argument: cs_close(), cs_errno(), cs_option()
CS_ERR_CSH, // Invalid csh argument: cs_close(), cs_errno(), cs_option()
CS_ERR_MODE, // Invalid/unsupported mode: cs_open()
CS_ERR_OPTION, // Invalid/unsupported option: cs_option()
CS_ERR_DETAIL, // Information is unavailable because detail option is OFF
Expand Down

0 comments on commit 53fc5c1

Please sign in to comment.