-
-
Notifications
You must be signed in to change notification settings - Fork 34.5k
bpo-29753: fix merging packed bitfields in ctypes struct/union #19850
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
2b159a0
80ee351
192e4b9
de3df46
9f015f5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
…tfields Signed-off-by: Filipe Laíns <lains@riseup.net>
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -73,7 +73,9 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index, | |
| } | ||
|
|
||
| #ifndef MS_WIN32 | ||
| if (pack && bitsize) { /* packed bitfield */ | ||
| /* if we have a packed bitfield, calculate the minimum number of bytes we | ||
| need to fit it. otherwise use the specified size. */ | ||
| if (pack && bitsize) { | ||
| size = (bitsize - 1) / 8 + 1; | ||
| } else | ||
| #endif | ||
|
|
@@ -97,6 +99,8 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index, | |
| } else if (bitsize /* this is a bitfield request */ | ||
| && *pfield_size /* we have a bitfield open */ | ||
| && dict->size * 8 >= *pfield_size | ||
| /* if this is a packed bitfield, always expand it. | ||
| otherwise calculate if we need to expand it. */ | ||
| && (((*pbitofs + bitsize) <= dict->size * 8) || pack)) { | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. When we have a packed bitfield and it doesn't fit the current open bitfield, always expand it.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm quite convinced this comment should go in the source code. |
||
| /* expand bit field */ | ||
| fieldtype = EXPAND_BITFIELD; | ||
|
|
@@ -105,6 +109,8 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index, | |
| /* start new bitfield */ | ||
| fieldtype = NEW_BITFIELD; | ||
| *pbitofs = 0; | ||
| /* use our calculated size (size) instead of type size (dict->size), | ||
| which can be different for packed bitfields */ | ||
| *pfield_size = size * 8; | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Start a new bitfield with the size we calculated above. This means if it is a packed bitfield we start a new bitfield with only the needed size, not the specified field size.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto here. |
||
| } else { | ||
| /* not a bit field */ | ||
|
|
@@ -177,6 +183,9 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index, | |
| break; | ||
|
|
||
| case EXPAND_BITFIELD: | ||
| /* increase the size if it is a packed bitfield. | ||
| EXPAND_BITFIELD should not be selected for non-packed fields if the | ||
| current size isn't already enough. */ | ||
| if (pack) | ||
|
FFY00 marked this conversation as resolved.
Outdated
|
||
| size = (*pbitofs + bitsize - 1) / 8 + 1; | ||
|
|
||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
(copying the old comment, so that it shows up in the github editor for reviewers)
If we have a packed bitfield, calculate the minimum number of bytes needed to fit the bitfield.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'd follow this rule: if a comment is necessary/helpful/crucial for reviewers it should be in the source code so that everyone looking at this code at any point later on sees it, not in GitHub comments.