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

Skip to content

Commit 0847c5c

Browse files
committed
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.
1 parent 3bbc0ee commit 0847c5c

1 file changed

Lines changed: 50 additions & 18 deletions

File tree

Modules/posixmodule.c

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2314,11 +2314,13 @@ posix_execve(PyObject *self, PyObject *args)
23142314
getitem = PyTuple_GetItem;
23152315
}
23162316
else {
2317-
PyErr_SetString(PyExc_TypeError, "execve() arg 2 must be a tuple or list");
2317+
PyErr_SetString(PyExc_TypeError,
2318+
"execve() arg 2 must be a tuple or list");
23182319
goto fail_0;
23192320
}
23202321
if (!PyMapping_Check(env)) {
2321-
PyErr_SetString(PyExc_TypeError, "execve() arg 3 must be a mapping object");
2322+
PyErr_SetString(PyExc_TypeError,
2323+
"execve() arg 3 must be a mapping object");
23222324
goto fail_0;
23232325
}
23242326

@@ -2347,6 +2349,8 @@ posix_execve(PyObject *self, PyObject *args)
23472349
argvlist[argc] = NULL;
23482350

23492351
i = PyMapping_Size(env);
2352+
if (i < 0)
2353+
goto fail_1;
23502354
envlist = PyMem_NEW(char *, i + 1);
23512355
if (envlist == NULL) {
23522356
PyErr_NoMemory();
@@ -2357,6 +2361,11 @@ posix_execve(PyObject *self, PyObject *args)
23572361
vals = PyMapping_Values(env);
23582362
if (!keys || !vals)
23592363
goto fail_2;
2364+
if (!PyList_Check(keys) || !PyList_Check(vals)) {
2365+
PyErr_SetString(PyExc_TypeError,
2366+
"execve(): env.keys() or env.values() is not a list");
2367+
goto fail_2;
2368+
}
23602369

23612370
for (pos = 0; pos < i; pos++) {
23622371
char *p, *k, *v;
@@ -2367,8 +2376,14 @@ posix_execve(PyObject *self, PyObject *args)
23672376
if (!key || !val)
23682377
goto fail_2;
23692378

2370-
if (!PyArg_Parse(key, "s;execve() arg 3 contains a non-string key", &k) ||
2371-
!PyArg_Parse(val, "s;execve() arg 3 contains a non-string value", &v))
2379+
if (!PyArg_Parse(
2380+
key,
2381+
"s;execve() arg 3 contains a non-string key",
2382+
&k) ||
2383+
!PyArg_Parse(
2384+
val,
2385+
"s;execve() arg 3 contains a non-string value",
2386+
&v))
23722387
{
23732388
goto fail_2;
23742389
}
@@ -2402,15 +2417,15 @@ posix_execve(PyObject *self, PyObject *args)
24022417

24032418
(void) posix_error();
24042419

2405-
fail_2:
2420+
fail_2:
24062421
while (--envc >= 0)
24072422
PyMem_DEL(envlist[envc]);
24082423
PyMem_DEL(envlist);
2409-
fail_1:
2410-
free_string_array(argvlist,lastarg);
2424+
fail_1:
2425+
free_string_array(argvlist, lastarg);
24112426
Py_XDECREF(vals);
24122427
Py_XDECREF(keys);
2413-
fail_0:
2428+
fail_0:
24142429
PyMem_Free(path);
24152430
return NULL;
24162431
}
@@ -2452,7 +2467,8 @@ posix_spawnv(PyObject *self, PyObject *args)
24522467
getitem = PyTuple_GetItem;
24532468
}
24542469
else {
2455-
PyErr_SetString(PyExc_TypeError, "spawnv() arg 2 must be a tuple or list");
2470+
PyErr_SetString(PyExc_TypeError,
2471+
"spawnv() arg 2 must be a tuple or list");
24562472
PyMem_Free(path);
24572473
return NULL;
24582474
}
@@ -2467,8 +2483,9 @@ posix_spawnv(PyObject *self, PyObject *args)
24672483
Py_FileSystemDefaultEncoding,
24682484
&argvlist[i])) {
24692485
free_string_array(argvlist, i);
2470-
PyErr_SetString(PyExc_TypeError,
2471-
"spawnv() arg 2 must contain only strings");
2486+
PyErr_SetString(
2487+
PyExc_TypeError,
2488+
"spawnv() arg 2 must contain only strings");
24722489
PyMem_Free(path);
24732490
return NULL;
24742491
}
@@ -2541,11 +2558,13 @@ posix_spawnve(PyObject *self, PyObject *args)
25412558
getitem = PyTuple_GetItem;
25422559
}
25432560
else {
2544-
PyErr_SetString(PyExc_TypeError, "spawnve() arg 2 must be a tuple or list");
2561+
PyErr_SetString(PyExc_TypeError,
2562+
"spawnve() arg 2 must be a tuple or list");
25452563
goto fail_0;
25462564
}
25472565
if (!PyMapping_Check(env)) {
2548-
PyErr_SetString(PyExc_TypeError, "spawnve() arg 3 must be a mapping object");
2566+
PyErr_SetString(PyExc_TypeError,
2567+
"spawnve() arg 3 must be a mapping object");
25492568
goto fail_0;
25502569
}
25512570

@@ -2556,7 +2575,7 @@ posix_spawnve(PyObject *self, PyObject *args)
25562575
}
25572576
for (i = 0; i < argc; i++) {
25582577
if (!PyArg_Parse((*getitem)(argv, i),
2559-
"et;spawnve() arg 2 must contain only strings",
2578+
"et;spawnve() arg 2 must contain only strings",
25602579
Py_FileSystemDefaultEncoding,
25612580
&argvlist[i]))
25622581
{
@@ -2568,6 +2587,8 @@ posix_spawnve(PyObject *self, PyObject *args)
25682587
argvlist[argc] = NULL;
25692588

25702589
i = PyMapping_Size(env);
2590+
if (i < 0)
2591+
goto fail_1;
25712592
envlist = PyMem_NEW(char *, i + 1);
25722593
if (envlist == NULL) {
25732594
PyErr_NoMemory();
@@ -2578,6 +2599,11 @@ posix_spawnve(PyObject *self, PyObject *args)
25782599
vals = PyMapping_Values(env);
25792600
if (!keys || !vals)
25802601
goto fail_2;
2602+
if (!PyList_Check(keys) || !PyList_Check(vals)) {
2603+
PyErr_SetString(PyExc_TypeError,
2604+
"spawnve(): env.keys() or env.values() is not a list");
2605+
goto fail_2;
2606+
}
25812607

25822608
for (pos = 0; pos < i; pos++) {
25832609
char *p, *k, *v;
@@ -2588,8 +2614,14 @@ posix_spawnve(PyObject *self, PyObject *args)
25882614
if (!key || !val)
25892615
goto fail_2;
25902616

2591-
if (!PyArg_Parse(key, "s;spawnve() arg 3 contains a non-string key", &k) ||
2592-
!PyArg_Parse(val, "s;spawnve() arg 3 contains a non-string value", &v))
2617+
if (!PyArg_Parse(
2618+
key,
2619+
"s;spawnve() arg 3 contains a non-string key",
2620+
&k) ||
2621+
!PyArg_Parse(
2622+
val,
2623+
"s;spawnve() arg 3 contains a non-string value",
2624+
&v))
25932625
{
25942626
goto fail_2;
25952627
}
@@ -2626,11 +2658,11 @@ posix_spawnve(PyObject *self, PyObject *args)
26262658
res = Py_BuildValue("L", (LONG_LONG) spawnval);
26272659
#endif
26282660

2629-
fail_2:
2661+
fail_2:
26302662
while (--envc >= 0)
26312663
PyMem_DEL(envlist[envc]);
26322664
PyMem_DEL(envlist);
2633-
fail_1:
2665+
fail_1:
26342666
free_string_array(argvlist, lastarg);
26352667
Py_XDECREF(vals);
26362668
Py_XDECREF(keys);

0 commit comments

Comments
 (0)