Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 19c4e0d

Browse files
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
parses nested mutating sequence.
1 parent 64359d2 commit 19c4e0d

7 files changed

Lines changed: 117 additions & 14 deletions

File tree

Lib/ctypes/test/test_returnfuncptrs.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,33 @@ def test_without_prototype(self):
3333
self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0)
3434
self.assertRaises(TypeError, strchr, b"abcdef")
3535

36+
def test_from_dll(self):
37+
dll = CDLL(_ctypes_test.__file__)
38+
# _CFuncPtr instances are now callable with a tuple argument
39+
# which denotes a function name and a dll:
40+
strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(("strchr", dll))
41+
self.assertTrue(strchr(b"abcdef", b"b"), "bcdef")
42+
self.assertEqual(strchr(b"abcdef", b"x"), None)
43+
self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0)
44+
self.assertRaises(TypeError, strchr, b"abcdef")
45+
46+
# Issue 6083: Reference counting bug
47+
def test_test_from_dll_refcount(self):
48+
class BadSequence(tuple):
49+
def __getitem__(self, key):
50+
if key == 0:
51+
return "strchr"
52+
if key == 1:
53+
return CDLL(_ctypes_test.__file__)
54+
raise IndexError
55+
56+
# _CFuncPtr instances are now callable with a tuple argument
57+
# which denotes a function name and a dll:
58+
strchr = CFUNCTYPE(c_char_p, c_char_p, c_char)(BadSequence(("strchr", CDLL(_ctypes_test.__file__))))
59+
self.assertTrue(strchr(b"abcdef", b"b"), "bcdef")
60+
self.assertEqual(strchr(b"abcdef", b"x"), None)
61+
self.assertRaises(ArgumentError, strchr, b"abcdef", 3.0)
62+
self.assertRaises(TypeError, strchr, b"abcdef")
63+
3664
if __name__ == "__main__":
3765
unittest.main()

Lib/test/test_functools.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,25 @@ def test_pickle(self):
179179
f_copy = pickle.loads(pickle.dumps(f))
180180
self.assertEqual(signature(f), signature(f_copy))
181181

182+
# Issue 6083: Reference counting bug
183+
def test_setstate_refcount(self):
184+
class BadSequence:
185+
def __len__(self):
186+
return 4
187+
def __getitem__(self, key):
188+
if key == 0:
189+
return max
190+
elif key == 1:
191+
return tuple(range(1000000))
192+
elif key in (2, 3):
193+
return {}
194+
raise IndexError
195+
196+
f = self.thetype(object)
197+
self.assertRaisesRegex(SystemError,
198+
"new style getargs format but argument is not a tuple",
199+
f.__setstate__, BadSequence())
200+
182201
class PartialSubclass(functools.partial):
183202
pass
184203

@@ -195,6 +214,7 @@ def test_repr(self): pass
195214

196215
# the python version isn't picklable
197216
def test_pickle(self): pass
217+
def test_setstate_refcount(self): pass
198218

199219
class TestUpdateWrapper(unittest.TestCase):
200220

Lib/test/test_resource.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,23 @@ def test_getrusage(self):
107107
except (ValueError, AttributeError):
108108
pass
109109

110+
# Issue 6083: Reference counting bug
111+
def test_setrusage_refcount(self):
112+
try:
113+
limits = resource.getrlimit(resource.RLIMIT_CPU)
114+
except AttributeError:
115+
pass
116+
else:
117+
class BadSequence:
118+
def __len__(self):
119+
return 2
120+
def __getitem__(self, key):
121+
if key in (0, 1):
122+
return len(tuple(range(1000000)))
123+
raise IndexError
124+
125+
resource.setrlimit(resource.RLIMIT_CPU, BadSequence())
126+
110127
def test_main(verbose=None):
111128
support.run_unittest(ResourceTest)
112129

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,9 @@ Core and Builtins
212212
Library
213213
-------
214214

215+
- Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
216+
parses nested mutating sequence.
217+
215218
- Issue #17106: Fix a segmentation fault in io.TextIOWrapper when an underlying
216219
stream or a decoder produces data of an unexpected type (i.e. when
217220
io.TextIOWrapper initialized with text stream or use bytes-to-bytes codec).

Modules/_ctypes/_ctypes.c

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3184,23 +3184,37 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
31843184
{
31853185
char *name;
31863186
int (* address)(void);
3187+
PyObject *ftuple;
31873188
PyObject *dll;
31883189
PyObject *obj;
31893190
PyCFuncPtrObject *self;
31903191
void *handle;
31913192
PyObject *paramflags = NULL;
31923193

3193-
if (!PyArg_ParseTuple(args, "(O&O)|O", _get_name, &name, &dll, &paramflags))
3194+
if (!PyArg_ParseTuple(args, "O|O", &ftuple, &paramflags))
31943195
return NULL;
31953196
if (paramflags == Py_None)
31963197
paramflags = NULL;
31973198

3199+
ftuple = PySequence_Tuple(ftuple);
3200+
if (!ftuple)
3201+
/* Here ftuple is a borrowed reference */
3202+
return NULL;
3203+
3204+
if (!PyArg_ParseTuple(ftuple, "O&O", _get_name, &name, &dll)) {
3205+
Py_DECREF(ftuple);
3206+
return NULL;
3207+
}
3208+
31983209
obj = PyObject_GetAttrString(dll, "_handle");
3199-
if (!obj)
3210+
if (!obj) {
3211+
Py_DECREF(ftuple);
32003212
return NULL;
3213+
}
32013214
if (!PyLong_Check(obj)) {
32023215
PyErr_SetString(PyExc_TypeError,
32033216
"the _handle attribute of the second argument must be an integer");
3217+
Py_DECREF(ftuple);
32043218
Py_DECREF(obj);
32053219
return NULL;
32063220
}
@@ -3209,6 +3223,7 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
32093223
if (PyErr_Occurred()) {
32103224
PyErr_SetString(PyExc_ValueError,
32113225
"could not convert the _handle attribute to a pointer");
3226+
Py_DECREF(ftuple);
32123227
return NULL;
32133228
}
32143229

@@ -3223,6 +3238,7 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
32233238
PyErr_Format(PyExc_AttributeError,
32243239
"function ordinal %d not found",
32253240
(WORD)(size_t)name);
3241+
Py_DECREF(ftuple);
32263242
return NULL;
32273243
}
32283244
#else
@@ -3236,9 +3252,12 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
32363252
#else
32373253
PyErr_SetString(PyExc_AttributeError, ctypes_dlerror());
32383254
#endif
3255+
Py_DECREF(ftuple);
32393256
return NULL;
32403257
}
32413258
#endif
3259+
Py_INCREF(dll); /* for KeepRef */
3260+
Py_DECREF(ftuple);
32423261
if (!_validate_paramflags(type, paramflags))
32433262
return NULL;
32443263

@@ -3251,7 +3270,6 @@ PyCFuncPtr_FromDll(PyTypeObject *type, PyObject *args, PyObject *kwds)
32513270

32523271
*(void **)self->b_ptr = address;
32533272

3254-
Py_INCREF((PyObject *)dll); /* for KeepRef */
32553273
if (-1 == KeepRef((CDataObject *)self, 0, dll)) {
32563274
Py_DECREF((PyObject *)self);
32573275
return NULL;

Modules/_functoolsmodule.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ partial_reduce(partialobject *pto, PyObject *unused)
254254
}
255255

256256
static PyObject *
257-
partial_setstate(partialobject *pto, PyObject *args)
257+
partial_setstate(partialobject *pto, PyObject *state)
258258
{
259259
PyObject *fn, *fnargs, *kw, *dict;
260-
if (!PyArg_ParseTuple(args, "(OOOO):__setstate__",
260+
if (!PyArg_ParseTuple(state, "OOOO",
261261
&fn, &fnargs, &kw, &dict))
262262
return NULL;
263263
Py_XDECREF(pto->fn);
@@ -281,7 +281,7 @@ partial_setstate(partialobject *pto, PyObject *args)
281281

282282
static PyMethodDef partial_methods[] = {
283283
{"__reduce__", (PyCFunction)partial_reduce, METH_NOARGS},
284-
{"__setstate__", (PyCFunction)partial_setstate, METH_VARARGS},
284+
{"__setstate__", (PyCFunction)partial_setstate, METH_O},
285285
{NULL, NULL} /* sentinel */
286286
};
287287

Modules/resource.c

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -144,10 +144,9 @@ resource_setrlimit(PyObject *self, PyObject *args)
144144
{
145145
struct rlimit rl;
146146
int resource;
147-
PyObject *curobj, *maxobj;
147+
PyObject *limits, *curobj, *maxobj;
148148

149-
if (!PyArg_ParseTuple(args, "i(OO):setrlimit",
150-
&resource, &curobj, &maxobj))
149+
if (!PyArg_ParseTuple(args, "iO:setrlimit", &resource, &limits))
151150
return NULL;
152151

153152
if (resource < 0 || resource >= RLIM_NLIMITS) {
@@ -156,21 +155,34 @@ resource_setrlimit(PyObject *self, PyObject *args)
156155
return NULL;
157156
}
158157

158+
limits = PySequence_Tuple(limits);
159+
if (!limits)
160+
/* Here limits is a borrowed reference */
161+
return NULL;
162+
163+
if (PyTuple_GET_SIZE(limits) != 2) {
164+
PyErr_SetString(PyExc_ValueError,
165+
"expected a tuple of 2 integers");
166+
goto error;
167+
}
168+
curobj = PyTuple_GET_ITEM(limits, 0);
169+
maxobj = PyTuple_GET_ITEM(limits, 1);
170+
159171
#if !defined(HAVE_LARGEFILE_SUPPORT)
160172
rl.rlim_cur = PyLong_AsLong(curobj);
161173
if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
162-
return NULL;
174+
goto error;
163175
rl.rlim_max = PyLong_AsLong(maxobj);
164176
if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
165-
return NULL;
177+
goto error;
166178
#else
167179
/* The limits are probably bigger than a long */
168180
rl.rlim_cur = PyLong_AsLongLong(curobj);
169181
if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
170-
return NULL;
182+
goto error;
171183
rl.rlim_max = PyLong_AsLongLong(maxobj);
172184
if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
173-
return NULL;
185+
goto error;
174186
#endif
175187

176188
rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
@@ -184,10 +196,15 @@ resource_setrlimit(PyObject *self, PyObject *args)
184196
"not allowed to raise maximum limit");
185197
else
186198
PyErr_SetFromErrno(ResourceError);
187-
return NULL;
199+
goto error;
188200
}
201+
Py_DECREF(limits);
189202
Py_INCREF(Py_None);
190203
return Py_None;
204+
205+
error:
206+
Py_DECREF(limits);
207+
return NULL;
191208
}
192209

193210
static PyObject *

0 commit comments

Comments
 (0)