Commit a445c16
committed
gh-144475: Fix use-after-free in functools.partial.__repr__()
Hold strong references to pto->args, pto->kw, and pto->fn during
partial_repr() to prevent them from being freed by a user-defined
__repr__() that mutates the partial object via __setstate__().
Previously, partial_repr() iterated over pto->args using a size 'n'
captured before the loop, and accessed tuple items via borrowed
references. If a __repr__() called during formatting invoked
pto.__setstate__() with a new (smaller) args tuple, the original
tuple could be freed while the loop was still iterating, leading to
a heap-buffer-overflow (out-of-bounds read).
The fix takes a new reference (Py_NewRef) to the args tuple, kw dict,
and fn callable before using them, ensuring they stay alive regardless
of any mutations to the partial object during formatting.1 parent 41fa2db commit a445c16
1 file changed
+24
-8
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
705 | 705 | | |
706 | 706 | | |
707 | 707 | | |
708 | | - | |
| 708 | + | |
| 709 | + | |
| 710 | + | |
| 711 | + | |
| 712 | + | |
709 | 713 | | |
710 | | - | |
| 714 | + | |
| 715 | + | |
711 | 716 | | |
712 | 717 | | |
713 | | - | |
714 | | - | |
| 718 | + | |
| 719 | + | |
| 720 | + | |
715 | 721 | | |
| 722 | + | |
716 | 723 | | |
717 | | - | |
| 724 | + | |
| 725 | + | |
| 726 | + | |
718 | 727 | | |
719 | | - | |
| 728 | + | |
| 729 | + | |
720 | 730 | | |
721 | 731 | | |
722 | 732 | | |
723 | 733 | | |
724 | 734 | | |
725 | | - | |
| 735 | + | |
| 736 | + | |
726 | 737 | | |
| 738 | + | |
727 | 739 | | |
| 740 | + | |
728 | 741 | | |
729 | 742 | | |
730 | 743 | | |
| |||
735 | 748 | | |
736 | 749 | | |
737 | 750 | | |
738 | | - | |
| 751 | + | |
| 752 | + | |
| 753 | + | |
| 754 | + | |
739 | 755 | | |
740 | 756 | | |
741 | 757 | | |
| |||
0 commit comments