summaryrefslogtreecommitdiffstats
path: root/Modules/resource.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-02-04 10:54:04 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-02-04 10:54:04 (GMT)
commit1d0bb9c8f97b0f4fc6717f73555576f523965e42 (patch)
treec6fe486557438be079c676b0fb2fc3b80af8d772 /Modules/resource.c
parentcfe34744e3c785a56525ab8a9473336206f5854d (diff)
parent19c4e0df29234355074fe7ec67857f0a0b7e0a18 (diff)
downloadcpython-1d0bb9c8f97b0f4fc6717f73555576f523965e42.zip
cpython-1d0bb9c8f97b0f4fc6717f73555576f523965e42.tar.gz
cpython-1d0bb9c8f97b0f4fc6717f73555576f523965e42.tar.bz2
Issue #6083: Fix multiple segmentation faults occured when PyArg_ParseTuple
parses nested mutating sequence.
Diffstat (limited to 'Modules/resource.c')
-rw-r--r--Modules/resource.c33
1 files changed, 25 insertions, 8 deletions
diff --git a/Modules/resource.c b/Modules/resource.c
index 1aed497..b294a8c 100644
--- a/Modules/resource.c
+++ b/Modules/resource.c
@@ -142,10 +142,9 @@ resource_setrlimit(PyObject *self, PyObject *args)
{
struct rlimit rl;
int resource;
- PyObject *curobj, *maxobj;
+ PyObject *limits, *curobj, *maxobj;
- if (!PyArg_ParseTuple(args, "i(OO):setrlimit",
- &resource, &curobj, &maxobj))
+ if (!PyArg_ParseTuple(args, "iO:setrlimit", &resource, &limits))
return NULL;
if (resource < 0 || resource >= RLIM_NLIMITS) {
@@ -154,21 +153,34 @@ resource_setrlimit(PyObject *self, PyObject *args)
return NULL;
}
+ limits = PySequence_Tuple(limits);
+ if (!limits)
+ /* Here limits is a borrowed reference */
+ return NULL;
+
+ if (PyTuple_GET_SIZE(limits) != 2) {
+ PyErr_SetString(PyExc_ValueError,
+ "expected a tuple of 2 integers");
+ goto error;
+ }
+ curobj = PyTuple_GET_ITEM(limits, 0);
+ maxobj = PyTuple_GET_ITEM(limits, 1);
+
#if !defined(HAVE_LARGEFILE_SUPPORT)
rl.rlim_cur = PyLong_AsLong(curobj);
if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
- return NULL;
+ goto error;
rl.rlim_max = PyLong_AsLong(maxobj);
if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
- return NULL;
+ goto error;
#else
/* The limits are probably bigger than a long */
rl.rlim_cur = PyLong_AsLongLong(curobj);
if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
- return NULL;
+ goto error;
rl.rlim_max = PyLong_AsLongLong(maxobj);
if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
- return NULL;
+ goto error;
#endif
rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
@@ -182,10 +194,15 @@ resource_setrlimit(PyObject *self, PyObject *args)
"not allowed to raise maximum limit");
else
PyErr_SetFromErrno(PyExc_OSError);
- return NULL;
+ goto error;
}
+ Py_DECREF(limits);
Py_INCREF(Py_None);
return Py_None;
+
+ error:
+ Py_DECREF(limits);
+ return NULL;
}
static PyObject *