Skip to content

Commit bef2815

Browse files
miss-islingtonvsajip
authored andcommitted
bpo-16576: Add checks for bitfields passed by value to functions. (GH-17097) (GH-17223)
(cherry picked from commit 1062715)
1 parent 21eb731 commit bef2815

File tree

3 files changed

+141
-3
lines changed

3 files changed

+141
-3
lines changed

Lib/ctypes/test/test_structures.py

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -651,6 +651,87 @@ class Test5(Structure):
651651
self.assertEqual(test5.nested.an_int, 0)
652652
self.assertEqual(test5.another_int, 0)
653653

654+
#@unittest.skipIf('s390' in MACHINE, 'Test causes segfault on S390')
655+
def test_bitfield_by_value(self):
656+
# See bpo-16576
657+
658+
# These should mirror the structures in Modules/_ctypes/_ctypes_test.c
659+
660+
class Test6(Structure):
661+
_fields_ = [
662+
('A', c_int, 1),
663+
('B', c_int, 2),
664+
('C', c_int, 3),
665+
('D', c_int, 2),
666+
]
667+
668+
test6 = Test6()
669+
# As these are signed int fields, all are logically -1 due to sign
670+
# extension.
671+
test6.A = 1
672+
test6.B = 3
673+
test6.C = 7
674+
test6.D = 3
675+
dll = CDLL(_ctypes_test.__file__)
676+
with self.assertRaises(TypeError) as ctx:
677+
func = dll._testfunc_bitfield_by_value1
678+
func.restype = c_long
679+
func.argtypes = (Test6,)
680+
result = func(test6)
681+
self.assertEqual(ctx.exception.args[0], 'item 1 in _argtypes_ passes '
682+
'a struct/union with a bitfield by value, which is '
683+
'unsupported.')
684+
# passing by reference should be OK
685+
func = dll._testfunc_bitfield_by_reference1
686+
func.restype = c_long
687+
func.argtypes = (POINTER(Test6),)
688+
result = func(byref(test6))
689+
self.assertEqual(result, -4)
690+
self.assertEqual(test6.A, 0)
691+
self.assertEqual(test6.B, 0)
692+
self.assertEqual(test6.C, 0)
693+
self.assertEqual(test6.D, 0)
694+
695+
class Test7(Structure):
696+
_fields_ = [
697+
('A', c_uint, 1),
698+
('B', c_uint, 2),
699+
('C', c_uint, 3),
700+
('D', c_uint, 2),
701+
]
702+
test7 = Test7()
703+
test7.A = 1
704+
test7.B = 3
705+
test7.C = 7
706+
test7.D = 3
707+
func = dll._testfunc_bitfield_by_reference2
708+
func.restype = c_long
709+
func.argtypes = (POINTER(Test7),)
710+
result = func(byref(test7))
711+
self.assertEqual(result, 14)
712+
self.assertEqual(test7.A, 0)
713+
self.assertEqual(test7.B, 0)
714+
self.assertEqual(test7.C, 0)
715+
self.assertEqual(test7.D, 0)
716+
717+
# for a union with bitfields, the union check happens first
718+
class Test8(Union):
719+
_fields_ = [
720+
('A', c_int, 1),
721+
('B', c_int, 2),
722+
('C', c_int, 3),
723+
('D', c_int, 2),
724+
]
725+
726+
test8 = Test8()
727+
with self.assertRaises(TypeError) as ctx:
728+
func = dll._testfunc_bitfield_by_value2
729+
func.restype = c_long
730+
func.argtypes = (Test8,)
731+
result = func(test8)
732+
self.assertEqual(ctx.exception.args[0], 'item 1 in _argtypes_ passes '
733+
'a union by value, which is unsupported.')
734+
654735
class PointerMemberTestCase(unittest.TestCase):
655736

656737
def test(self):

Modules/_ctypes/_ctypes.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2400,11 +2400,18 @@ converters_from_argtypes(PyObject *ob)
24002400
}
24012401
return NULL;
24022402
}
2403-
/*
24042403
if (stgdict->flags & TYPEFLAG_HASBITFIELD) {
2405-
printf("found stgdict with bitfield\n");
2404+
Py_DECREF(converters);
2405+
Py_DECREF(ob);
2406+
if (!PyErr_Occurred()) {
2407+
PyErr_Format(PyExc_TypeError,
2408+
"item %zd in _argtypes_ passes a struct/"
2409+
"union with a bitfield by value, which is "
2410+
"unsupported.",
2411+
i + 1);
2412+
}
2413+
return NULL;
24062414
}
2407-
*/
24082415
}
24092416

24102417
if (_PyObject_LookupAttrId(tp, &PyId_from_param, &cnv) <= 0) {

Modules/_ctypes/_ctypes_test.c

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,56 @@ _testfunc_union_by_reference3(Test5 *in) {
198198
return result;
199199
}
200200

201+
typedef struct {
202+
signed int A: 1, B:2, C:3, D:2;
203+
} Test6;
204+
205+
EXPORT(long)
206+
_testfunc_bitfield_by_value1(Test6 in) {
207+
long result = in.A + in.B + in.C + in.D;
208+
209+
/* As the struct is passed by value, changes to it shouldn't be
210+
* reflected in the caller.
211+
*/
212+
memset(&in, 0, sizeof(in));
213+
return result;
214+
}
215+
216+
EXPORT(long)
217+
_testfunc_bitfield_by_reference1(Test6 *in) {
218+
long result = in->A + in->B + in->C + in->D;
219+
220+
memset(in, 0, sizeof(Test6));
221+
return result;
222+
}
223+
224+
typedef struct {
225+
unsigned int A: 1, B:2, C:3, D:2;
226+
} Test7;
227+
228+
EXPORT(long)
229+
_testfunc_bitfield_by_reference2(Test7 *in) {
230+
long result = in->A + in->B + in->C + in->D;
231+
232+
memset(in, 0, sizeof(Test7));
233+
return result;
234+
}
235+
236+
typedef union {
237+
signed int A: 1, B:2, C:3, D:2;
238+
} Test8;
239+
240+
EXPORT(long)
241+
_testfunc_bitfield_by_value2(Test8 in) {
242+
long result = in.A + in.B + in.C + in.D;
243+
244+
/* As the struct is passed by value, changes to it shouldn't be
245+
* reflected in the caller.
246+
*/
247+
memset(&in, 0, sizeof(in));
248+
return result;
249+
}
250+
201251
EXPORT(void)testfunc_array(int values[4])
202252
{
203253
printf("testfunc_array %d %d %d %d\n",

0 commit comments

Comments
 (0)