Commit bf3bdde
committed
gh-148660: Fix use-after-free in OrderedDict.copy() on reentrant mutation
OrderedDict.copy() walks the internal linked list while building the new
dict. The loop body can run arbitrary Python (a key's __eq__/__hash__, or
a subclass __getitem__/__setitem__) which can clear the source dict and
free the nodes being iterated.
Detect this the same way OrderedDict.__eq__ already does (gh-119004):
snapshot od_state before the loop, hold a strong reference to the key and
read the hash before any reentrant call, and raise RuntimeError if the
state changed before advancing to the next node.1 parent 9e863fa commit bf3bdde
3 files changed
Lines changed: 62 additions & 10 deletions
File tree
- Lib/test
- Misc/NEWS.d/next/Library
- Objects
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
879 | 879 | | |
880 | 880 | | |
881 | 881 | | |
| 882 | + | |
| 883 | + | |
| 884 | + | |
| 885 | + | |
| 886 | + | |
| 887 | + | |
| 888 | + | |
| 889 | + | |
| 890 | + | |
| 891 | + | |
| 892 | + | |
| 893 | + | |
| 894 | + | |
| 895 | + | |
| 896 | + | |
| 897 | + | |
| 898 | + | |
| 899 | + | |
| 900 | + | |
| 901 | + | |
| 902 | + | |
| 903 | + | |
| 904 | + | |
| 905 | + | |
| 906 | + | |
| 907 | + | |
| 908 | + | |
| 909 | + | |
| 910 | + | |
| 911 | + | |
| 912 | + | |
| 913 | + | |
| 914 | + | |
882 | 915 | | |
883 | 916 | | |
884 | 917 | | |
| |||
Lines changed: 3 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
| 3 | + | |
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1251 | 1251 | | |
1252 | 1252 | | |
1253 | 1253 | | |
| 1254 | + | |
| 1255 | + | |
| 1256 | + | |
| 1257 | + | |
1254 | 1258 | | |
1255 | 1259 | | |
1256 | | - | |
1257 | | - | |
| 1260 | + | |
| 1261 | + | |
| 1262 | + | |
1258 | 1263 | | |
1259 | 1264 | | |
1260 | 1265 | | |
| 1266 | + | |
1261 | 1267 | | |
1262 | 1268 | | |
1263 | | - | |
1264 | | - | |
| 1269 | + | |
| 1270 | + | |
| 1271 | + | |
| 1272 | + | |
1265 | 1273 | | |
| 1274 | + | |
| 1275 | + | |
1266 | 1276 | | |
1267 | 1277 | | |
1268 | 1278 | | |
1269 | 1279 | | |
1270 | | - | |
1271 | | - | |
1272 | | - | |
1273 | | - | |
| 1280 | + | |
| 1281 | + | |
| 1282 | + | |
| 1283 | + | |
1274 | 1284 | | |
1275 | | - | |
1276 | | - | |
| 1285 | + | |
| 1286 | + | |
1277 | 1287 | | |
| 1288 | + | |
1278 | 1289 | | |
1279 | 1290 | | |
| 1291 | + | |
| 1292 | + | |
1280 | 1293 | | |
1281 | 1294 | | |
1282 | 1295 | | |
1283 | 1296 | | |
| 1297 | + | |
| 1298 | + | |
| 1299 | + | |
1284 | 1300 | | |
1285 | 1301 | | |
1286 | 1302 | | |
| |||
0 commit comments