Skip to content

Commit 76859dc

Browse files
committed
Merged revisions 81398 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk ........ r81398 | antoine.pitrou | 2010-05-21 19:12:38 +0200 (ven., 21 mai 2010) | 6 lines Issue #5753: A new C API function, :cfunc:`PySys_SetArgvEx`, allows embedders of the interpreter to set sys.argv without also modifying sys.path. This helps fix `CVE-2008-5983 <http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_. ........
1 parent a65923d commit 76859dc

4 files changed

Lines changed: 50 additions & 7 deletions

File tree

Doc/c-api/init.rst

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Initialization, Finalization, and Threads
2222
module: sys
2323
triple: module; search; path
2424
single: PySys_SetArgv()
25+
single: PySys_SetArgvEx()
2526
single: Py_Finalize()
2627

2728
Initialize the Python interpreter. In an application embedding Python, this
@@ -31,7 +32,7 @@ Initialization, Finalization, and Threads
3132
the table of loaded modules (``sys.modules``), and creates the fundamental
3233
modules :mod:`__builtin__`, :mod:`__main__` and :mod:`sys`. It also initializes
3334
the module search path (``sys.path``). It does not set ``sys.argv``; use
34-
:cfunc:`PySys_SetArgv` for that. This is a no-op when called for a second time
35+
:cfunc:`PySys_SetArgvEx` for that. This is a no-op when called for a second time
3536
(without calling :cfunc:`Py_Finalize` first). There is no return value; it is a
3637
fatal error if the initialization fails.
3738

@@ -338,7 +339,7 @@ Initialization, Finalization, and Threads
338339
``sys.version``.
339340

340341

341-
.. cfunction:: void PySys_SetArgv(int argc, char **argv)
342+
.. cfunction:: void PySys_SetArgvEx(int argc, char **argv, int updatepath)
342343

343344
.. index::
344345
single: main()
@@ -353,14 +354,41 @@ Initialization, Finalization, and Threads
353354
string. If this function fails to initialize :data:`sys.argv`, a fatal
354355
condition is signalled using :cfunc:`Py_FatalError`.
355356

356-
This function also prepends the executed script's path to :data:`sys.path`.
357-
If no script is executed (in the case of calling ``python -c`` or just the
358-
interactive interpreter), the empty string is used instead.
357+
If *updatepath* is zero, this is all the function does. If *updatepath*
358+
is non-zero, the function also modifies :data:`sys.path` according to the
359+
following algorithm:
360+
361+
- If the name of an existing script is passed in ``argv[0]``, the absolute
362+
path of the directory where the script is located is prepended to
363+
:data:`sys.path`.
364+
- Otherwise (that is, if *argc* is 0 or ``argv[0]`` doesn't point
365+
to an existing file name), an empty string is prepended to
366+
:data:`sys.path`, which is the same as prepending the current working
367+
directory (``"."``).
368+
369+
.. note::
370+
It is recommended that applications embedding the Python interpreter
371+
for purposes other than executing a single script pass 0 as *updatepath*,
372+
and update :data:`sys.path` themselves if desired.
373+
See `CVE-2008-5983 <http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_.
374+
375+
On versions before 2.6.6, you can achieve the same effect by manually
376+
popping the first :data:`sys.path` element after having called
377+
:cfunc:`PySys_SetArgv`, for example using::
378+
379+
PyRun_SimpleString("import sys; sys.path.pop(0)\n");
380+
381+
.. versionadded:: 2.6.6
359382

360383
.. XXX impl. doesn't seem consistent in allowing 0/NULL for the params;
361384
check w/ Guido.
362385
363386
387+
.. cfunction:: void PySys_SetArgv(int argc, char **argv)
388+
389+
This function works like :cfunc:`PySys_SetArgv` with *updatepath* set to 1.
390+
391+
364392
.. cfunction:: void Py_SetPythonHome(char *home)
365393

366394
Set the default "home" directory, that is, the location of the standard

Include/sysmodule.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ PyAPI_FUNC(PyObject *) PySys_GetObject(char *);
1111
PyAPI_FUNC(int) PySys_SetObject(char *, PyObject *);
1212
PyAPI_FUNC(FILE *) PySys_GetFile(char *, FILE *);
1313
PyAPI_FUNC(void) PySys_SetArgv(int, char **);
14+
PyAPI_FUNC(void) PySys_SetArgvEx(int, char **, int);
1415
PyAPI_FUNC(void) PySys_SetPath(char *);
1516

1617
PyAPI_FUNC(void) PySys_WriteStdout(const char *format, ...)

Misc/NEWS

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ Core and Builtins
4444

4545
- Issue #7072: isspace(0xa0) is true on Mac OS X
4646

47+
C-API
48+
-----
49+
50+
- Issue #5753: A new C API function, :cfunc:`PySys_SetArgvEx`, allows
51+
embedders of the interpreter to set sys.argv without also modifying
52+
sys.path. This helps fix `CVE-2008-5983
53+
<http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2008-5983>`_.
54+
4755
Library
4856
-------
4957

Python/sysmodule.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ makeargvobject(int argc, char **argv)
15281528
}
15291529

15301530
void
1531-
PySys_SetArgv(int argc, char **argv)
1531+
PySys_SetArgvEx(int argc, char **argv, int updatepath)
15321532
{
15331533
#if defined(HAVE_REALPATH)
15341534
char fullpath[MAXPATHLEN];
@@ -1541,7 +1541,7 @@ PySys_SetArgv(int argc, char **argv)
15411541
Py_FatalError("no mem for sys.argv");
15421542
if (PySys_SetObject("argv", av) != 0)
15431543
Py_FatalError("can't assign sys.argv");
1544-
if (path != NULL) {
1544+
if (updatepath && path != NULL) {
15451545
char *argv0 = argv[0];
15461546
char *p = NULL;
15471547
Py_ssize_t n = 0;
@@ -1631,6 +1631,12 @@ PySys_SetArgv(int argc, char **argv)
16311631
Py_DECREF(av);
16321632
}
16331633

1634+
void
1635+
PySys_SetArgv(int argc, char **argv)
1636+
{
1637+
PySys_SetArgvEx(argc, argv, 1);
1638+
}
1639+
16341640

16351641
/* APIs to write to sys.stdout or sys.stderr using a printf-like interface.
16361642
Adapted from code submitted by Just van Rossum.

0 commit comments

Comments
 (0)