⚡ Sangsakala Proxy 🏠 Home
Skip to content

gh-160708: Use the type size for resized ctypes simple objects - #160720

Open
iamsaugatpandey wants to merge 1 commit into
python:mainfrom
iamsaugatpandey:gh-160708-ctypes-resized-simple
Open

iamsaugatpandey wants to merge 1 commit into
python:mainfrom
iamsaugatpandey:gh-160708-ctypes-resized-simple

Conversation

@iamsaugatpandey

@iamsaugatpandey iamsaugatpandey commented Oct 26, 2026 •

Copy link
Copy Markdown

After ctypes.resize(), the buffer of a simple ctypes object is bigger than the type itself. Two places still used the buffer size (b_size) where they needed the type size:

  • Simple_bool_impl() compared b_size bytes against a 16-byte zero string literal. For a resized object it reads past the literal, and it can also return the wrong answer (bool(c_int(0)) is True if the extra space isn't zero).
  • PyCSimpleType_paramfunc() copied b_size bytes into PyCArgObject.value, which is a fixed-size union, so passing a resized object to a foreign function overwrote the heap.

Both now use info->size, the same as the value getter since gh-159739. bool() checks the value's bytes in a loop instead of comparing with the literal. This also covers simple types larger than 16 bytes, such as c_longdouble_complex on x86-64.

Tests: bool() on a resized c_int whose extra space is non-zero, a call to tf_i with a resized c_int, and bool() on the complex types.

After ctypes.resize(), a simple object's buffer is larger than its
type. bool() and passing the object to a foreign function still used
the buffer size, so bool() read past a 16-byte string literal and the
call copied the whole buffer into the fixed-size PyCArgObject value,
corrupting the heap.

Use the type's size instead, like the value getter does since
pythongh-159739. bool() now checks the value's bytes directly, which also
covers simple types larger than 16 bytes such as c_longdouble_complex
on x86-64.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Copilot was unable to review this pull request because the user who requested the review has insufficient remaining quota.

@iamsaugatpandey

Copy link
Copy Markdown
Author

Hi @JelleZijlstra, since you reviewed gh-159739 (#159767, reading resized ctypes scalars using their declared size): this PR is the follow-up for the two places that still use b_size after ctypes.resize(). Those are bool(), which reads past a 16-byte literal, and passing the object to a foreign function, which overflows PyCArgObject.value and corrupts the heap. Both now use info->size, like the value getter. CI is green. Would you have time to take a look? cc @ZeroIntensity

Comment thread Modules/_ctypes/_ctypes.c
Comment on lines +5538 to +5540
assert(info); /* Cannot be NULL for CDataObject instances */

/* Only look at the value, not at extra space added by resize(). */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

These comments don't seem very helpful:

Suggested change
assert(info); /* Cannot be NULL for CDataObject instances */
/* Only look at the value, not at extra space added by resize(). */
assert(info != NULL);

Comment thread Modules/_ctypes/_ctypes.c
assert(info); /* Cannot be NULL for CDataObject instances */

/* Only look at the value, not at extra space added by resize(). */
for (Py_ssize_t i = 0; i < info->size; i++) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It'd be nice to have an assertion documenting the invariant:

Suggested change
for (Py_ssize_t i = 0; i < info->size; i++) {
assert(info->size <= self->b_size);
for (Py_ssize_t i = 0; i < info->size; i++) {

@ZeroIntensity ZeroIntensity added needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes labels Nov 4, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

awaiting merge needs backport to 3.14 bugs and security fixes needs backport to 3.15 bugs and security fixes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants