Kaydet (Commit) 0847c5c6 authored tarafından Guido van Rossum's avatar Guido van Rossum

execve(), spawnve(): add some extra sanity checking to env;

PyMapping_Check() doesn't guarantee that PyMapping_Size() won't raise
an exception, nor that keys and values are lists.

Also folded some long lines and did a little whitespace normalization.

Probably a 2.2 backport candidate.
üst 3bbc0eea
...@@ -2314,11 +2314,13 @@ posix_execve(PyObject *self, PyObject *args) ...@@ -2314,11 +2314,13 @@ posix_execve(PyObject *self, PyObject *args)
getitem = PyTuple_GetItem; getitem = PyTuple_GetItem;
} }
else { else {
PyErr_SetString(PyExc_TypeError, "execve() arg 2 must be a tuple or list"); PyErr_SetString(PyExc_TypeError,
"execve() arg 2 must be a tuple or list");
goto fail_0; goto fail_0;
} }
if (!PyMapping_Check(env)) { if (!PyMapping_Check(env)) {
PyErr_SetString(PyExc_TypeError, "execve() arg 3 must be a mapping object"); PyErr_SetString(PyExc_TypeError,
"execve() arg 3 must be a mapping object");
goto fail_0; goto fail_0;
} }
...@@ -2347,6 +2349,8 @@ posix_execve(PyObject *self, PyObject *args) ...@@ -2347,6 +2349,8 @@ posix_execve(PyObject *self, PyObject *args)
argvlist[argc] = NULL; argvlist[argc] = NULL;
i = PyMapping_Size(env); i = PyMapping_Size(env);
if (i < 0)
goto fail_1;
envlist = PyMem_NEW(char *, i + 1); envlist = PyMem_NEW(char *, i + 1);
if (envlist == NULL) { if (envlist == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
...@@ -2357,6 +2361,11 @@ posix_execve(PyObject *self, PyObject *args) ...@@ -2357,6 +2361,11 @@ posix_execve(PyObject *self, PyObject *args)
vals = PyMapping_Values(env); vals = PyMapping_Values(env);
if (!keys || !vals) if (!keys || !vals)
goto fail_2; goto fail_2;
if (!PyList_Check(keys) || !PyList_Check(vals)) {
PyErr_SetString(PyExc_TypeError,
"execve(): env.keys() or env.values() is not a list");
goto fail_2;
}
for (pos = 0; pos < i; pos++) { for (pos = 0; pos < i; pos++) {
char *p, *k, *v; char *p, *k, *v;
...@@ -2367,8 +2376,14 @@ posix_execve(PyObject *self, PyObject *args) ...@@ -2367,8 +2376,14 @@ posix_execve(PyObject *self, PyObject *args)
if (!key || !val) if (!key || !val)
goto fail_2; goto fail_2;
if (!PyArg_Parse(key, "s;execve() arg 3 contains a non-string key", &k) || if (!PyArg_Parse(
!PyArg_Parse(val, "s;execve() arg 3 contains a non-string value", &v)) key,
"s;execve() arg 3 contains a non-string key",
&k) ||
!PyArg_Parse(
val,
"s;execve() arg 3 contains a non-string value",
&v))
{ {
goto fail_2; goto fail_2;
} }
...@@ -2402,15 +2417,15 @@ posix_execve(PyObject *self, PyObject *args) ...@@ -2402,15 +2417,15 @@ posix_execve(PyObject *self, PyObject *args)
(void) posix_error(); (void) posix_error();
fail_2: fail_2:
while (--envc >= 0) while (--envc >= 0)
PyMem_DEL(envlist[envc]); PyMem_DEL(envlist[envc]);
PyMem_DEL(envlist); PyMem_DEL(envlist);
fail_1: fail_1:
free_string_array(argvlist,lastarg); free_string_array(argvlist, lastarg);
Py_XDECREF(vals); Py_XDECREF(vals);
Py_XDECREF(keys); Py_XDECREF(keys);
fail_0: fail_0:
PyMem_Free(path); PyMem_Free(path);
return NULL; return NULL;
} }
...@@ -2452,7 +2467,8 @@ posix_spawnv(PyObject *self, PyObject *args) ...@@ -2452,7 +2467,8 @@ posix_spawnv(PyObject *self, PyObject *args)
getitem = PyTuple_GetItem; getitem = PyTuple_GetItem;
} }
else { else {
PyErr_SetString(PyExc_TypeError, "spawnv() arg 2 must be a tuple or list"); PyErr_SetString(PyExc_TypeError,
"spawnv() arg 2 must be a tuple or list");
PyMem_Free(path); PyMem_Free(path);
return NULL; return NULL;
} }
...@@ -2467,8 +2483,9 @@ posix_spawnv(PyObject *self, PyObject *args) ...@@ -2467,8 +2483,9 @@ posix_spawnv(PyObject *self, PyObject *args)
Py_FileSystemDefaultEncoding, Py_FileSystemDefaultEncoding,
&argvlist[i])) { &argvlist[i])) {
free_string_array(argvlist, i); free_string_array(argvlist, i);
PyErr_SetString(PyExc_TypeError, PyErr_SetString(
"spawnv() arg 2 must contain only strings"); PyExc_TypeError,
"spawnv() arg 2 must contain only strings");
PyMem_Free(path); PyMem_Free(path);
return NULL; return NULL;
} }
...@@ -2541,11 +2558,13 @@ posix_spawnve(PyObject *self, PyObject *args) ...@@ -2541,11 +2558,13 @@ posix_spawnve(PyObject *self, PyObject *args)
getitem = PyTuple_GetItem; getitem = PyTuple_GetItem;
} }
else { else {
PyErr_SetString(PyExc_TypeError, "spawnve() arg 2 must be a tuple or list"); PyErr_SetString(PyExc_TypeError,
"spawnve() arg 2 must be a tuple or list");
goto fail_0; goto fail_0;
} }
if (!PyMapping_Check(env)) { if (!PyMapping_Check(env)) {
PyErr_SetString(PyExc_TypeError, "spawnve() arg 3 must be a mapping object"); PyErr_SetString(PyExc_TypeError,
"spawnve() arg 3 must be a mapping object");
goto fail_0; goto fail_0;
} }
...@@ -2556,7 +2575,7 @@ posix_spawnve(PyObject *self, PyObject *args) ...@@ -2556,7 +2575,7 @@ posix_spawnve(PyObject *self, PyObject *args)
} }
for (i = 0; i < argc; i++) { for (i = 0; i < argc; i++) {
if (!PyArg_Parse((*getitem)(argv, i), if (!PyArg_Parse((*getitem)(argv, i),
"et;spawnve() arg 2 must contain only strings", "et;spawnve() arg 2 must contain only strings",
Py_FileSystemDefaultEncoding, Py_FileSystemDefaultEncoding,
&argvlist[i])) &argvlist[i]))
{ {
...@@ -2568,6 +2587,8 @@ posix_spawnve(PyObject *self, PyObject *args) ...@@ -2568,6 +2587,8 @@ posix_spawnve(PyObject *self, PyObject *args)
argvlist[argc] = NULL; argvlist[argc] = NULL;
i = PyMapping_Size(env); i = PyMapping_Size(env);
if (i < 0)
goto fail_1;
envlist = PyMem_NEW(char *, i + 1); envlist = PyMem_NEW(char *, i + 1);
if (envlist == NULL) { if (envlist == NULL) {
PyErr_NoMemory(); PyErr_NoMemory();
...@@ -2578,6 +2599,11 @@ posix_spawnve(PyObject *self, PyObject *args) ...@@ -2578,6 +2599,11 @@ posix_spawnve(PyObject *self, PyObject *args)
vals = PyMapping_Values(env); vals = PyMapping_Values(env);
if (!keys || !vals) if (!keys || !vals)
goto fail_2; goto fail_2;
if (!PyList_Check(keys) || !PyList_Check(vals)) {
PyErr_SetString(PyExc_TypeError,
"spawnve(): env.keys() or env.values() is not a list");
goto fail_2;
}
for (pos = 0; pos < i; pos++) { for (pos = 0; pos < i; pos++) {
char *p, *k, *v; char *p, *k, *v;
...@@ -2588,8 +2614,14 @@ posix_spawnve(PyObject *self, PyObject *args) ...@@ -2588,8 +2614,14 @@ posix_spawnve(PyObject *self, PyObject *args)
if (!key || !val) if (!key || !val)
goto fail_2; goto fail_2;
if (!PyArg_Parse(key, "s;spawnve() arg 3 contains a non-string key", &k) || if (!PyArg_Parse(
!PyArg_Parse(val, "s;spawnve() arg 3 contains a non-string value", &v)) key,
"s;spawnve() arg 3 contains a non-string key",
&k) ||
!PyArg_Parse(
val,
"s;spawnve() arg 3 contains a non-string value",
&v))
{ {
goto fail_2; goto fail_2;
} }
...@@ -2626,11 +2658,11 @@ posix_spawnve(PyObject *self, PyObject *args) ...@@ -2626,11 +2658,11 @@ posix_spawnve(PyObject *self, PyObject *args)
res = Py_BuildValue("L", (LONG_LONG) spawnval); res = Py_BuildValue("L", (LONG_LONG) spawnval);
#endif #endif
fail_2: fail_2:
while (--envc >= 0) while (--envc >= 0)
PyMem_DEL(envlist[envc]); PyMem_DEL(envlist[envc]);
PyMem_DEL(envlist); PyMem_DEL(envlist);
fail_1: fail_1:
free_string_array(argvlist, lastarg); free_string_array(argvlist, lastarg);
Py_XDECREF(vals); Py_XDECREF(vals);
Py_XDECREF(keys); Py_XDECREF(keys);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment