X Tutup
Skip to content

gh-144100: Fix crash for POINTER(str) used in ctypes argtypes#144108

Merged
vstinner merged 7 commits intopython:mainfrom
VanshAgarwal24036:gh-144100-ctypes-pointer-protol
Jan 26, 2026
Merged

gh-144100: Fix crash for POINTER(str) used in ctypes argtypes#144108
vstinner merged 7 commits intopython:mainfrom
VanshAgarwal24036:gh-144100-ctypes-pointer-protol

Conversation

@VanshAgarwal24036
Copy link
Contributor

@VanshAgarwal24036 VanshAgarwal24036 commented Jan 21, 2026

It fixes a crash in ctypes that occur when a deprecated POINTER(str) type was used in argtypes.

When such a pointer type was encountered during argument conversion, ctypes would hit an internal assertion and abort the interpreter in debug builds.
This PR replaces the assertion with proper error handling and raises a Python exception instead.

@VanshAgarwal24036

This comment was marked as resolved.

@VanshAgarwal24036
Copy link
Contributor Author

@vstinner This is my proposed solution if you have time please review it.

Comment on lines +478 to +479
with self.assertWarns(DeprecationWarning):
BadType = ctypes.POINTER("BugTrigger")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to avoid deprecated code path. You should be able to use:

Suggested change
with self.assertWarns(DeprecationWarning):
BadType = ctypes.POINTER("BugTrigger")
class BadType(ctypes._Pointer):
# _type_ is not defined on purpose
pass

Comment on lines +481 to +488
if os.name == "nt":
libc = ctypes.WinDLL("kernel32.dll")
func = libc.GetCurrentProcessId
else:
libc = ctypes.CDLL(None)
func = libc.getpid

func.argtypes = (BadType,)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can use the Python C API:

Suggested change
if os.name == "nt":
libc = ctypes.WinDLL("kernel32.dll")
func = libc.GetCurrentProcessId
else:
libc = ctypes.CDLL(None)
func = libc.getpid
func.argtypes = (BadType,)
func = ctypes.pythonapi.Py_GetVersion
func.argtypes = (BadType,)

ptr.set_type(c_int)
self.assertIs(ptr._type_, c_int)

class TestPointerStringProto(unittest.TestCase):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would prefer to add the test to the end of PointersTestCase, instead of adding a new test case.

import gc
import sys
import unittest
import os
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This import can be removed with my proposed code.

VanshAgarwal24036 and others added 5 commits January 23, 2026 23:14
Co-authored-by: Victor Stinner <vstinner@python.org>
Co-authored-by: Victor Stinner <vstinner@python.org>
Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vstinner vstinner added needs backport to 3.13 bugs and security fixes needs backport to 3.14 bugs and security fixes labels Jan 26, 2026
@vstinner vstinner merged commit 8f45925 into python:main Jan 26, 2026
55 checks passed
@miss-islington-app
Copy link

Thanks @VanshAgarwal24036 for the PR, and @vstinner for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13, 3.14.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Jan 26, 2026
…ythonGH-144108)

(cherry picked from commit 8f45925)

Co-authored-by: VanshAgarwal24036 <148854295+VanshAgarwal24036@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
@miss-islington-app
Copy link

Sorry, @VanshAgarwal24036 and @vstinner, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker 8f459255eba2b6639f1912e5c5e318a7cdafada1 3.13

@bedevere-app
Copy link

bedevere-app bot commented Jan 26, 2026

GH-144244 is a backport of this pull request to the 3.14 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.14 bugs and security fixes label Jan 26, 2026
@bedevere-app
Copy link

bedevere-app bot commented Jan 26, 2026

GH-144245 is a backport of this pull request to the 3.13 branch.

@bedevere-app bedevere-app bot removed the needs backport to 3.13 bugs and security fixes label Jan 26, 2026
@vstinner
Copy link
Member

Merged. Thanks for the fix.

@VanshAgarwal24036
Copy link
Contributor Author

Thanks for guidance and review

@VanshAgarwal24036 VanshAgarwal24036 deleted the gh-144100-ctypes-pointer-protol branch January 26, 2026 12:36
vstinner added a commit that referenced this pull request Jan 26, 2026
…H-144108) (#144244)

gh-144100: Fix crash for POINTER(str) used in ctypes argtypes (GH-144108)
(cherry picked from commit 8f45925)

Co-authored-by: VanshAgarwal24036 <148854295+VanshAgarwal24036@users.noreply.github.com>
Co-authored-by: Victor Stinner <vstinner@python.org>
vstinner added a commit that referenced this pull request Jan 27, 2026
…144108) (#144245)

gh-144100: Fix crash for POINTER(str) used in ctypes argtypes (#144108)


(cherry picked from commit 8f45925)

Co-authored-by: VanshAgarwal24036 <148854295+VanshAgarwal24036@users.noreply.github.com>
thunder-coding pushed a commit to thunder-coding/cpython that referenced this pull request Feb 15, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

X Tutup