classification
Title: ctypes: Crash if manually-created CField instance is used
Type: crash Stage: patch review
Components: ctypes Versions: Python 3.8, Python 3.7, Python 3.6, Python 2.7
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, belopolsky, berker.peksag, izbyshev, meador.inge, serhiy.storchaka, shihai1991, xtreak
Priority: normal Keywords: patch

Created on 2018-09-15 17:13 by izbyshev, last changed 2019-08-03 06:03 by shihai1991.

Pull Requests
URL Status Linked Edit
PR 14837 open shihai1991, 2019-07-18 12:22
Messages (4)
msg325448 - (view) Author: Alexey Izbyshev (izbyshev) * (Python triager) Date: 2018-09-15 17:13
It is possible to manually create an instance of private CField type which is used by ctypes to represent fields of Structure and Union types. This instance will be uninitialized because it's normally initialized when instances of Structure/Union are created, so calling its methods may crash the interpreter:

from ctypes import *

class S(Structure):
    _fields_ = [('x', c_int)]

CField = type(S.x)
f = CField()
repr(f) # Crash here

Is this issue worth fixing?

If so, is the correct way to set tp_new slot to NULL and fix the internal callers so that users wouldn't be able to create CField instances?
msg347814 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2019-07-13 13:18
> Is this issue worth fixing?

Definitely yes.

> If so, is the correct way to set tp_new slot to NULL and fix the internal callers so that users wouldn't be able to create CField instances?

I think yes.

Do you mind to create a PR?
msg348113 - (view) Author: hai shi (shihai1991) * Date: 2019-07-18 12:33
I have try to cancel this PyCField_new function, but i am not sure I haven't break the code structure.
msg348944 - (view) Author: hai shi (shihai1991) * Date: 2019-08-03 06:03
ping
History
Date User Action Args
2019-08-03 06:03:24shihai1991setmessages: + msg348944
2019-07-18 12:33:55shihai1991setnosy: + shihai1991
messages: + msg348113
2019-07-18 12:22:04shihai1991setkeywords: + patch
stage: patch review
pull_requests: + pull_request14628
2019-07-13 13:18:37serhiy.storchakasetmessages: + msg347814
2019-07-10 12:06:57shihai1991setnosy: - shihai1991
2019-07-10 04:42:52shihai1991setnosy: + shihai1991
2018-09-16 07:45:55xtreaksetnosy: + xtreak
2018-09-15 17:13:13izbyshevcreate