Skip to content

Commit f347c6e

Browse files
ZackerySpytzserhiy-storchaka
authored andcommitted
bpo-35504: Fix segfaults and SystemErrors when deleting certain attrs. (GH-11175) (GH-11249)
(cherry picked from commit 842acaa)
1 parent 3752bc9 commit f347c6e

File tree

9 files changed

+38
-0
lines changed

9 files changed

+38
-0
lines changed

Lib/ctypes/test/test_strings.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,13 @@ def test_param_2(self):
6161
## print BUF.from_param(c_char_p("python"))
6262
## print BUF.from_param(BUF(*"pyth"))
6363

64+
def test_del_segfault(self):
65+
BUF = c_char * 4
66+
buf = BUF()
67+
with self.assertRaises(AttributeError):
68+
del buf.raw
69+
70+
6471
@need_symbol('c_wchar')
6572
class WStringArrayTestCase(unittest.TestCase):
6673
def test(self):

Lib/sqlite3/test/regression.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,6 +361,10 @@ def callback(*args):
361361
del ref
362362
support.gc_collect()
363363

364+
def CheckDelIsolation_levelSegfault(self):
365+
with self.assertRaises(AttributeError):
366+
del self.con.isolation_level
367+
364368

365369
class UnhashableFunc:
366370
def __hash__(self):

Lib/test/test_io.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2807,6 +2807,11 @@ def test_rwpair_cleared_before_textio(self):
28072807
t2.buddy = t1
28082808
support.gc_collect()
28092809

2810+
def test_del__CHUNK_SIZE_SystemError(self):
2811+
t = self.TextIOWrapper(self.BytesIO(), encoding='ascii')
2812+
with self.assertRaises(AttributeError):
2813+
del t._CHUNK_SIZE
2814+
28102815
maybeRaises = unittest.TestCase.assertRaises
28112816

28122817

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix segfaults and :exc:`SystemError`\ s when deleting certain attributes.
2+
Patch by Zackery Spytz.

Modules/_ctypes/_ctypes.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,10 @@ CharArray_set_raw(CDataObject *self, PyObject *value)
12161216
#if (PY_VERSION_HEX >= 0x02060000)
12171217
Py_buffer view = { 0 };
12181218
#endif
1219+
if (value == NULL) {
1220+
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1221+
return -1;
1222+
}
12191223
if (PyBuffer_Check(value)) {
12201224
size = Py_TYPE(value)->tp_as_buffer->bf_getreadbuffer(value, 0, (void *)&ptr);
12211225
if (size < 0)

Modules/_io/textio.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2593,6 +2593,10 @@ textiowrapper_chunk_size_set(textio *self, PyObject *arg, void *context)
25932593
{
25942594
Py_ssize_t n;
25952595
CHECK_ATTACHED_INT(self);
2596+
if (arg == NULL) {
2597+
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
2598+
return -1;
2599+
}
25962600
n = PyNumber_AsSsize_t(arg, PyExc_TypeError);
25972601
if (n == -1 && PyErr_Occurred())
25982602
return -1;

Modules/_sqlite/connection.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1131,6 +1131,10 @@ static int pysqlite_connection_set_isolation_level(pysqlite_Connection* self, Py
11311131
PyObject* begin_statement;
11321132
char* begin_statement_str;
11331133

1134+
if (isolation_level == NULL) {
1135+
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
1136+
return -1;
1137+
}
11341138
Py_XDECREF(self->isolation_level);
11351139

11361140
if (self->begin_statement) {

Modules/cjkcodecs/multibytecodec.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,10 @@ codecctx_errors_set(MultibyteStatefulCodecContext *self, PyObject *value,
138138
{
139139
PyObject *cb;
140140

141+
if (value == NULL) {
142+
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
143+
return -1;
144+
}
141145
if (!PyString_Check(value)) {
142146
PyErr_SetString(PyExc_TypeError, "errors must be a string");
143147
return -1;

Objects/frameobject.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,10 @@ frame_setlineno(PyFrameObject *f, PyObject* p_new_lineno)
118118
int blockstack_top = 0; /* (ditto) */
119119
unsigned char setup_op = 0; /* (ditto) */
120120

121+
if (p_new_lineno == NULL) {
122+
PyErr_SetString(PyExc_AttributeError, "cannot delete attribute");
123+
return -1;
124+
}
121125
/* f_lineno must be an integer. */
122126
if (!PyInt_Check(p_new_lineno)) {
123127
PyErr_SetString(PyExc_ValueError,

0 commit comments

Comments
 (0)