Skip to content

Commit

Permalink
Merge pull request #96 from Gadgetoid/patch-exception-decref
Browse files Browse the repository at this point in the history
Add missing Py_DECREF to error handling
  • Loading branch information
doceme authored Jun 2, 2020
2 parents 25129b5 + 7cebd14 commit 0891de9
Showing 1 changed file with 24 additions and 4 deletions.
28 changes: 24 additions & 4 deletions spidev_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -478,15 +478,22 @@ SpiDev_xfer(SpiDevObject *self, PyObject *args)
return NULL;

seq = PySequence_Fast(obj, "expected a sequence");
len = PySequence_Fast_GET_SIZE(obj);
if (!seq || len <= 0) {
if (!seq) {
PyErr_SetString(PyExc_TypeError, wrmsg_list0);
return NULL;
}

len = PySequence_Fast_GET_SIZE(seq);
if (len <= 0) {
Py_DECREF(seq);
PyErr_SetString(PyExc_TypeError, wrmsg_list0);
return NULL;
}

if (len > SPIDEV_MAXPATH) {
snprintf(wrmsg_text, sizeof(wrmsg_text) - 1, wrmsg_listmax, SPIDEV_MAXPATH);
PyErr_SetString(PyExc_OverflowError, wrmsg_text);
Py_DECREF(seq);
return NULL;
}

Expand All @@ -512,6 +519,7 @@ SpiDev_xfer(SpiDevObject *self, PyObject *args)
free(xferptr);
free(txbuf);
free(rxbuf);
Py_DECREF(seq);
return NULL;
}
}
Expand All @@ -535,6 +543,7 @@ SpiDev_xfer(SpiDevObject *self, PyObject *args)
PyErr_SetFromErrno(PyExc_IOError);
free(txbuf);
free(rxbuf);
Py_DECREF(seq);
return NULL;
}
#else
Expand All @@ -553,6 +562,7 @@ SpiDev_xfer(SpiDevObject *self, PyObject *args)
PyErr_SetString(PyExc_TypeError, wrmsg_text);
free(txbuf);
free(rxbuf);
Py_DECREF(seq);
return NULL;
}
}
Expand Down Expand Up @@ -581,6 +591,7 @@ SpiDev_xfer(SpiDevObject *self, PyObject *args)
PyErr_SetFromErrno(PyExc_IOError);
free(txbuf);
free(rxbuf);
Py_DECREF(seq);
return NULL;
}
#endif
Expand Down Expand Up @@ -637,15 +648,22 @@ SpiDev_xfer2(SpiDevObject *self, PyObject *args)
return NULL;

seq = PySequence_Fast(obj, "expected a sequence");
len = PySequence_Fast_GET_SIZE(obj);
if (!seq || len <= 0) {
if (!seq) {
PyErr_SetString(PyExc_TypeError, wrmsg_list0);
return NULL;
}

len = PySequence_Fast_GET_SIZE(seq);
if (len <= 0) {
Py_DECREF(seq);
PyErr_SetString(PyExc_TypeError, wrmsg_list0);
return NULL;
}

if (len > SPIDEV_MAXPATH) {
snprintf(wrmsg_text, sizeof(wrmsg_text) - 1, wrmsg_listmax, SPIDEV_MAXPATH);
PyErr_SetString(PyExc_OverflowError, wrmsg_text);
Py_DECREF(seq);
return NULL;
}

Expand All @@ -669,6 +687,7 @@ SpiDev_xfer2(SpiDevObject *self, PyObject *args)
PyErr_SetString(PyExc_TypeError, wrmsg_text);
free(txbuf);
free(rxbuf);
Py_DECREF(seq);
return NULL;
}
}
Expand All @@ -693,6 +712,7 @@ SpiDev_xfer2(SpiDevObject *self, PyObject *args)
PyErr_SetFromErrno(PyExc_IOError);
free(txbuf);
free(rxbuf);
Py_DECREF(seq);
return NULL;
}

Expand Down

0 comments on commit 0891de9

Please sign in to comment.