Raise exceptions on instruction decoding

This commit is contained in:
angie 2023-08-08 11:57:39 -04:00
parent 3d0221687b
commit 72b439fe9a
4 changed files with 15 additions and 8 deletions

View File

@ -4,7 +4,7 @@
[package]
name = "rabbitizer"
# Version should be synced with include/common/RabbitizerVersion.h
version = "1.7.4"
version = "1.7.5"
edition = "2021"
authors = ["Anghelo Carvajal <angheloalf95@gmail.com>"]
description = "MIPS instruction decoder"

View File

@ -14,7 +14,7 @@ extern "C" {
// Header version
#define RAB_VERSION_MAJOR 1
#define RAB_VERSION_MINOR 7
#define RAB_VERSION_PATCH 4
#define RAB_VERSION_PATCH 5
#define RAB_VERSION_STR RAB_STRINGIFY(RAB_VERSION_MAJOR) "." RAB_STRINGIFY(RAB_VERSION_MINOR) "." RAB_STRINGIFY(RAB_VERSION_PATCH)

View File

@ -4,7 +4,7 @@
[project]
name = "rabbitizer"
# Version should be synced with include/common/RabbitizerVersion.h
version = "1.7.4"
version = "1.7.5.dev0"
description = "MIPS instruction decoder"
# license = "MIT"
readme = "README.md"

View File

@ -463,6 +463,7 @@ static PyObject *rabbitizer_type_Instruction_disassemble(PyRabbitizerInstruction
size_t immOverrideLength = 0;
int extraLJust = 0;
size_t bufferSize;
size_t disassembledSize;
char *buffer;
PyObject *ret;
@ -478,11 +479,17 @@ static PyObject *rabbitizer_type_Instruction_disassemble(PyRabbitizerInstruction
buffer = malloc(bufferSize+1);
if (buffer == NULL) {
// TODO: signal an exception?
PyErr_SetString(PyExc_MemoryError, "Not able to allocate enough space for decoded instruction.");
return NULL;
}
RabbitizerInstruction_disassemble(&self->instr, buffer, immOverride, immOverrideLength, extraLJust);
disassembledSize = RabbitizerInstruction_disassemble(&self->instr, buffer, immOverride, immOverrideLength, extraLJust);
if (disassembledSize > bufferSize) {
PyErr_SetString(PyExc_AssertionError, "Decoded instruction does not fit in the allocated buffer.\n"
"This will produce a memory corruption error.\n"
"This is not an user error, please report this bug.");
return NULL;
}
ret = PyUnicode_FromString(buffer);
free(buffer);
@ -613,7 +620,7 @@ static PyObject *rabbitizer_type_Instruction_repr(PyRabbitizerInstruction *self)
buffer = bufferStart = malloc(disasmBufferSize+1 + typeNameLength + extraSize);
if (buffer == NULL) {
// TODO: signal an exception?
PyErr_SetString(PyExc_MemoryError, "Not able to allocate enough space for decoded instruction.");
return NULL;
}
@ -622,8 +629,8 @@ static PyObject *rabbitizer_type_Instruction_repr(PyRabbitizerInstruction *self)
len = sprintf(buffer, "(0x%08X) # ", RabbitizerInstruction_getRaw(&self->instr));
if (len != 15) {
// bad stuff
// TODO: exception?
PyErr_SetString(PyExc_AssertionError, "This should not be triggered. assertion: len == 15");
return NULL;
}
assert(len == 15);
buffer += len;