Skip to content

Commit c5212d0

Browse files
authored
do not crash on __delitem__ (#235)
1 parent 956ad23 commit c5212d0

3 files changed

Lines changed: 18 additions & 0 deletions

File tree

src/c/_cffi_backend.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2609,6 +2609,11 @@ cdata_ass_slice(CDataObject *cd, PySliceObject *slice, PyObject *v)
26092609
cdata = cd->c_data + itemsize * bounds[0];
26102610
length = bounds[1];
26112611

2612+
if (v == NULL) {
2613+
PyErr_SetString(PyExc_TypeError,
2614+
"'del x[n]' not supported for cdata objects");
2615+
return -1;
2616+
}
26122617
if (CData_Check(v)) {
26132618
CTypeDescrObject *ctv = ((CDataObject *)v)->c_type;
26142619
if ((ctv->ct_flags & CT_ARRAY) && (ctv->ct_itemdescr == ct) &&

src/c/minibuffer.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,11 @@ static PyObject *mb_subscript(MiniBufferObj *self, PyObject *item)
243243
static int
244244
mb_ass_subscript(MiniBufferObj* self, PyObject* item, PyObject* value)
245245
{
246+
if (value == NULL) {
247+
PyErr_SetString(PyExc_TypeError,
248+
"'del x[n]' not supported for buffer objects");
249+
return -1;
250+
}
246251
if (PyIndex_Check(item)) {
247252
Py_ssize_t i = PyNumber_AsSsize_t(item, PyExc_IndexError);
248253
if (i == -1 && PyErr_Occurred())

testing/cffi0/test_ffi_backend.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,14 @@ def test_unpack(self):
587587
p = ffi.new("int[]", [-123456789])
588588
assert ffi.unpack(p, 1) == [-123456789]
589589

590+
def test_delitem_raises(self):
591+
ffi = FFI()
592+
arr = ffi.new("int[5]")
593+
buf = ffi.buffer(arr)
594+
for obj in (arr, buf):
595+
pytest.raises(TypeError, obj.__delitem__, 0) # del obj[0]
596+
pytest.raises(TypeError, obj.__delitem__, slice(1, 3)) # del obj[1:3]
597+
590598
def test_negative_array_size(self):
591599
ffi = FFI()
592600
pytest.raises(ValueError, ffi.cast, "int[-5]", 0)

0 commit comments

Comments
 (0)