-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
gh-143544: Fix possible use-after-free in the JSON decoder when JSONDecodeError disappears during raising it #143561
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
vstinner
merged 25 commits into
python:main
from
VanshAgarwal24036:gh-143547-unraisablehook-uaf
Jan 12, 2026
Merged
Changes from 1 commit
Commits
Show all changes
25 commits
Select commit
Hold shift + click to select a range
9477824
gh-143544: Fix use-after-free in _json.raise_errmsg
VanshAgarwal24036 946aee3
gh-143544: Fix use-after-free in _json.raise_errmsg
VanshAgarwal24036 55b4850
gh-143544: Add regression test for re-entrant JSONDecodeError
VanshAgarwal24036 1940b1e
gh-143544: Add NEWS entry
VanshAgarwal24036 ed2c55c
gh-143544: Fix trailing whitespace in test
VanshAgarwal24036 099fc4f
gh-143544: Adjust regression test to allow SystemError
VanshAgarwal24036 b238345
gh-143544: Fix use-after-free in _json.raise_errmsg
VanshAgarwal24036 fa69d1e
gh-143544: Move json import to module level in regression test
VanshAgarwal24036 39767c6
gh-143544: Remove unnecessary comment
VanshAgarwal24036 b500563
gh-143544: Fix reference lifetime in raise_errmsg
VanshAgarwal24036 5841c2c
Update Lib/test/test_json/test_fail.py
VanshAgarwal24036 c397e8e
gh-143544: Remove unnecessary json import from regression test
VanshAgarwal24036 10c61c4
gh-143544: Use support.swap_attr in re-entrant JSONDecodeError test
VanshAgarwal24036 4a7f323
gh-143544: Fix use-after-free in _json.raise_errmsg
VanshAgarwal24036 0b38b8e
Update Lib/test/test_json/test_fail.py
VanshAgarwal24036 0593e2f
Update Lib/test/test_json/test_fail.py
VanshAgarwal24036 b485677
gh-143544: Tighten regression test exception assertion
VanshAgarwal24036 bbc357d
gh-143544: Fix re-entrant JSONDecodeError regression test
VanshAgarwal24036 765ffb2
test_json: add regression test for re-entrant JSONDecodeError crash
VanshAgarwal24036 b745fed
test_json: added Comment
VanshAgarwal24036 0a03442
test_json: assert TypeError for invalid JSONDecodeError replacement
VanshAgarwal24036 4257cdc
test_json: document and assert refcount for reentrant JSONDecodeError…
VanshAgarwal24036 74cfaed
test_json: fix reentrant JSONDecodeError test
VanshAgarwal24036 211cb30
test_json: fix re-entrant JSONDecodeError test
VanshAgarwal24036 a2e23bc
gh-143544: Revert test_json regression test changes
VanshAgarwal24036 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -413,21 +413,33 @@ write_escaped_unicode(PyUnicodeWriter *writer, PyObject *pystr) | |
| static void | ||
| raise_errmsg(const char *msg, PyObject *s, Py_ssize_t end) | ||
| { | ||
| /* Use JSONDecodeError exception to raise a nice looking ValueError subclass */ | ||
| _Py_DECLARE_STR(json_decoder, "json.decoder"); | ||
| PyObject *JSONDecodeError = | ||
| PyImport_ImportModuleAttr(&_Py_STR(json_decoder), &_Py_ID(JSONDecodeError)); | ||
| if (JSONDecodeError == NULL) { | ||
|
|
||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is an unrelated change. |
||
| PyObject *json_error = | ||
| PyImport_ImportModuleAttr(&_Py_STR(json_decoder), &_Py_ID(JSONDecodeError)); | ||
| if (json_error == NULL) { | ||
|
VanshAgarwal24036 marked this conversation as resolved.
Outdated
|
||
| return; | ||
| } | ||
|
|
||
| PyObject *exc; | ||
| exc = PyObject_CallFunction(JSONDecodeError, "zOn", msg, s, end); | ||
| Py_DECREF(JSONDecodeError); | ||
| if (exc) { | ||
| PyErr_SetObject(JSONDecodeError, exc); | ||
| /* Hold a strong reference across user code execution */ | ||
| PyObject *error_type = Py_NewRef(json_error); | ||
|
VanshAgarwal24036 marked this conversation as resolved.
Outdated
|
||
|
|
||
| PyObject *exc = PyObject_CallFunction(error_type, "zOn", msg, s, end); | ||
|
|
||
|
VanshAgarwal24036 marked this conversation as resolved.
Outdated
|
||
| if (exc != NULL) { | ||
| /* Only use it if it's a valid exception type */ | ||
| if (PyExceptionClass_Check(error_type)) { | ||
|
VanshAgarwal24036 marked this conversation as resolved.
Outdated
|
||
| PyErr_SetObject(error_type, exc); | ||
| } | ||
| else { | ||
| /* Fallback: always safe */ | ||
| PyErr_SetString(PyExc_ValueError, msg); | ||
| } | ||
| Py_DECREF(exc); | ||
| } | ||
|
|
||
| Py_DECREF(error_type); | ||
| Py_DECREF(json_error); | ||
| } | ||
|
|
||
| static void | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.