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

Skip to content

Commit bce2626

Browse files
committed
Issue #28732: Raise ValueError when argv[0] is empty
2 parents 859fd7b + 93ff872 commit bce2626

2 files changed

Lines changed: 50 additions & 2 deletions

File tree

Lib/test/test_os.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,8 +1481,16 @@ def test_execvpe_with_bad_program(self):
14811481
self.assertRaises(OSError, os.execvpe, 'no such app-',
14821482
['no such app-'], None)
14831483

1484+
def test_execv_with_bad_arglist(self):
1485+
self.assertRaises(ValueError, os.execv, 'notepad', ())
1486+
self.assertRaises(ValueError, os.execv, 'notepad', [])
1487+
self.assertRaises(ValueError, os.execv, 'notepad', ('',))
1488+
self.assertRaises(ValueError, os.execv, 'notepad', [''])
1489+
14841490
def test_execvpe_with_bad_arglist(self):
14851491
self.assertRaises(ValueError, os.execvpe, 'notepad', [], None)
1492+
self.assertRaises(ValueError, os.execvpe, 'notepad', [], {})
1493+
self.assertRaises(ValueError, os.execvpe, 'notepad', [''], {})
14861494

14871495
@unittest.skipUnless(hasattr(os, '_execvpe'),
14881496
"No internal os._execvpe function to test.")
@@ -2325,23 +2333,29 @@ def test_spawnve_bytes(self):
23252333
def test_spawnl_noargs(self):
23262334
args = self.create_args()
23272335
self.assertRaises(ValueError, os.spawnl, os.P_NOWAIT, args[0])
2336+
self.assertRaises(ValueError, os.spawnl, os.P_NOWAIT, args[0], '')
23282337

23292338
@requires_os_func('spawnle')
2330-
def test_spawnl_noargs(self):
2339+
def test_spawnle_noargs(self):
23312340
args = self.create_args()
23322341
self.assertRaises(ValueError, os.spawnle, os.P_NOWAIT, args[0], {})
2342+
self.assertRaises(ValueError, os.spawnle, os.P_NOWAIT, args[0], '', {})
23332343

23342344
@requires_os_func('spawnv')
23352345
def test_spawnv_noargs(self):
23362346
args = self.create_args()
23372347
self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, args[0], ())
23382348
self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, args[0], [])
2349+
self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, args[0], ('',))
2350+
self.assertRaises(ValueError, os.spawnv, os.P_NOWAIT, args[0], [''])
23392351

23402352
@requires_os_func('spawnve')
2341-
def test_spawnv_noargs(self):
2353+
def test_spawnve_noargs(self):
23422354
args = self.create_args()
23432355
self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], (), {})
23442356
self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], [], {})
2357+
self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], ('',), {})
2358+
self.assertRaises(ValueError, os.spawnve, os.P_NOWAIT, args[0], [''], {})
23452359

23462360
# The introduction of this TestCase caused at least two different errors on
23472361
# *nix buildbots. Temporarily skip this to let the buildbots move along.

Modules/posixmodule.c

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4916,12 +4916,20 @@ os_execv_impl(PyObject *module, path_t *path, PyObject *argv)
49164916
if (argvlist == NULL) {
49174917
return NULL;
49184918
}
4919+
if (!argvlist[0][0]) {
4920+
PyErr_SetString(PyExc_ValueError,
4921+
"execv() arg 2 first element cannot be empty");
4922+
free_string_array(argvlist, argc);
4923+
return NULL;
4924+
}
49194925

4926+
_Py_BEGIN_SUPPRESS_IPH
49204927
#ifdef HAVE_WEXECV
49214928
_wexecv(path->wide, argvlist);
49224929
#else
49234930
execv(path->narrow, argvlist);
49244931
#endif
4932+
_Py_END_SUPPRESS_IPH
49254933

49264934
/* If we get here it's definitely an error */
49274935

@@ -4961,6 +4969,11 @@ os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
49614969
goto fail;
49624970
}
49634971
argc = PySequence_Size(argv);
4972+
if (argc < 1) {
4973+
PyErr_SetString(PyExc_ValueError, "execve: argv must not be empty");
4974+
return NULL;
4975+
}
4976+
49644977
if (!PyMapping_Check(env)) {
49654978
PyErr_SetString(PyExc_TypeError,
49664979
"execve: environment must be a mapping object");
@@ -4971,11 +4984,17 @@ os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
49714984
if (argvlist == NULL) {
49724985
goto fail;
49734986
}
4987+
if (!argvlist[0][0]) {
4988+
PyErr_SetString(PyExc_ValueError,
4989+
"execve: argv first element cannot be empty");
4990+
goto fail;
4991+
}
49744992

49754993
envlist = parse_envlist(env, &envc);
49764994
if (envlist == NULL)
49774995
goto fail;
49784996

4997+
_Py_BEGIN_SUPPRESS_IPH
49794998
#ifdef HAVE_FEXECVE
49804999
if (path->fd > -1)
49815000
fexecve(path->fd, argvlist, envlist);
@@ -4986,6 +5005,7 @@ os_execve_impl(PyObject *module, path_t *path, PyObject *argv, PyObject *env)
49865005
#else
49875006
execve(path->narrow, argvlist, envlist);
49885007
#endif
5008+
_Py_END_SUPPRESS_IPH
49895009

49905010
/* If we get here it's definitely an error */
49915011

@@ -5061,6 +5081,13 @@ os_spawnv_impl(PyObject *module, int mode, path_t *path, PyObject *argv)
50615081
"spawnv() arg 2 must contain only strings");
50625082
return NULL;
50635083
}
5084+
if (i == 0 && !argvlist[0][0]) {
5085+
free_string_array(argvlist, i);
5086+
PyErr_SetString(
5087+
PyExc_ValueError,
5088+
"spawnv() arg 2 first element cannot be empty");
5089+
return NULL;
5090+
}
50645091
}
50655092
argvlist[argc] = NULL;
50665093

@@ -5155,6 +5182,13 @@ os_spawnve_impl(PyObject *module, int mode, path_t *path, PyObject *argv,
51555182
lastarg = i;
51565183
goto fail_1;
51575184
}
5185+
if (i == 0 && !argvlist[0][0]) {
5186+
lastarg = i;
5187+
PyErr_SetString(
5188+
PyExc_ValueError,
5189+
"spawnv() arg 2 first element cannot be empty");
5190+
goto fail_1;
5191+
}
51585192
}
51595193
lastarg = argc;
51605194
argvlist[argc] = NULL;

0 commit comments

Comments
 (0)