Skip to content

Commit 10b28c1

Browse files
committed
Merge branch 'main' into pr/92078
2 parents 02f9e2b + 4e9005d commit 10b28c1

File tree

9 files changed

+50
-7
lines changed

9 files changed

+50
-7
lines changed

Doc/library/json.rst

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,17 @@ is a lightweight data interchange format inspired by
1818
`JavaScript <https://en.wikipedia.org/wiki/JavaScript>`_ object literal syntax
1919
(although it is not a strict subset of JavaScript [#rfc-errata]_ ).
2020

21+
.. note::
22+
The term "object" in the context of JSON processing in Python can be
23+
ambiguous. All values in Python are objects. In JSON, an object refers to
24+
any data wrapped in curly braces, similar to a Python dictionary.
25+
2126
.. warning::
2227
Be cautious when parsing JSON data from untrusted sources. A malicious
2328
JSON string may cause the decoder to consume considerable CPU and memory
2429
resources. Limiting the size of data to be parsed is recommended.
2530

26-
:mod:`json` exposes an API familiar to users of the standard library
31+
This module exposes an API familiar to users of the standard library
2732
:mod:`marshal` and :mod:`pickle` modules.
2833

2934
Encoding basic Python object hierarchies::
@@ -60,7 +65,7 @@ Pretty printing::
6065
"6": 7
6166
}
6267

63-
Specializing JSON object encoding::
68+
Customizing JSON object encoding::
6469

6570
>>> import json
6671
>>> def custom_json(obj):
@@ -83,7 +88,7 @@ Decoding JSON::
8388
>>> json.load(io)
8489
['streaming API']
8590

86-
Specializing JSON object decoding::
91+
Customizing JSON object decoding::
8792

8893
>>> import json
8994
>>> def as_complex(dct):
@@ -279,7 +284,7 @@ Basic Usage
279284

280285
:param object_hook:
281286
If set, a function that is called with the result of
282-
any object literal decoded (a :class:`dict`).
287+
any JSON object literal decoded (a :class:`dict`).
283288
The return value of this function will be used
284289
instead of the :class:`dict`.
285290
This feature can be used to implement custom decoders,
@@ -289,7 +294,7 @@ Basic Usage
289294

290295
:param object_pairs_hook:
291296
If set, a function that is called with the result of
292-
any object literal decoded with an ordered list of pairs.
297+
any JSON object literal decoded with an ordered list of pairs.
293298
The return value of this function will be used
294299
instead of the :class:`dict`.
295300
This feature can be used to implement custom decoders.

Lib/test/test_importlib/import_/test_relative_imports.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,21 @@ def test_relative_import_no_package_exists_absolute(self):
223223
self.__import__('sys', {'__package__': '', '__spec__': None},
224224
level=1)
225225

226+
def test_malicious_relative_import(self):
227+
# https://github.com/python/cpython/issues/134100
228+
# Test to make sure UAF bug with error msg doesn't come back to life
229+
import sys
230+
loooong = "".ljust(0x23000, "b")
231+
name = f"a.{loooong}.c"
232+
233+
with util.uncache(name):
234+
sys.modules[name] = {}
235+
with self.assertRaisesRegex(
236+
KeyError,
237+
r"'a\.b+' not in sys\.modules as expected"
238+
):
239+
__import__(f"{loooong}.c", {"__package__": "a"}, level=1)
240+
226241

227242
(Frozen_RelativeImports,
228243
Source_RelativeImports

Lib/test/test_string/test_templatelib.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,13 @@ def test_iter(self):
148148
self.assertEqual(res[1].format_spec, '')
149149
self.assertEqual(res[2], ' yz')
150150

151+
def test_exhausted(self):
152+
# See https://github.com/python/cpython/issues/134119.
153+
template_iter = iter(t"{1}")
154+
self.assertIsInstance(next(template_iter), Interpolation)
155+
self.assertRaises(StopIteration, next, template_iter)
156+
self.assertRaises(StopIteration, next, template_iter)
157+
151158

152159
if __name__ == '__main__':
153160
unittest.main()

Lib/test/test_sys.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2176,6 +2176,13 @@ def test_remote_exec_invalid_pid(self):
21762176
with self.assertRaises(OSError):
21772177
sys.remote_exec(99999, "print('should not run')")
21782178

2179+
def test_remote_exec_invalid_script(self):
2180+
"""Test remote exec with invalid script type"""
2181+
with self.assertRaises(TypeError):
2182+
sys.remote_exec(0, None)
2183+
with self.assertRaises(TypeError):
2184+
sys.remote_exec(0, 123)
2185+
21792186
def test_remote_exec_syntax_error(self):
21802187
"""Test remote exec with syntax error in script"""
21812188
script = '''
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix a use-after-free bug that occurs when an imported module isn't
2+
in :data:`sys.modules` after its initial import. Patch by Nico-Posada.
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix crash when calling :func:`next` on an exhausted template string iterator.
2+
Patch by Jelle Zijlstra.

Objects/templateobject.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,9 @@ templateiter_next(PyObject *op)
2323
if (self->from_strings) {
2424
item = PyIter_Next(self->stringsiter);
2525
self->from_strings = 0;
26+
if (item == NULL) {
27+
return NULL;
28+
}
2629
if (PyUnicode_GET_LENGTH(item) == 0) {
2730
Py_SETREF(item, PyIter_Next(self->interpolationsiter));
2831
self->from_strings = 1;

Python/import.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3854,15 +3854,17 @@ PyImport_ImportModuleLevelObject(PyObject *name, PyObject *globals,
38543854
}
38553855

38563856
final_mod = import_get_module(tstate, to_return);
3857-
Py_DECREF(to_return);
38583857
if (final_mod == NULL) {
38593858
if (!_PyErr_Occurred(tstate)) {
38603859
_PyErr_Format(tstate, PyExc_KeyError,
38613860
"%R not in sys.modules as expected",
38623861
to_return);
38633862
}
3863+
Py_DECREF(to_return);
38643864
goto error;
38653865
}
3866+
3867+
Py_DECREF(to_return);
38663868
}
38673869
}
38683870
else {

Python/sysmodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2485,7 +2485,7 @@ sys_remote_exec_impl(PyObject *module, int pid, PyObject *script)
24852485
PyObject *path;
24862486
const char *debugger_script_path;
24872487

2488-
if (PyUnicode_FSConverter(script, &path) < 0) {
2488+
if (PyUnicode_FSConverter(script, &path) == 0) {
24892489
return NULL;
24902490
}
24912491
debugger_script_path = PyBytes_AS_STRING(path);

0 commit comments

Comments
 (0)