Skip to content

Commit 0532d93

Browse files
committed
more safe
1 parent 7958c55 commit 0532d93

File tree

1 file changed

+15
-3
lines changed

1 file changed

+15
-3
lines changed

Modules/arraymodule.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -728,10 +728,17 @@ array_dealloc(arrayobject *op)
728728
PyTypeObject *tp = Py_TYPE(op);
729729
PyObject_GC_UnTrack(op);
730730

731-
if (op->weakreflist != NULL)
731+
if (op->ob_exports > 0) {
732+
PyErr_SetString(PyExc_SystemError,
733+
"deallocated array object has exported buffers");
734+
PyErr_Print();
735+
}
736+
if (op->weakreflist != NULL) {
732737
PyObject_ClearWeakRefs((PyObject *) op);
733-
if (op->ob_item != NULL)
738+
}
739+
if (op->ob_item != NULL) {
734740
PyMem_Free(op->ob_item);
741+
}
735742
tp->tp_free(op);
736743
Py_DECREF(tp);
737744
}
@@ -2835,7 +2842,11 @@ array_buffer_getbuf_lock_held(arrayobject *self, Py_buffer *view, int flags)
28352842
#endif
28362843
}
28372844

2845+
#ifdef Py_GIL_DISABLED
2846+
_Py_atomic_add_ssize(&self->ob_exports, 1);
2847+
#else
28382848
self->ob_exports++;
2849+
#endif
28392850
return 0;
28402851
}
28412852

@@ -2853,9 +2864,10 @@ static void
28532864
array_buffer_relbuf(arrayobject *self, Py_buffer *view)
28542865
{
28552866
#ifdef Py_GIL_DISABLED
2856-
_Py_atomic_add_ssize(&self->ob_exports, -1);
2867+
assert(_Py_atomic_add_ssize(&self->ob_exports, -1) >= 1);
28572868
#else
28582869
self->ob_exports--;
2870+
assert(self->ob_exports >= 0);
28592871
#endif
28602872
}
28612873

0 commit comments

Comments
 (0)