diff --git a/crates/victory-gpu/src/dxgi.rs b/crates/victory-gpu/src/dxgi.rs index af10227..5dfb218 100644 --- a/crates/victory-gpu/src/dxgi.rs +++ b/crates/victory-gpu/src/dxgi.rs @@ -90,6 +90,14 @@ fn query(kind: usize, iid: *const Iid, known: &[Iid], found: This, out: *mut Thi object::answer(kind, iid, object::is(iid, known).then_some(found), out) } +fn enumeration_not_found(out: *mut This) -> HRESULT { + if !out.is_null() { + // SAFETY: the caller supplies a writable output slot when non-null. + unsafe { out.write(std::ptr::null()) }; + } + NOT_FOUND +} + /// The DXGI device also answers for the D3D12 device. extern "system" fn device_query(this: This, iid: *const Iid, out: *mut This) -> HRESULT { if crate::device::is_device(iid) { @@ -112,7 +120,7 @@ extern "system" fn adapter_parent(_: This, iid: *const Iid, out: *mut This) -> H } extern "system" fn adapter_outputs(_: This, index: u32, out: *mut This) -> HRESULT { if index != 0 { - return NOT_FOUND; + return enumeration_not_found(out); } object::answer(ADAPTER, &OUTPUT_IIDS[0], Some(&OUTPUT_OBJECT), out) } @@ -165,7 +173,7 @@ extern "system" fn factory_query(this: This, iid: *const Iid, out: *mut This) -> } extern "system" fn factory_adapters(_: This, index: u32, out: *mut This) -> HRESULT { if index != 0 { - return NOT_FOUND; + return enumeration_not_found(out); } object::answer(FACTORY, &ADAPTER_IIDS[0], Some(&ADAPTER_OBJECT), out) } @@ -338,4 +346,18 @@ mod tests { assert!(!output.is_null()); assert_eq!(outputs(adapter, 1, &mut output), NOT_FOUND); } + + #[test] + fn out_of_range_enumeration_clears_output() { + for (object, offset, method) in [ + (&FACTORY_OBJECT, 0x38, "EnumAdapters"), + (&FACTORY_OBJECT, 0x60, "EnumAdapters1"), + (&ADAPTER_OBJECT, 0x38, "EnumOutputs"), + ] { + let enumerate = slot::(object, offset); + let mut out = object as This; + assert_eq!(enumerate(object, 1, &mut out), NOT_FOUND, "{method}"); + assert!(out.is_null(), "{method} left a stale output pointer"); + } + } }