Skip to content

Commit 9647f32

Browse files
committed
fix: release buffer on tp_alloc failure in vectorcall functions
When _parse_fastcall_args succeeds but tp_alloc subsequently fails, the acquired buffer was leaked. Now release buf/buf_owner before returning NULL in all four vectorcall functions.
1 parent da76b27 commit 9647f32

1 file changed

Lines changed: 16 additions & 4 deletions

File tree

src/_xxhash.c

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -511,8 +511,11 @@ PYXXH32_vectorcall(PyObject *type, PyObject *const *args,
511511

512512
PYXXH32Object *self = (PYXXH32Object *)
513513
((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0);
514-
if (self == NULL)
514+
if (self == NULL) {
515+
PyBuffer_Release(&buf);
516+
Py_XDECREF(buf_owner);
515517
return NULL;
518+
}
516519

517520
self->xxhash_state = XXH32_createState();
518521
if (self->xxhash_state == NULL) {
@@ -871,8 +874,11 @@ PYXXH64_vectorcall(PyObject *type, PyObject *const *args,
871874

872875
PYXXH64Object *self = (PYXXH64Object *)
873876
((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0);
874-
if (self == NULL)
877+
if (self == NULL) {
878+
PyBuffer_Release(&buf);
879+
Py_XDECREF(buf_owner);
875880
return NULL;
881+
}
876882

877883
self->xxhash_state = XXH64_createState();
878884
if (self->xxhash_state == NULL) {
@@ -1227,8 +1233,11 @@ PYXXH3_64_vectorcall(PyObject *type, PyObject *const *args,
12271233

12281234
PYXXH3_64Object *self = (PYXXH3_64Object *)
12291235
((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0);
1230-
if (self == NULL)
1236+
if (self == NULL) {
1237+
PyBuffer_Release(&buf);
1238+
Py_XDECREF(buf_owner);
12311239
return NULL;
1240+
}
12321241

12331242
self->xxhash_state = XXH3_createState();
12341243
if (self->xxhash_state == NULL) {
@@ -1592,8 +1601,11 @@ PYXXH3_128_vectorcall(PyObject *type, PyObject *const *args,
15921601

15931602
PYXXH3_128Object *self = (PYXXH3_128Object *)
15941603
((PyTypeObject *)type)->tp_alloc((PyTypeObject *)type, 0);
1595-
if (self == NULL)
1604+
if (self == NULL) {
1605+
PyBuffer_Release(&buf);
1606+
Py_XDECREF(buf_owner);
15961607
return NULL;
1608+
}
15971609

15981610
self->xxhash_state = XXH3_createState();
15991611
if (self->xxhash_state == NULL) {

0 commit comments

Comments
 (0)