-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-143544: Fix use-after-free in _json.raise_errmsg #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
base: main
Are you sure you want to change the base?
Changes from all commits
9477824
946aee3
55b4850
1940b1e
ed2c55c
099fc4f
b238345
fa69d1e
39767c6
b500563
5841c2c
c397e8e
10c61c4
4a7f323
0b38b8e
0593e2f
b485677
bbc357d
765ffb2
b745fed
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| from test.test_json import PyTest, CTest | ||
| import json | ||
|
|
||
| # 2007-10-05 | ||
| JSONDOCS = [ | ||
|
|
@@ -236,5 +237,36 @@ def test_linecol(self): | |
| 'Expecting value: line %s column %d (char %d)' % | ||
| (line, col, idx)) | ||
|
|
||
|
|
||
| def test_reentrant_jsondecodeerror_does_not_crash(self): | ||
| # gh-143544 | ||
|
|
||
| class Trigger: | ||
| def __call__(self, *args, **kwargs): | ||
| # Remove JSONDecodeError during construction to trigger re-entrancy | ||
| del json.JSONDecodeError | ||
| del json.decoder.JSONDecodeError | ||
| return ValueError("boom") | ||
|
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. Just for the case, call |
||
|
|
||
| hook = Trigger() | ||
|
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.
class hook(Exception):
def __new__(...):
# ...And now calling |
||
|
|
||
| orig_json_error = json.JSONDecodeError | ||
| orig_decoder_error = json.decoder.JSONDecodeError | ||
|
|
||
| try: | ||
| # NOTE: Do not use swap_attr() here. | ||
| # swap_attr() keeps the replacement object alive for the duration of | ||
| # the context manager, which prevents the crash this test is meant | ||
| # to reproduce | ||
| json.JSONDecodeError = hook | ||
| json.decoder.JSONDecodeError = hook | ||
| del hook | ||
|
|
||
| with self.assertRaises(ValueError): | ||
| json.loads('"\\uZZZZ"') | ||
| finally: | ||
| json.JSONDecodeError = orig_json_error | ||
| json.decoder.JSONDecodeError = orig_decoder_error | ||
|
|
||
| class TestPyFail(TestFail, PyTest): pass | ||
| class TestCFail(TestFail, CTest): pass | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we really need this? I'm asking because
TestFailis a mixin class with aselfattribute forJSONDecoderError. So should we patch this module directly or not?Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This test mutates json.JSONDecodeError and json.decoder.JSONDecodeError directly to trigger the re-entrancy case, so it needs access to the json module object. Using self.JSONDecodeError wouldn’t be sufficient here.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure this is the right place for such test. This file tests how different decoding errors are handled (and some encoding errors, but I think they are misplaced). But we need to test the bug in the code that raises JSONDecodeError. Maybe
test_speedups.pyis better place for this.