Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
C
cpython
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
Kirill Smelkov
cpython
Commits
77703942
Commit
77703942
authored
Jun 25, 2017
by
Serhiy Storchaka
Committed by
GitHub
Jun 25, 2017
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
bpo-30746: Prohibited the '=' character in environment variable names (#2382)
in `os.putenv()` and `os.spawn*()`.
parent
1ba9469e
Changes
4
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
95 additions
and
4 deletions
+95
-4
Lib/test/test_os.py
Lib/test/test_os.py
+49
-0
Lib/test/test_posix.py
Lib/test/test_posix.py
+15
-0
Misc/NEWS
Misc/NEWS
+3
-0
Modules/posixmodule.c
Modules/posixmodule.c
+28
-4
No files found.
Lib/test/test_os.py
View file @
77703942
...
@@ -2382,6 +2382,55 @@ class SpawnTests(unittest.TestCase):
...
@@ -2382,6 +2382,55 @@ class SpawnTests(unittest.TestCase):
self
.
assertRaises
(
ValueError
,
os
.
spawnve
,
os
.
P_NOWAIT
,
args
[
0
],
(
''
,),
{})
self
.
assertRaises
(
ValueError
,
os
.
spawnve
,
os
.
P_NOWAIT
,
args
[
0
],
(
''
,),
{})
self
.
assertRaises
(
ValueError
,
os
.
spawnve
,
os
.
P_NOWAIT
,
args
[
0
],
[
''
],
{})
self
.
assertRaises
(
ValueError
,
os
.
spawnve
,
os
.
P_NOWAIT
,
args
[
0
],
[
''
],
{})
@
requires_os_func
(
'spawnve'
)
def
test_spawnve_invalid_env
(
self
):
# null character in the enviroment variable name
args
=
[
sys
.
executable
,
'-c'
,
'pass'
]
newenv
=
os
.
environ
.
copy
()
newenv
[
"FRUIT
\
0
VEGETABLE"
]
=
"cabbage"
try
:
exitcode
=
os
.
spawnve
(
os
.
P_WAIT
,
args
[
0
],
args
,
newenv
)
except
ValueError
:
pass
else
:
self
.
assertEqual
(
exitcode
,
127
)
# null character in the enviroment variable value
args
=
[
sys
.
executable
,
'-c'
,
'pass'
]
newenv
=
os
.
environ
.
copy
()
newenv
[
"FRUIT"
]
=
"orange
\
0
VEGETABLE=cabbage"
try
:
exitcode
=
os
.
spawnve
(
os
.
P_WAIT
,
args
[
0
],
args
,
newenv
)
except
ValueError
:
pass
else
:
self
.
assertEqual
(
exitcode
,
127
)
# equal character in the enviroment variable name
args
=
[
sys
.
executable
,
'-c'
,
'pass'
]
newenv
=
os
.
environ
.
copy
()
newenv
[
"FRUIT=ORANGE"
]
=
"lemon"
try
:
exitcode
=
os
.
spawnve
(
os
.
P_WAIT
,
args
[
0
],
args
,
newenv
)
except
ValueError
:
pass
else
:
self
.
assertEqual
(
exitcode
,
127
)
# equal character in the enviroment variable value
filename
=
support
.
TESTFN
self
.
addCleanup
(
support
.
unlink
,
filename
)
with
open
(
filename
,
"w"
)
as
fp
:
fp
.
write
(
'import sys, os
\
n
'
'if os.getenv("FRUIT") != "orange=lemon":
\
n
'
' raise AssertionError'
)
args
=
[
sys
.
executable
,
filename
]
newenv
=
os
.
environ
.
copy
()
newenv
[
"FRUIT"
]
=
"orange=lemon"
exitcode
=
os
.
spawnve
(
os
.
P_WAIT
,
args
[
0
],
args
,
newenv
)
self
.
assertEqual
(
exitcode
,
0
)
# The introduction of this TestCase caused at least two different errors on
# The introduction of this TestCase caused at least two different errors on
# *nix buildbots. Temporarily skip this to let the buildbots move along.
# *nix buildbots. Temporarily skip this to let the buildbots move along.
@
unittest
.
skip
(
"Skip due to platform/environment differences on *NIX buildbots"
)
@
unittest
.
skip
(
"Skip due to platform/environment differences on *NIX buildbots"
)
...
...
Lib/test/test_posix.py
View file @
77703942
...
@@ -813,6 +813,21 @@ class PosixTester(unittest.TestCase):
...
@@ -813,6 +813,21 @@ class PosixTester(unittest.TestCase):
self
.
assertEqual
(
type
(
k
),
item_type
)
self
.
assertEqual
(
type
(
k
),
item_type
)
self
.
assertEqual
(
type
(
v
),
item_type
)
self
.
assertEqual
(
type
(
v
),
item_type
)
@
unittest
.
skipUnless
(
hasattr
(
os
,
"putenv"
),
"requires os.putenv()"
)
def
test_putenv
(
self
):
with
self
.
assertRaises
(
ValueError
):
os
.
putenv
(
'FRUIT
\
0
VEGETABLE'
,
'cabbage'
)
with
self
.
assertRaises
(
ValueError
):
os
.
putenv
(
b'FRUIT
\
0
VEGETABLE'
,
b'cabbage'
)
with
self
.
assertRaises
(
ValueError
):
os
.
putenv
(
'FRUIT'
,
'orange
\
0
VEGETABLE=cabbage'
)
with
self
.
assertRaises
(
ValueError
):
os
.
putenv
(
b'FRUIT'
,
b'orange
\
0
VEGETABLE=cabbage'
)
with
self
.
assertRaises
(
ValueError
):
os
.
putenv
(
'FRUIT=ORANGE'
,
'lemon'
)
with
self
.
assertRaises
(
ValueError
):
os
.
putenv
(
b'FRUIT=ORANGE'
,
b'lemon'
)
@
unittest
.
skipUnless
(
hasattr
(
posix
,
'getcwd'
),
'test needs posix.getcwd()'
)
@
unittest
.
skipUnless
(
hasattr
(
posix
,
'getcwd'
),
'test needs posix.getcwd()'
)
def
test_getcwd_long_pathnames
(
self
):
def
test_getcwd_long_pathnames
(
self
):
dirname
=
'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
dirname
=
'getcwd-test-directory-0123456789abcdef-01234567890abcdef'
...
...
Misc/NEWS
View file @
77703942
...
@@ -374,6 +374,9 @@ Extension Modules
...
@@ -374,6 +374,9 @@ Extension Modules
Library
Library
-------
-------
- bpo-30746: Prohibited the '
=
' character in environment variable names in
``os.putenv()`` and ``os.spawn*()``.
- bpo-30664: The description of a unittest subtest now preserves the order of
- bpo-30664: The description of a unittest subtest now preserves the order of
keyword arguments of TestCase.subTest().
keyword arguments of TestCase.subTest().
...
...
Modules/posixmodule.c
View file @
77703942
...
@@ -4894,6 +4894,14 @@ parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
...
@@ -4894,6 +4894,14 @@ parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
Py_DECREF
(
key2
);
Py_DECREF
(
key2
);
goto
error
;
goto
error
;
}
}
/* Search from index 1 because on Windows starting '=' is allowed for
defining hidden environment variables. */
if
(
PyUnicode_GET_LENGTH
(
key2
)
==
0
||
PyUnicode_FindChar
(
key2
,
'='
,
1
,
PyUnicode_GET_LENGTH
(
key2
),
1
)
!=
-
1
)
{
PyErr_SetString
(
PyExc_ValueError
,
"illegal environment variable name"
);
goto
error
;
}
keyval
=
PyUnicode_FromFormat
(
"%U=%U"
,
key2
,
val2
);
keyval
=
PyUnicode_FromFormat
(
"%U=%U"
,
key2
,
val2
);
#else
#else
if
(
!
PyUnicode_FSConverter
(
key
,
&
key2
))
if
(
!
PyUnicode_FSConverter
(
key
,
&
key2
))
...
@@ -4902,6 +4910,12 @@ parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
...
@@ -4902,6 +4910,12 @@ parse_envlist(PyObject* env, Py_ssize_t *envc_ptr)
Py_DECREF
(
key2
);
Py_DECREF
(
key2
);
goto
error
;
goto
error
;
}
}
if
(
PyBytes_GET_SIZE
(
key2
)
==
0
||
strchr
(
PyBytes_AS_STRING
(
key2
)
+
1
,
'='
)
!=
NULL
)
{
PyErr_SetString
(
PyExc_ValueError
,
"illegal environment variable name"
);
goto
error
;
}
keyval
=
PyBytes_FromFormat
(
"%s=%s"
,
PyBytes_AS_STRING
(
key2
),
keyval
=
PyBytes_FromFormat
(
"%s=%s"
,
PyBytes_AS_STRING
(
key2
),
PyBytes_AS_STRING
(
val2
));
PyBytes_AS_STRING
(
val2
));
#endif
#endif
...
@@ -8985,9 +8999,16 @@ os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
...
@@ -8985,9 +8999,16 @@ os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
{
{
const
wchar_t
*
env
;
const
wchar_t
*
env
;
/* Search from index 1 because on Windows starting '=' is allowed for
defining hidden environment variables. */
if
(
PyUnicode_GET_LENGTH
(
name
)
==
0
||
PyUnicode_FindChar
(
name
,
'='
,
1
,
PyUnicode_GET_LENGTH
(
name
),
1
)
!=
-
1
)
{
PyErr_SetString
(
PyExc_ValueError
,
"illegal environment variable name"
);
return
NULL
;
}
PyObject
*
unicode
=
PyUnicode_FromFormat
(
"%U=%U"
,
name
,
value
);
PyObject
*
unicode
=
PyUnicode_FromFormat
(
"%U=%U"
,
name
,
value
);
if
(
unicode
==
NULL
)
{
if
(
unicode
==
NULL
)
{
PyErr_NoMemory
();
return
NULL
;
return
NULL
;
}
}
if
(
_MAX_ENV
<
PyUnicode_GET_LENGTH
(
unicode
))
{
if
(
_MAX_ENV
<
PyUnicode_GET_LENGTH
(
unicode
))
{
...
@@ -9029,12 +9050,15 @@ os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
...
@@ -9029,12 +9050,15 @@ os_putenv_impl(PyObject *module, PyObject *name, PyObject *value)
{
{
PyObject
*
bytes
=
NULL
;
PyObject
*
bytes
=
NULL
;
char
*
env
;
char
*
env
;
const
char
*
name_string
=
PyBytes_A
sString
(
name
);
const
char
*
name_string
=
PyBytes_A
S_STRING
(
name
);
const
char
*
value_string
=
PyBytes_A
sString
(
value
);
const
char
*
value_string
=
PyBytes_A
S_STRING
(
value
);
if
(
strchr
(
name_string
,
'='
)
!=
NULL
)
{
PyErr_SetString
(
PyExc_ValueError
,
"illegal environment variable name"
);
return
NULL
;
}
bytes
=
PyBytes_FromFormat
(
"%s=%s"
,
name_string
,
value_string
);
bytes
=
PyBytes_FromFormat
(
"%s=%s"
,
name_string
,
value_string
);
if
(
bytes
==
NULL
)
{
if
(
bytes
==
NULL
)
{
PyErr_NoMemory
();
return
NULL
;
return
NULL
;
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment