Commit bd72783
committed
pythongh-142884: Fix use-after-free in array.array.tofile() with reentrant writer
array_array_tofile_impl() pre-computed nbytes and nblocks once at the
start of the function. If the file-like object's write() callback
mutated the array (e.g. by clearing it or replacing its contents), the
cached values became stale and subsequent iterations read from freed or
invalid memory.
Fix by re-checking Py_SIZE(self) on every loop iteration so the loop
terminates safely when the array is modified during the write callback.1 parent 6577d87 commit bd72783
3 files changed
Lines changed: 68 additions & 10 deletions
File tree
- Lib/test
- Misc/NEWS.d/next/Library
- Modules
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1737 | 1737 | | |
1738 | 1738 | | |
1739 | 1739 | | |
| 1740 | + | |
| 1741 | + | |
| 1742 | + | |
| 1743 | + | |
| 1744 | + | |
| 1745 | + | |
| 1746 | + | |
| 1747 | + | |
| 1748 | + | |
| 1749 | + | |
| 1750 | + | |
| 1751 | + | |
| 1752 | + | |
| 1753 | + | |
| 1754 | + | |
| 1755 | + | |
| 1756 | + | |
| 1757 | + | |
| 1758 | + | |
| 1759 | + | |
| 1760 | + | |
| 1761 | + | |
| 1762 | + | |
| 1763 | + | |
| 1764 | + | |
| 1765 | + | |
| 1766 | + | |
| 1767 | + | |
| 1768 | + | |
| 1769 | + | |
| 1770 | + | |
| 1771 | + | |
| 1772 | + | |
| 1773 | + | |
| 1774 | + | |
| 1775 | + | |
| 1776 | + | |
| 1777 | + | |
| 1778 | + | |
| 1779 | + | |
| 1780 | + | |
| 1781 | + | |
| 1782 | + | |
| 1783 | + | |
| 1784 | + | |
| 1785 | + | |
| 1786 | + | |
| 1787 | + | |
| 1788 | + | |
| 1789 | + | |
| 1790 | + | |
| 1791 | + | |
1740 | 1792 | | |
1741 | 1793 | | |
1742 | 1794 | | |
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 | |
|---|---|---|---|
| |||
1659 | 1659 | | |
1660 | 1660 | | |
1661 | 1661 | | |
1662 | | - | |
1663 | 1662 | | |
1664 | 1663 | | |
1665 | 1664 | | |
1666 | | - | |
1667 | 1665 | | |
1668 | 1666 | | |
1669 | 1667 | | |
1670 | 1668 | | |
1671 | 1669 | | |
1672 | | - | |
1673 | 1670 | | |
1674 | 1671 | | |
1675 | 1672 | | |
1676 | | - | |
1677 | | - | |
| 1673 | + | |
| 1674 | + | |
| 1675 | + | |
| 1676 | + | |
| 1677 | + | |
| 1678 | + | |
| 1679 | + | |
| 1680 | + | |
1678 | 1681 | | |
1679 | | - | |
| 1682 | + | |
| 1683 | + | |
1680 | 1684 | | |
1681 | | - | |
1682 | | - | |
1683 | | - | |
| 1685 | + | |
| 1686 | + | |
1684 | 1687 | | |
1685 | 1688 | | |
1686 | | - | |
| 1689 | + | |
1687 | 1690 | | |
1688 | 1691 | | |
1689 | 1692 | | |
| |||
0 commit comments