cs_open() should return error on invalid mode

This commit is contained in:
Nguyen Anh Quynh 2014-01-21 15:26:02 +08:00
parent 1031048241
commit 06b3c05e20
7 changed files with 30 additions and 2 deletions

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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);

View File

@ -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;

7
cs.c
View File

@ -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;

View File

@ -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