classification
Title: segfault in ctypes.Struct with bad _fields_
Type: crash Stage: committed/rejected
Components: ctypes Versions: Python 3.4, Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: amaury.forgeotdarc, meador.inge, python-dev, vladris
Priority: normal Keywords: easy, patch

Created on 2011-08-16 20:21 by amaury.forgeotdarc, last changed 2011-09-03 03:26 by meador.inge. This issue is now closed.

Files
File name Uploaded Description Edit
issue12764_patch3x.diff vladris, 2011-08-17 16:02 Patch for 3.3 review
issue12764_patch2x.diff vladris, 2011-08-17 16:02 Patch for 2.7 review
Messages (7)
msg142218 - (view) Author: Amaury Forgeot d'Arc (amaury.forgeotdarc) * (Python committer) Date: 2011-08-16 20:21
This crashes on python 3.3::

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

This also crashes on python 2.7::

   class S(ctypes.Structure):
       _fields_ = [(u'x\xe9', ctypes.c_int)]

The cause is the same: in Modules/_ctypes/stgdict.c, the assignment 
    fieldname = _PyUnicode_AsString(name);
does not check for errors.
msg142284 - (view) Author: Vlad Riscutia (vladris) Date: 2011-08-17 16:02
Attached patch for 3.3 with unittest
msg142285 - (view) Author: Vlad Riscutia (vladris) Date: 2011-08-17 16:02
Also patch for 2.7 with unittest. BTW, b"x" works on 2.7.
msg143375 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-09-02 03:45
Vlad,

Thanks for the patch.  A few nits:

   1. The test case is in 'test_bitfields.py'.
      I think it should go in 'test_structures.py'.

   2. The test case would probably be cleaner using a 'with' context
      manager:

         with self.assertRaises(TypeError):
               class S(Structure):
                   _fields_ = [(b"x", c_int)]

      A few more test cases might be nice too.

   3. The TypeError message display something like:

      "structure field name must be string not bytes"

      maybe the following would be more understandable:

      "field name must be an object of type str not bytes"

   4. The 'ptr', 'len', and 'buf' initializers are unnecessary.

Otherwise, looks good.
msg143430 - (view) Author: Roundup Robot (python-dev) Date: 2011-09-02 18:44
New changeset b8acee08283c by Amaury Forgeot d'Arc in branch '3.2':
Issue #12764: Fix a crash in ctypes when the name of a Structure field is not
http://hg.python.org/cpython/rev/b8acee08283c

New changeset 1ed1ea0f4cd8 by Amaury Forgeot d'Arc in branch 'default':
Merge from 3.2: Issue #12764: Fix a crash in ctypes when the name of a
http://hg.python.org/cpython/rev/1ed1ea0f4cd8
msg143431 - (view) Author: Roundup Robot (python-dev) Date: 2011-09-02 18:44
New changeset 73827c23cdde by Amaury Forgeot d'Arc in branch '2.7':
Issue #12764: Fix a crash in ctypes when the name of a Structure field is not
http://hg.python.org/cpython/rev/73827c23cdde
msg143450 - (view) Author: Meador Inge (meador.inge) * (Python committer) Date: 2011-09-03 03:26
This has been fixed.  I verified tip and 2.7.
History
Date User Action Args
2011-09-03 03:26:48meador.ingesetstatus: open -> closed
resolution: fixed
messages: + msg143450

stage: patch review -> committed/rejected
2011-09-02 18:44:15python-devsetmessages: + msg143431
2011-09-02 18:44:13python-devsetnosy: + python-dev
messages: + msg143430
2011-09-02 03:45:39meador.ingesetversions: + Python 3.3, Python 3.4
nosy: + meador.inge

messages: + msg143375

stage: patch review
2011-08-17 16:02:39vladrissetfiles: + issue12764_patch2x.diff

messages: + msg142285
2011-08-17 16:02:01vladrissetfiles: + issue12764_patch3x.diff

nosy: + vladris
messages: + msg142284

keywords: + patch
2011-08-16 20:21:56amaury.forgeotdarccreate