Skip to content

Commit dabe6c9

Browse files
Fixed crashes with uart_write and generic_api_send_command
Signed-off-by: David Rebbe <drebbe@intrepidcs.com>
1 parent 1825d77 commit dabe6c9

1 file changed

Lines changed: 9 additions & 31 deletions

File tree

src/methods.cpp

Lines changed: 9 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3740,53 +3740,42 @@ PyObject* meth_uart_write(PyObject* self, PyObject* args)
37403740
{
37413741
PyObject* obj = NULL;
37423742
EUartPort_t port = eUART0;
3743-
PyObject* data = NULL;
3743+
const char* data = NULL;
3744+
Py_ssize_t data_len = 0;
37443745
uint8_t flags = 0;
37453746
bool check_size = true;
3746-
if (!PyArg_ParseTuple(args, arg_parse("OIy*|bp:", __FUNCTION__), &obj, &port, &data, &flags, &check_size)) {
3747+
if (!PyArg_ParseTuple(args, arg_parse("OIy#|bp:", __FUNCTION__), &obj, &port, &data, &data_len, &flags, &check_size)) {
37473748
return NULL;
37483749
}
3749-
// Check if the data buffer is valid and grab the buffer
3750-
if (!PyObject_CheckBuffer(data)) {
3751-
return set_ics_exception(exception_runtime_error(), "Argument 'data' must be of type 'bytes'");
3752-
}
3753-
Py_buffer data_buffer = {};
3754-
PyObject_GetBuffer(data, &data_buffer, PyBUF_C_CONTIGUOUS | PyBUF_WRITABLE);
37553750

37563751
// Get the device handle
37573752
if (!PyNeoDevice_CheckExact(obj)) {
3758-
PyBuffer_Release(&data_buffer);
37593753
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
37603754
}
37613755
ICS_HANDLE handle = PyNeoDevice_GetHandle(obj);
37623756
try
37633757
{
37643758
ice::Library* lib = dll_get_library();
37653759
if (!lib) {
3766-
PyBuffer_Release(&data_buffer);
37673760
char buffer[512];
37683761
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
37693762
}
37703763
// int _stdcall icsneoUartWrite(void* hObject, const EUartPort_t uart, const void* bData, const size_t bytesToSend, size_t* bytesActuallySent, uint8_t* flags)
37713764
size_t bytesActuallySent = 0;
37723765
ice::Function<int __stdcall (ICS_HANDLE, const EUartPort_t, const void*, const size_t, size_t*, uint8_t*)> icsneoUartWrite(lib, "icsneoUartWrite");
37733766
Py_BEGIN_ALLOW_THREADS
3774-
if (!icsneoUartWrite(handle, port, data_buffer.buf, data_buffer.len, &bytesActuallySent, &flags)) {
3767+
if (!icsneoUartWrite(handle, port, data, data_len, &bytesActuallySent, &flags)) {
37753768
Py_BLOCK_THREADS
3776-
PyBuffer_Release(&data_buffer);
37773769
return set_ics_exception(exception_runtime_error(), "icsneoUartWrite() Failed");
37783770
}
37793771
Py_END_ALLOW_THREADS
3780-
if (check_size && data_buffer.len != bytesActuallySent) {
3781-
PyBuffer_Release(&data_buffer);
3772+
if (check_size && data_len != bytesActuallySent) {
37823773
return set_ics_exception(exception_runtime_error(), "Bytes actually sent didn't match bytes to send length");
37833774
}
3784-
PyBuffer_Release(&data_buffer);
37853775
return Py_BuildValue("i", bytesActuallySent);
37863776
}
37873777
catch (ice::Exception& ex)
37883778
{
3789-
PyBuffer_Release(&data_buffer);
37903779
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
37913780
}
37923781
return set_ics_exception(exception_runtime_error(), "This is a bug!");
@@ -3928,27 +3917,20 @@ PyObject* meth_generic_api_send_command(PyObject* self, PyObject* args)
39283917
unsigned char apiIndex = 0;
39293918
unsigned char instanceIndex = 0;
39303919
unsigned char functionIndex = 0;
3931-
PyObject* data = NULL;
3932-
if (!PyArg_ParseTuple(args, arg_parse("Obbby*:", __FUNCTION__), &obj, &apiIndex, &instanceIndex, &functionIndex, data)) {
3920+
const char* data = NULL;
3921+
Py_ssize_t data_len = 0;
3922+
if (!PyArg_ParseTuple(args, arg_parse("Obbby#:", __FUNCTION__), &obj, &apiIndex, &instanceIndex, &functionIndex, data, &data_len)) {
39333923
return NULL;
39343924
}
3935-
// Check if the data buffer is valid and grab the buffer
3936-
if (!PyObject_CheckBuffer(data)) {
3937-
return set_ics_exception(exception_runtime_error(), "Argument 'data' must be of type 'bytes'");
3938-
}
3939-
Py_buffer data_buffer = {};
3940-
PyObject_GetBuffer(data, &data_buffer, PyBUF_C_CONTIGUOUS | PyBUF_WRITABLE);
39413925
// Get the device handle
39423926
if (!PyNeoDevice_CheckExact(obj)) {
3943-
PyBuffer_Release(&data_buffer);
39443927
return set_ics_exception(exception_runtime_error(), "Argument must be of type " MODULE_NAME "." NEO_DEVICE_OBJECT_NAME);
39453928
}
39463929
ICS_HANDLE handle = PyNeoDevice_GetHandle(obj);
39473930
try
39483931
{
39493932
ice::Library* lib = dll_get_library();
39503933
if (!lib) {
3951-
PyBuffer_Release(&data_buffer);
39523934
char buffer[512];
39533935
return set_ics_exception(exception_runtime_error(), dll_get_error(buffer));
39543936
}
@@ -3964,19 +3946,16 @@ PyObject* meth_generic_api_send_command(PyObject* self, PyObject* args)
39643946
ice::Function<int __stdcall (ICS_HANDLE, unsigned char, unsigned char, unsigned char, void*, unsigned int, unsigned char*)> icsneoGenericAPISendCommand(lib, "icsneoGenericAPISendCommand");
39653947
unsigned char functionError = 0;
39663948
Py_BEGIN_ALLOW_THREADS
3967-
if (!icsneoGenericAPISendCommand(handle, apiIndex, instanceIndex, functionIndex, data_buffer.buf, data_buffer.len, &functionError)) {
3968-
PyBuffer_Release(&data_buffer);
3949+
if (!icsneoGenericAPISendCommand(handle, apiIndex, instanceIndex, functionIndex, (void*)data, data_len, &functionError)) {
39693950
Py_BLOCK_THREADS
39703951
return set_ics_exception(exception_runtime_error(), "icsneoGenericAPISendCommand() Failed");
39713952
}
39723953
Py_END_ALLOW_THREADS
3973-
PyBuffer_Release(&data_buffer);
39743954
return Py_BuildValue("i", functionError);
39753955

39763956
}
39773957
catch (ice::Exception& ex)
39783958
{
3979-
PyBuffer_Release(&data_buffer);
39803959
return set_ics_exception(exception_runtime_error(), (char*)ex.what());
39813960
}
39823961
return set_ics_exception(exception_runtime_error(), "This is a bug!");
@@ -3987,7 +3966,6 @@ PyObject* meth_generic_api_read_data(PyObject* self, PyObject* args)
39873966
PyObject* obj = NULL;
39883967
unsigned char apiIndex = 0;
39893968
unsigned char instanceIndex = 0;
3990-
//PyObject* data = NULL;
39913969
unsigned int length = 0;
39923970
if (!PyArg_ParseTuple(args, arg_parse("ObbI:", __FUNCTION__), &obj, &apiIndex, &instanceIndex, &length)) {
39933971
return NULL;

0 commit comments

Comments
 (0)