Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 80f78a3

Browse files
committed
fix use after free (closes #24552)
1 parent b7a688b commit 80f78a3

3 files changed

Lines changed: 15 additions & 1 deletion

File tree

Lib/test/pickletester.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1039,6 +1039,18 @@ def test_newobj_proxies(self):
10391039
self.assertEqual(B(x), B(y), detail)
10401040
self.assertEqual(x.__dict__, y.__dict__, detail)
10411041

1042+
def test_newobj_not_class(self):
1043+
# Issue 24552
1044+
global SimpleNewObj
1045+
save = SimpleNewObj
1046+
o = object.__new__(SimpleNewObj)
1047+
b = self.dumps(o, 4)
1048+
try:
1049+
SimpleNewObj = 42
1050+
self.assertRaises((TypeError, pickle.UnpicklingError), self.loads, b)
1051+
finally:
1052+
SimpleNewObj = save
1053+
10421054
# Register a type with copyreg, with extension code extcode. Pickle
10431055
# an object of that type. Check that the resulting pickle uses opcode
10441056
# (EXT[124]) under proto 2, and not in proto 1.

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ Core and Builtins
6464
Library
6565
-------
6666

67+
- Issue #24552: Fix use after free in an error case of the _pickle module.
68+
6769
- Issue #24514: tarfile now tolerates number fields consisting of only
6870
whitespace.
6971

Modules/_pickle.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5210,10 +5210,10 @@ load_newobj_ex(UnpicklerObject *self)
52105210
if (!PyType_Check(cls)) {
52115211
Py_DECREF(kwargs);
52125212
Py_DECREF(args);
5213-
Py_DECREF(cls);
52145213
PyErr_Format(st->UnpicklingError,
52155214
"NEWOBJ_EX class argument must be a type, not %.200s",
52165215
Py_TYPE(cls)->tp_name);
5216+
Py_DECREF(cls);
52175217
return -1;
52185218
}
52195219

0 commit comments

Comments
 (0)