summaryrefslogtreecommitdiff
path: root/Modules/resource.c
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-02-04 12:45:46 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2013-02-04 12:45:46 +0200
commita07a8b4f18c9f3a6eb14080dcbef4dc23a2d3f3b (patch)
tree90dbe40b508a126e0ee9f20b2297995bd5cd67ff /Modules/resource.c
parentf727c31133822b3e39f851fcca9d3239f389c054 (diff)
downloadcpython-git-a07a8b4f18c9f3a6eb14080dcbef4dc23a2d3f3b.tar.gz
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 9993b93c9e..53a6c3ea43 100644
--- a/Modules/resource.c
+++ b/Modules/resource.c
@@ -145,10 +145,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) {
@@ -157,23 +156,36 @@ 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 = PyInt_AsLong(curobj);
if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
- return NULL;
+ goto error;
rl.rlim_max = PyInt_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_Check(curobj) ?
PyLong_AsLongLong(curobj) : PyInt_AsLong(curobj);
if (rl.rlim_cur == (rlim_t)-1 && PyErr_Occurred())
- return NULL;
+ goto error;
rl.rlim_max = PyLong_Check(maxobj) ?
PyLong_AsLongLong(maxobj) : PyInt_AsLong(maxobj);
if (rl.rlim_max == (rlim_t)-1 && PyErr_Occurred())
- return NULL;
+ goto error;
#endif
rl.rlim_cur = rl.rlim_cur & RLIM_INFINITY;
@@ -187,10 +199,15 @@ resource_setrlimit(PyObject *self, PyObject *args)
"not allowed to raise maximum limit");
else
PyErr_SetFromErrno(ResourceError);
- return NULL;
+ goto error;
}
+ Py_DECREF(limits);
Py_INCREF(Py_None);
return Py_None;
+
+ error:
+ Py_DECREF(limits);
+ return NULL;
}
static PyObject *