Commit 4847088
committed
gh-145244: Fix use-after-free on borrowed dict key in json encoder
In encoder_encode_key_value(), key is a borrowed reference from
PyDict_Next(). If the default callback mutates or clears the dict,
key becomes a dangling pointer. The error path then calls
_PyErr_FormatNote("%R", key) on freed memory.
Fix by holding strong references to key and value unconditionally
during encoding, not just in the free-threading build.1 parent 1ac9d13 commit 4847088
File tree
3 files changed
+31
-7
lines changed- Lib/test/test_json
- Misc/NEWS.d/next/Library
- Modules
3 files changed
+31
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
77 | 77 | | |
78 | 78 | | |
79 | 79 | | |
| 80 | + | |
| 81 | + | |
| 82 | + | |
| 83 | + | |
| 84 | + | |
| 85 | + | |
| 86 | + | |
| 87 | + | |
| 88 | + | |
| 89 | + | |
| 90 | + | |
| 91 | + | |
| 92 | + | |
| 93 | + | |
| 94 | + | |
| 95 | + | |
| 96 | + | |
| 97 | + | |
| 98 | + | |
| 99 | + | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
80 | 103 | | |
81 | 104 | | |
82 | 105 | | |
| |||
Lines changed: 4 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| 4 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1774 | 1774 | | |
1775 | 1775 | | |
1776 | 1776 | | |
1777 | | - | |
1778 | | - | |
| 1777 | + | |
| 1778 | + | |
| 1779 | + | |
| 1780 | + | |
1779 | 1781 | | |
1780 | 1782 | | |
1781 | | - | |
1782 | 1783 | | |
1783 | 1784 | | |
1784 | 1785 | | |
1785 | | - | |
1786 | 1786 | | |
1787 | 1787 | | |
1788 | | - | |
1789 | 1788 | | |
1790 | 1789 | | |
1791 | | - | |
1792 | 1790 | | |
1793 | 1791 | | |
1794 | | - | |
1795 | 1792 | | |
1796 | 1793 | | |
1797 | 1794 | | |
| |||
0 commit comments