From 968d9ecd80d474281e37c4f06747e041f46ef540 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Beno=C3=AEt=20CORTIER?= Date: Fri, 4 Sep 2026 14:20:06 -0400 Subject: [PATCH] fix(dgw): reject null event source handles Treat the null handle returned by RegisterEventSourceW as a registration failure. This prevents invalid event source handles from being retained and used for Windows Event Log writes. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> --- crates/sysevent-winevent/src/lib.rs | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/crates/sysevent-winevent/src/lib.rs b/crates/sysevent-winevent/src/lib.rs index 1ba2d7718..2b7d647f7 100644 --- a/crates/sysevent-winevent/src/lib.rs +++ b/crates/sysevent-winevent/src/lib.rs @@ -25,7 +25,7 @@ impl WinEvent { // SAFETY: Proper UTF-16, null-terminated string. let handle = unsafe { EventLog::RegisterEventSourceW(std::ptr::null(), source_name_utf16.as_ptr()) }; - if handle == windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE { + if event_source_registration_failed(handle) { return Err(SysEventError::Platform(format!( "failed to register event source '{source_name}'" ))); @@ -141,6 +141,10 @@ impl Drop for WinEvent { } } +fn event_source_registration_failed(handle: windows_sys::Win32::Foundation::HANDLE) -> bool { + handle.is_null() +} + fn severity_to_event_type(severity: Severity) -> u16 { match severity { Severity::Critical => EventLog::EVENTLOG_ERROR_TYPE, @@ -159,6 +163,14 @@ fn to_null_terminated_utf16(input: &str) -> Vec { mod tests { use super::*; + #[test] + fn event_source_registration_failure_sentinel() { + assert!(event_source_registration_failed(std::ptr::null_mut())); + assert!(!event_source_registration_failed( + windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE + )); + } + #[test] fn severity_to_event_type_mapping() { assert_eq!(