Skip to content

Commit ea2c45e

Browse files
committed
Reject arrays for incomplete ctypes pointer fields
Assisted-by: Codex:GPT-6
1 parent 6dd75f6 commit ea2c45e

2 files changed

Lines changed: 29 additions & 6 deletions

File tree

‎crates/vm/src/stdlib/_ctypes/base.rs‎

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,12 +1093,15 @@ impl PyCData {
10931093
.class()
10941094
.fast_issubclass(super::pointer::PyCPointerType::static_type())
10951095
&& let Some(array) = value.downcast_ref::<super::array::PyCArray>()
1096-
&& proto_type.stg_info(vm)?.proto().is(array
1097-
.class()
1098-
.stg_info(vm)?
1099-
.element_type
1100-
.as_deref()
1101-
.expect("array has element type"))
1096+
&& {
1097+
let target_info = proto_type.stg_info(vm)?;
1098+
let array_info = array.class().stg_info(vm)?;
1099+
target_info
1100+
.proto
1101+
.as_deref()
1102+
.zip(array_info.element_type.as_deref())
1103+
.is_some_and(|(target, element)| target.is(element))
1104+
}
11021105
{
11031106
let buffer_addr = {
11041107
let array_buffer = array.0.buffer.read();

‎extra_tests/snippets/stdlib_ctypes.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -835,3 +835,23 @@ class ConvertedEndianField(_base):
835835
raise AssertionError("incompatible native field value accepted")
836836
_value = _swapped(0x01020304)
837837
assert bytes(ConvertedEndianField(_value)) == bytes(_value)
838+
839+
840+
# Array assignment cannot complete a pointer field's missing target type.
841+
class IncompletePointer(ctypes._Pointer):
842+
pass
843+
844+
845+
class IncompletePointerField(ctypes.Structure):
846+
_fields_ = [("value", IncompletePointer)]
847+
848+
849+
_field = IncompletePointerField()
850+
_before = bytes(_field)
851+
try:
852+
_field.value = (ctypes.c_int * 1)(42)
853+
except TypeError:
854+
pass
855+
else:
856+
raise AssertionError("array accepted for an incomplete pointer field")
857+
assert bytes(_field) == _before

0 commit comments

Comments
 (0)