Skip to content

Commit ce50f69

Browse files
committed
BUG: fix reference leaks and null dereferences across the extension
1 parent 4ebd953 commit ce50f69

7 files changed

Lines changed: 25 additions & 9 deletions

File tree

‎src/csrc/casts.cpp‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ extern "C" {
1515
}
1616
#include <cstring>
1717
#include <cstdlib>
18+
#include <new>
1819
#include <type_traits>
1920
#include "sleef.h"
2021
#include "sleefquad.h"
@@ -1828,7 +1829,7 @@ init_casts(void)
18281829
try {
18291830
return init_casts_internal();
18301831
}
1831-
catch (int e) {
1832+
catch (const std::bad_alloc &) {
18321833
PyErr_NoMemory();
18331834
return nullptr;
18341835
}

‎src/csrc/dtype.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -628,8 +628,7 @@ QuadPrecDType_new(PyTypeObject *NPY_UNUSED(cls), PyObject *args, PyObject *kwds)
628628
return NULL;
629629
}
630630

631-
return (PyObject *)quadprec_discover_descriptor_from_pyobject(
632-
&QuadPrecDType, (PyObject *)QuadPrecision_raw_new(backend));
631+
return (PyObject *)new_quaddtype_instance(backend);
633632
}
634633

635634
static PyObject *
@@ -707,10 +706,12 @@ init_quadprec_dtype(void)
707706
((PyTypeObject *)&QuadPrecDType)->tp_base = &PyArrayDescr_Type;
708707

709708
if (PyType_Ready((PyTypeObject *)&QuadPrecDType) < 0) {
709+
free_casts();
710710
return -1;
711711
}
712712

713713
if (PyArrayInitDTypeMeta_FromSpec(&QuadPrecDType, &QuadPrecDType_DTypeSpec) < 0) {
714+
free_casts();
714715
return -1;
715716
}
716717

‎src/csrc/lock.c‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,14 @@ PyThread_type_lock sleef_lock = NULL;
66
PyMutex sleef_lock = {0};
77
#endif
88

9-
void init_sleef_locks(void)
9+
int init_sleef_locks(void)
1010
{
1111
#if PY_VERSION_HEX < 0x30d00b3
1212
sleef_lock = PyThread_allocate_lock();
1313
if (!sleef_lock) {
1414
PyErr_NoMemory();
15+
return -1;
1516
}
1617
#endif
18+
return 0;
1719
}

‎src/csrc/quaddtype_main.c‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,19 @@ PyInit__quaddtype_main(void)
102102
PyUnstable_Module_SetGIL(m, Py_MOD_GIL_NOT_USED);
103103
#endif
104104

105-
init_sleef_locks();
105+
if (init_sleef_locks() < 0)
106+
goto error;
106107

107108
if (init_quadprecision_scalar() < 0)
108109
goto error;
109110

110-
if (PyModule_AddObject(m, "QuadPrecision", (PyObject *)&QuadPrecision_Type) < 0)
111+
if (PyModule_AddObjectRef(m, "QuadPrecision", (PyObject *)&QuadPrecision_Type) < 0)
111112
goto error;
112113

113114
if (init_quadprec_dtype() < 0)
114115
goto error;
115116

116-
if (PyModule_AddObject(m, "QuadPrecDType", (PyObject *)&QuadPrecDType) < 0)
117+
if (PyModule_AddObjectRef(m, "QuadPrecDType", (PyObject *)&QuadPrecDType) < 0)
117118
goto error;
118119

119120
if (init_quad_umath() < 0) {

‎src/csrc/scalar.c‎

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,10 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
200200
}
201201
else if (PyUnicode_Check(value)) {
202202
const char *s = PyUnicode_AsUTF8(value);
203+
if (s == NULL) {
204+
Py_DECREF(self);
205+
return NULL;
206+
}
203207
char *endptr = NULL;
204208
int err = NumPyOS_ascii_strtoq(s, backend, &self->value, &endptr);
205209
if (err < 0) {
@@ -248,6 +252,9 @@ QuadPrecision_from_object(PyObject *value, QuadBackendType backend)
248252
QuadPrecisionObject *quad_obj = (QuadPrecisionObject *)value;
249253
// create a new one with the same backend
250254
QuadPrecisionObject *self = QuadPrecision_raw_new(quad_obj->backend);
255+
if (self == NULL) {
256+
return NULL;
257+
}
251258
if (quad_obj->backend == BACKEND_SLEEF) {
252259
self->value.sleef_value = quad_obj->value.sleef_value;
253260
}
@@ -620,7 +627,10 @@ QuadPrecision_as_integer_ratio(QuadPrecisionObject *self, PyObject *Py_UNUSED(ig
620627
}
621628

622629
Py_DECREF(py_exp);
623-
return PyTuple_Pack(2, numerator, denominator);
630+
PyObject *ratio = PyTuple_Pack(2, numerator, denominator);
631+
Py_DECREF(numerator);
632+
Py_DECREF(denominator);
633+
return ratio;
624634
}
625635

626636
static int

‎src/csrc/scalar_ops.cpp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ quad_binary_func(PyObject *op1, PyObject *op2)
7878
other_quad = (QuadPrecisionObject *)other;
7979
if (other_quad->backend != backend) {
8080
PyErr_SetString(PyExc_TypeError, "Cannot mix QuadPrecision backends");
81+
Py_DECREF(other_quad);
8182
Py_DECREF(other);
8283
return NULL;
8384
}

‎src/include/lock.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,6 @@ extern PyMutex sleef_lock;
1313
#define UNLOCK_SLEEF PyMutex_Unlock(&sleef_lock)
1414
#endif
1515

16-
void init_sleef_locks(void);
16+
int init_sleef_locks(void);
1717

1818
#endif // _QUADDTYPE_LOCK_H

0 commit comments

Comments
 (0)