Skip to content

Structure field size/ofs __str__ wrong with large size fields #70045

Closed
@CharlesMachalow

Description

@CharlesMachalow
BPO 25858
Files
  • ctypesBug.py
  • Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.

    Show more details

    GitHub fields:

    assignee = None
    closed_at = None
    created_at = <Date 2015-12-14.02:52:09.933>
    labels = ['ctypes', 'type-bug', '3.8', '3.9', '3.10']
    title = 'Structure field size/ofs __str__ wrong with large size fields'
    updated_at = <Date 2020-11-08.18:15:58.491>
    user = 'https://bugs.python.org/CharlesMachalow'

    bugs.python.org fields:

    activity = <Date 2020-11-08.18:15:58.491>
    actor = 'iritkatriel'
    assignee = 'none'
    closed = False
    closed_date = None
    closer = None
    components = ['ctypes']
    creation = <Date 2015-12-14.02:52:09.933>
    creator = 'Charles Machalow'
    dependencies = []
    files = ['41299']
    hgrepos = []
    issue_num = 25858
    keywords = []
    message_count = 2.0
    messages = ['256359', '256361']
    nosy_count = 1.0
    nosy_names = ['Charles Machalow']
    pr_nums = []
    priority = 'normal'
    resolution = None
    stage = None
    status = 'open'
    superseder = None
    type = 'behavior'
    url = 'https://bugs.python.org/issue25858'
    versions = ['Python 3.8', 'Python 3.9', 'Python 3.10']

    Activity

    CharlesMachalow

    CharlesMachalow commented on Dec 14, 2015

    @CharlesMachalow
    MannequinAuthor

    Large sized fields in Structures lead to incorrect string representations because it is assuming that because the size is so large, it must be a bit field instead of a byte field.

    class bugStruct(Structure):
        _pack_ = 1
        _fields_ = [
                    ("correct", c_uint8 * 65535),
                    ("wrongSizeAndOffset", c_uint8 * 999999),
                   ]
    
    print(str(bugStruct.correct))
    <Field type=c_ubyte_Array_65535, ofs=0, size=65535> # Correct
    
    print(str(bugStruct.wrongSizeAndOffset))
    <Field type=c_ubyte_Array_999999, ofs=65535:16959, bits=15> # Incorrect
    # Should be: <Field type=c_ubyte_Array_999999, ofs=65535, size=999999>

    To get the math for this do the following on an incorrect size/offset:
    size = (bits << 16) + ofs.split(":")[1]
    ofs = ofs.split(":")[0]
    Though this isn't really safe because the field may actually be bit-sized

    CharlesMachalow

    CharlesMachalow commented on Dec 14, 2015

    @CharlesMachalow
    MannequinAuthor

    Adding file with code to reproduce.

    transferred this issue fromon Apr 10, 2022
    encukou

    encukou commented on May 13, 2025

    @encukou
    Member

    Fixed in #97702: [edit: fixed number]

    >>> from ctypes import *
    >>> class bugStruct(Structure):
    ...     _pack_ = 1
    ...     _fields_ = [
    ...                 ("correct", c_uint8 * 65535),
    ...                 ("wrongSizeAndOffset", c_uint8 * 999999),
    ...                ]
    ... 
    >>> print(str(bugStruct.correct))
    <ctypes.CField 'correct' type=c_ubyte_Array_65535, ofs=0, size=65535>
    >>> print(str(bugStruct.wrongSizeAndOffset))
    <ctypes.CField 'wrongSizeAndOffset' type=c_ubyte_Array_999999, ofs=65535, size=999999>
    
    Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

    Metadata

    Metadata

    Assignees

    No one assigned

      Labels

      3.10only security fixes3.8 (EOL)end of life3.9only security fixestopic-ctypestype-bugAn unexpected behavior, bug, or error

      Projects

      No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

        Structure field size/ofs __str__ wrong with large size fields · Issue #70045 · python/cpython

        Follow Lee on X/Twitter - Father, Husband, Serial builder creating AI, crypto, games & web tools. We are friends :) AI Will Come To Life!

        Check out: eBank.nz (Art Generator) | Netwrck.com (AI Tools) | Text-Generator.io (AI API) | BitBank.nz (Crypto AI) | ReadingTime (Kids Reading) | RewordGame | BigMultiplayerChess | WebFiddle | How.nz | Helix AI Assistant