@@ -73,7 +73,9 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
73
73
}
74
74
75
75
#ifndef MS_WIN32
76
- if (pack && bitsize ) { /* packed bitfield */
76
+ /* if we have a packed bitfield, calculate the minimum number of bytes we
77
+ need to fit it. otherwise use the specified size. */
78
+ if (pack && bitsize ) {
77
79
size = (bitsize - 1 ) / 8 + 1 ;
78
80
} else
79
81
#endif
@@ -97,6 +99,8 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
97
99
} else if (bitsize /* this is a bitfield request */
98
100
&& * pfield_size /* we have a bitfield open */
99
101
&& dict -> size * 8 >= * pfield_size
102
+ /* if this is a packed bitfield, always expand it.
103
+ otherwise calculate if we need to expand it. */
100
104
&& (((* pbitofs + bitsize ) <= dict -> size * 8 ) || pack )) {
101
105
/* expand bit field */
102
106
fieldtype = EXPAND_BITFIELD ;
@@ -105,6 +109,8 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
105
109
/* start new bitfield */
106
110
fieldtype = NEW_BITFIELD ;
107
111
* pbitofs = 0 ;
112
+ /* use our calculated size (size) instead of type size (dict->size),
113
+ which can be different for packed bitfields */
108
114
* pfield_size = size * 8 ;
109
115
} else {
110
116
/* not a bit field */
@@ -177,6 +183,9 @@ PyCField_FromDesc(PyObject *desc, Py_ssize_t index,
177
183
break ;
178
184
179
185
case EXPAND_BITFIELD :
186
+ /* increase the size if it is a packed bitfield.
187
+ EXPAND_BITFIELD should not be selected for non-packed fields if the
188
+ current size isn't already enough. */
180
189
if (pack )
181
190
size = (* pbitofs + bitsize - 1 ) / 8 + 1 ;
182
191
0 commit comments