GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Structure field size/ofs __str__ wrong with large size fields · Issue #70045 · python/cpython

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

New issue
Copy link
New issue
Copy link
Closed
Closed
Structure field size/ofs __str__ wrong with large size fields#70045
Copy link
Labels
3.10 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifetopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

Description

CharlesMachalow
mannequin
opened on Dec 14, 2015
Issue body actions
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

    1. CharlesMachalow commented on Dec 14, 2015

      @CharlesMachalow
      CharlesMachalowmannequin
      on Dec 14, 2015
      MannequinAuthor
      More actions

      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

    2. CharlesMachalow
      added
      topic-ctypes
      type-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error
      on Dec 14, 2015
    3. CharlesMachalow commented on Dec 14, 2015

      @CharlesMachalow
      CharlesMachalowmannequin
      on Dec 14, 2015
      MannequinAuthor
      More actions

      Adding file with code to reproduce.

    4. iritkatriel
      added
      3.8 (EOL)end of lifeend of life
      3.9 (EOL)end of lifeend of life
      3.10 (EOL)end of lifeend of life
      on Nov 8, 2020
    5. ezio-melotti
      transferred this issue fromon Apr 10, 2022
    6. serhiy-storchaka
      added this to Ctypes issueson Jan 1, 2024
    7. encukou commented on May 13, 2025

      @encukou
      encukou
      on May 13, 2025
      Member
      More actions

      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>
      
    8. encukou
      closed this as completedon May 13, 2025
    9. github-project-automation
      moved this to Done in Ctypes issueson May 13, 2025
    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.10 (EOL)end of lifeend of life3.8 (EOL)end of lifeend of life3.9 (EOL)end of lifeend of lifetopic-ctypestype-bugAn unexpected behavior, bug, or errorAn unexpected behavior, bug, or error

      Projects

      • Ctypes issues
        Status
        Done
        Show more project fields

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      You are visitor 01402520 — GeezerVison does not store the pages you look up.
      Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.