FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

gh-126937: ctypes: fix TypeError when a field's size is >65535 bytes by Melissa0x1f992 · Pull Request #126938 · python/cpython · GitHub

/ cpython Public

gh-126937: ctypes: fix TypeError when a field's size is >65535 bytes - #126938

Merged
encukou merged 20 commits into
python:mainfrom
Melissa0x1f992:main
Dec 10, 2024
Merged

gh-126937: ctypes: fix TypeError when a field's size is >65535 bytes#126938
encukou merged 20 commits into
python:mainfrom
Melissa0x1f992:main

Conversation

Melissa0x1f992 commented Nov 17, 2024
edited by bedevere-app Bot
Loading

Copy link
Copy Markdown
Contributor

Used the bit_size_obj as seems to be intended, based on its usage in Lib/ctypes/_layout.py

Tested the change locally informally. Didn't write any tests.

ghost commented Nov 17, 2024
edited by ghost
Loading

Copy link
Copy Markdown

All commit authors signed the Contributor License Agreement.

bedevere-app Bot commented Nov 17, 2024

Copy link
Copy Markdown

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

Copy link
Copy Markdown
Contributor Author

Need some help understanding the lint error on this

Comment thread Modules/_ctypes/cfield.c Outdated

terryjreedy commented Nov 17, 2024
edited
Loading

Copy link
Copy Markdown
Member

By somewhat random clicking of the search box and the test stage line, I got the full log. There were 6 spaces after the final \n, thus trailing spaces failed. (Note that running patchcheck as suggested somewhere would have fixed this.)

I asked to rerun just the one test, but the edit triggered all.

Copy link
Copy Markdown
Member

I can't review the actual change. It might help someone else if you could connect the change to the change in 3.14 that caused the regression. To find the latter, I would first run git blame on cfield.c

picnixz commented Nov 18, 2024

Copy link
Copy Markdown
Member

cc @encukou

Copy link
Copy Markdown
Contributor Author

Added a test. It doesn't have any assertions bc the failure case is an error getting raised. Not sure if this is the appropriate way to add such a test.

Comment thread .pre-commit-config.yaml

Copy link
Copy Markdown
Contributor Author

For the Win32, I don't have that setup. Could I get some help determining what field size should pass the < (1ULL << (8*sizeof(Py_ssize_t)-1)) / 8) test on Win32? Am I off by one? I don't want to commit (2^31 - 1 - 1) unless there's a good reason to consider that one byte smaller the limit for Win32.

Copy link
Copy Markdown
Contributor Author

Is there a way for the test to set up different field sizes based on the current platform, and is that desirable? I'd like to test the actual max size, regardless of platform, rather than the smallest of the different platform maxes.

ZeroIntensity left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Some initial comments :)

@@ -0,0 +1 @@
Fixed TypeError when a Structure's field's size is >65535 bytes

ZeroIntensity Nov 18, 2024
edited
Loading

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality
Suggested change
Fixed TypeError when a Structure's field's size is >65535 bytes
Fix :exc:`TypeError` when a :class:`ctypes.Structure` has a field size that doesn't fit into an unsigned 16-bit integer.

Comment thread Modules/_ctypes/cfield.c Outdated
Py_ssize_t bit_size = NUM_BITS(size);
if (bit_size) {
if (bit_size_obj != Py_None) {

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

No need for a newline

Suggested change

Comment thread Modules/_ctypes/cfield.c Outdated
Py_ssize_t bit_size;

if (PyLong_Check(bit_size_obj)) {
bit_size = PyLong_AsSsize_t(bit_size_obj);

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

PyLong_AsSsize_t can fail, you need to check for < 0 and then goto error.

Comment thread Modules/_ctypes/cfield.c Outdated
if (bit_size) {
if (bit_size_obj != Py_None) {

Py_ssize_t bit_size;

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Lint is complaining about this. I'm assuming this just needs to get moved to before the if

Comment thread .pre-commit-config.yaml Outdated
Comment thread .pre-commit-config.yaml Outdated
vstinner changed the title gh-126937: Fix TypeError when a field's size is >65535 bytes gh-126937, ctypes: Fix TypeError when a field's size is >65535 bytes Nov 19, 2024
Co-Authorsd-By: Peter Bierma <zintensitydev@gmail.com>

encukou commented Dec 4, 2024

Copy link
Copy Markdown
Member

OK. Thank you for the fix!
I hope you don't mind me pushing to your branch -- this way I can keep changes in this PR, with the discussion.

encukou commented Dec 9, 2024

Copy link
Copy Markdown
Member

@serhiy-storchaka, should I wait for your review?

Copy link
Copy Markdown
Member

I am looking.

serhiy-storchaka left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Ah, this is the same PR. As I said in the issue, it fixes some symptoms, but there is a deeper issue.

Comment thread Modules/_ctypes/stgdict.c
goto error;
}
Py_ssize_t total_size = PyLong_AsInt(tmp);
Py_ssize_t total_size = PyLong_AsSsize_t(tmp);

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

What was a reason for using PyLong_AsInt(). This is a new API, so it unlikely was accident. Is there a limitation on total_size to be in the range of int?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It was an accident -- my mistake.

Comment thread Modules/_ctypes/cfield.c
// Currently, the bit size is specified redundantly
// in NUM_BITS(size) and bit_size_obj.
// Verify that they match.
assert(PyLong_AsSsize_t(bit_size_obj) == bit_size);

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

Can this be guaranteed? What if bit_size_obj is not None and is not an int?

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

It can.
The implementation is split between internal Python code in ctypes._layout, and C code here. They are currently tightly coupled. The assert also verifies the Python part.

encukou commented Dec 9, 2024

Copy link
Copy Markdown
Member

As I said in the issue, it fixes some symptoms, but there is a deeper issue.

Yes. The important thing in this PR is the regression test (and fixing the test for now).
I plan to improve the code further: see #127297 for the next step, a refactoring to make future work easier. After that's in I plan to split up the size and bitfield info.

vstinner left a comment

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.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

LGTM

encukou merged commit cef0a90 into python:main Dec 10, 2024
serhiy-storchaka added needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes labels Dec 10, 2024

Copy link
Copy Markdown

Thanks @Melissa0x1f992 for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.13.
🐍🍒⛏🤖

Copy link
Copy Markdown

Thanks @Melissa0x1f992 for the PR, and @encukou for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12.
🐍🍒⛏🤖

Copy link
Copy Markdown

Sorry, @Melissa0x1f992 and @encukou, I could not cleanly backport this to 3.13 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker cef0a90d8f3a94aa534593f39b4abf98165675b9 3.13

Copy link
Copy Markdown

Sorry, @Melissa0x1f992 and @encukou, I could not cleanly backport this to 3.12 due to a conflict.
Please backport using cherry_picker on command line.

cherry_picker cef0a90d8f3a94aa534593f39b4abf98165675b9 3.12

serhiy-storchaka removed needs backport to 3.12 only security fixes needs backport to 3.13 bugs and security fixes labels Dec 10, 2024
encukou added a commit to encukou/cpython that referenced this pull request Dec 11, 2024
…ythonGH-126938)

This backports the test from pythonGH-126938, with changed limit and exception class.

Co-authored-by: Melissa0x1f992 <70096546+Melissa0x1f992@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Co-authored-by: Petr Viktorin <encukou@gmail.com>

bedevere-app Bot commented Dec 11, 2024

Copy link
Copy Markdown

GH-127825 is a backport of this pull request to the 3.13 branch.

encukou added a commit that referenced this pull request Dec 12, 2024
…GH-126938) (GH-127825)

This backports the *test* from GH-126938, with changed limit and exception class.

Co-authored-by: Melissa0x1f992 <70096546+Melissa0x1f992@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
encukou added a commit to encukou/cpython that referenced this pull request Dec 13, 2024
… field (pythonGH-126938) (pythonGH-127825)

This backports the *test* from pythonGH-126938, with changed limit and exception class.

Co-authored-by: Melissa0x1f992 <70096546+Melissa0x1f992@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>

(cherry-picked from d51c144)
encukou added a commit that referenced this pull request Dec 13, 2024
…GH-126938) (GH-127825) (GH-127909)

This backports the *test* from GH-126938, with changed limit and exception class.

Co-authored-by: Melissa0x1f992 <70096546+Melissa0x1f992@users.noreply.github.com>
Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>

(cherry-picked from d51c144)
srinivasreddy pushed a commit to srinivasreddy/cpython that referenced this pull request Jan 8, 2025
…bytes (pythonGH-126938)


Co-authored-by: Peter Bierma <zintensitydev@gmail.com>
Co-authored-by: Terry Jan Reedy <tjreedy@udel.edu>
Co-authored-by: Petr Viktorin <encukou@gmail.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

9 participants


Back | FazBrowse Home | New Git URL