mirror of
https://github.com/python/cpython.git
synced 2025-08-03 16:39:00 +00:00
[3.11] gh-96670: Raise SyntaxError when parsing NULL bytes (GH-97594) (#104195)
This commit is contained in:
parent
c5dafeaa6d
commit
a09d3901a5
9 changed files with 77 additions and 22 deletions
|
@ -857,6 +857,10 @@ class AST_Tests(unittest.TestCase):
|
|||
check_limit("a", "[0]")
|
||||
check_limit("a", "*a")
|
||||
|
||||
def test_null_bytes(self):
|
||||
with self.assertRaises(SyntaxError,
|
||||
msg="source code string cannot contain null bytes"):
|
||||
ast.parse("a\0b")
|
||||
|
||||
class ASTHelpers_Test(unittest.TestCase):
|
||||
maxDiff = None
|
||||
|
|
|
@ -334,11 +334,10 @@ class BuiltinTest(unittest.TestCase):
|
|||
self.assertRaises(TypeError, compile)
|
||||
self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'badmode')
|
||||
self.assertRaises(ValueError, compile, 'print(42)\n', '<string>', 'single', 0xff)
|
||||
self.assertRaises(ValueError, compile, chr(0), 'f', 'exec')
|
||||
self.assertRaises(TypeError, compile, 'pass', '?', 'exec',
|
||||
mode='eval', source='0', filename='tmp')
|
||||
compile('print("\xe5")\n', '', 'exec')
|
||||
self.assertRaises(ValueError, compile, chr(0), 'f', 'exec')
|
||||
self.assertRaises(SyntaxError, compile, chr(0), 'f', 'exec')
|
||||
self.assertRaises(ValueError, compile, str('a = 1'), 'f', 'bad')
|
||||
|
||||
# test the optimize argument
|
||||
|
|
|
@ -657,6 +657,31 @@ class CmdLineTest(unittest.TestCase):
|
|||
],
|
||||
)
|
||||
|
||||
def test_syntaxerror_null_bytes(self):
|
||||
script = "x = '\0' nothing to see here\n';import os;os.system('echo pwnd')\n"
|
||||
with os_helper.temp_dir() as script_dir:
|
||||
script_name = _make_test_script(script_dir, 'script', script)
|
||||
exitcode, stdout, stderr = assert_python_failure(script_name)
|
||||
self.assertEqual(
|
||||
stderr.splitlines()[-2:],
|
||||
[ b" x = '",
|
||||
b'SyntaxError: source code cannot contain null bytes'
|
||||
],
|
||||
)
|
||||
|
||||
def test_syntaxerror_null_bytes_in_multiline_string(self):
|
||||
scripts = ["\n'''\nmultilinestring\0\n'''", "\nf'''\nmultilinestring\0\n'''"] # Both normal and f-strings
|
||||
with os_helper.temp_dir() as script_dir:
|
||||
for script in scripts:
|
||||
script_name = _make_test_script(script_dir, 'script', script)
|
||||
_, _, stderr = assert_python_failure(script_name)
|
||||
self.assertEqual(
|
||||
stderr.splitlines()[-2:],
|
||||
[ b" multilinestring",
|
||||
b'SyntaxError: source code cannot contain null bytes'
|
||||
]
|
||||
)
|
||||
|
||||
def test_consistent_sys_path_for_direct_execution(self):
|
||||
# This test case ensures that the following all give the same
|
||||
# sys.path configuration:
|
||||
|
|
|
@ -542,7 +542,7 @@ if 1:
|
|||
with open(fn, "wb") as fp:
|
||||
fp.write(src)
|
||||
res = script_helper.run_python_until_end(fn)[0]
|
||||
self.assertIn(b"Non-UTF-8", res.err)
|
||||
self.assertIn(b"source code cannot contain null bytes", res.err)
|
||||
|
||||
def test_yet_more_evil_still_undecodable(self):
|
||||
# Issue #25388
|
||||
|
@ -552,7 +552,7 @@ if 1:
|
|||
with open(fn, "wb") as fp:
|
||||
fp.write(src)
|
||||
res = script_helper.run_python_until_end(fn)[0]
|
||||
self.assertIn(b"Non-UTF-8", res.err)
|
||||
self.assertIn(b"source code cannot contain null bytes", res.err)
|
||||
|
||||
@support.cpython_only
|
||||
def test_compiler_recursion_limit(self):
|
||||
|
@ -588,9 +588,9 @@ if 1:
|
|||
def test_null_terminated(self):
|
||||
# The source code is null-terminated internally, but bytes-like
|
||||
# objects are accepted, which could be not terminated.
|
||||
with self.assertRaisesRegex(ValueError, "cannot contain null"):
|
||||
with self.assertRaisesRegex(SyntaxError, "cannot contain null"):
|
||||
compile("123\x00", "<dummy>", "eval")
|
||||
with self.assertRaisesRegex(ValueError, "cannot contain null"):
|
||||
with self.assertRaisesRegex(SyntaxError, "cannot contain null"):
|
||||
compile(memoryview(b"123\x00"), "<dummy>", "eval")
|
||||
code = compile(memoryview(b"123\x00")[1:-1], "<dummy>", "eval")
|
||||
self.assertEqual(eval(code), 23)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue