Skip to content

Commit 2ecf528

Browse files
[3.13] gh-92347: Document pitfalls of ctypes.pointer iteration (GH-157801) (GH-157927)
(cherry picked from commit a3e389d) Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
1 parent bc0d56d commit 2ecf528

1 file changed

Lines changed: 9 additions & 0 deletions

File tree

‎Doc/library/ctypes.rst‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,6 +835,15 @@ Generally you only use this feature if you receive a pointer from a C function,
835835
and you *know* that the pointer actually points to an array instead of a single
836836
item.
837837

838+
.. warning::
839+
840+
Because pointer objects support subscription, they implicitly support
841+
:term:`iteration <iterator>`. Unless doing this in a controlled manner,
842+
such as by manually calling :func:`next` on a :func:`pointer` iterator, this
843+
will typically lead to infinite loops or crashes, because ctypes has no way
844+
of knowing when to stop iteration. In other words, a ``pointer`` iterator
845+
will infinitely yield arbitrary memory.
846+
838847
Behind the scenes, the :func:`pointer` function does more than simply create
839848
pointer instances, it has to create pointer *types* first. This is done with the
840849
:func:`POINTER` function, which accepts any :mod:`ctypes` type, and returns a

0 commit comments

Comments
 (0)