Skip to content

Commit a42831f

Browse files
author
Kevin Gosse
committed
Keep trying to please the linter
1 parent 31906d3 commit a42831f

File tree

1 file changed

+40
-52
lines changed
  • crashtracker-ffi/src/collector_windows

1 file changed

+40
-52
lines changed

crashtracker-ffi/src/collector_windows/api.rs

Lines changed: 40 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use crate::Metadata;
55
use anyhow::{anyhow, Context, Result};
6+
use core::mem::size_of;
67
use datadog_crashtracker::{CrashInfoBuilder, StackFrame, StackTrace, ThreadData};
78
use ddcommon::Endpoint;
89
use ddcommon_ffi::slice::AsBytes;
@@ -49,7 +50,6 @@ use windows::Win32::System::Threading::{GetProcessId, GetThreadId, OpenThread, T
4950

5051
#[no_mangle]
5152
#[must_use]
52-
#[named]
5353
/// Initialize the crash-tracking infrastructure.
5454
///
5555
/// # Preconditions
@@ -66,9 +66,9 @@ pub unsafe extern "C" fn ddog_crasht_init_windows(
6666
metadata: Metadata,
6767
) -> bool {
6868
let result: Result<(), _> = (|| {
69-
let endpoint = endpoint.map(|endpoint| endpoint.clone());
69+
let endpoint = endpoint.cloned();
7070
let error_context = ErrorContext {
71-
endpoint: endpoint,
71+
endpoint,
7272
metadata: metadata.try_into()?,
7373
};
7474
let error_context_json = serde_json::to_string(&error_context)?;
@@ -88,22 +88,18 @@ pub unsafe extern "C" fn ddog_crasht_init_windows(
8888

8989
if result.is_err() {
9090
output_debug_string(
91-
format!(
92-
"ddog_crasht_init_windows failed: {}",
93-
result.err().unwrap().to_string()
94-
)
95-
.as_str(),
91+
format!("ddog_crasht_init_windows failed: {}", result.err().unwrap()).as_str(),
9692
);
9793
return false;
9894
}
9995

10096
true
10197
}
10298

103-
unsafe fn create_registry_key(path: &String) -> Result<()> {
104-
// First, check if there is already a key named "path" in SOFTWARE\Microsoft\Windows\Windows Error Reporting\RuntimeExceptionHelperModules,
105-
// in either HKEY_LOCAL_MACHINE or HKEY_CURRENT_USER.
106-
// If not, create it in HKEY_CURRENT_USER.
99+
unsafe fn create_registry_key(path: &str) -> Result<()> {
100+
// First, check if there is already a key named "path" in SOFTWARE\Microsoft\Windows\Windows
101+
// Error Reporting\RuntimeExceptionHelperModules, in either HKEY_LOCAL_MACHINE or
102+
// HKEY_CURRENT_USER. If not, create it in HKEY_CURRENT_USER.
107103

108104
// Convert value name to null-terminated wide string
109105
let mut name_wide: Vec<u16> = path.encode_utf16().collect();
@@ -210,7 +206,6 @@ pub struct WerContext {
210206
}
211207

212208
#[no_mangle]
213-
#[named]
214209
#[cfg(windows)]
215210
pub unsafe extern "C" fn ddog_crasht_event_signature_callback(
216211
_context: *const c_void,
@@ -226,7 +221,6 @@ pub unsafe extern "C" fn ddog_crasht_event_signature_callback(
226221
}
227222

228223
#[no_mangle]
229-
#[named]
230224
#[cfg(windows)]
231225
pub unsafe extern "C" fn ddog_crasht_debugger_launch_callback(
232226
_context: *const c_void,
@@ -241,7 +235,6 @@ pub unsafe extern "C" fn ddog_crasht_debugger_launch_callback(
241235
}
242236

243237
#[no_mangle]
244-
#[named]
245238
#[cfg(windows)]
246239
pub unsafe extern "C" fn ddog_crasht_exception_event_callback(
247240
context: *const c_void,
@@ -274,18 +267,18 @@ pub unsafe extern "C" fn ddog_crasht_exception_event_callback(
274267
let stack: StackTrace;
275268
let stack_result = walk_thread_stack(exception_information.hProcess, thread, &modules);
276269

277-
if stack_result.is_err() {
270+
let stack: StackTrace = if stack_result.is_err() {
278271
output_debug_string(
279272
format!(
280273
"Failed to walk thread stack: {}",
281-
stack_result.err().unwrap().to_string()
274+
stack_result.err().unwrap()
282275
)
283276
.as_str(),
284277
);
285-
stack = StackTrace::new_incomplete();
278+
StackTrace::new_incomplete()
286279
} else {
287-
stack = stack_result.unwrap();
288-
}
280+
stack_result.unwrap()
281+
};
289282

290283
if thread == crash_tid {
291284
builder
@@ -296,7 +289,7 @@ pub unsafe extern "C" fn ddog_crasht_exception_event_callback(
296289
let thread_data = ThreadData {
297290
crashed: thread == crash_tid,
298291
name: format!("{}", thread),
299-
stack: stack,
292+
stack,
300293
state: None,
301294
};
302295

@@ -329,7 +322,7 @@ pub unsafe extern "C" fn ddog_crasht_exception_event_callback(
329322
output_debug_string(
330323
format!(
331324
"ddog_crasht_exception_event_callback failed: {}",
332-
result.err().unwrap().to_string()
325+
result.err().unwrap()
333326
)
334327
.as_str(),
335328
);
@@ -386,7 +379,7 @@ pub unsafe fn read_wer_context(process_handle: HANDLE, base_address: usize) -> R
386379
let wer_context = WerContext {
387380
prefix: WER_CONTEXT_PREFIX,
388381
ptr: static_slice.as_ptr(),
389-
len: len,
382+
len,
390383
suffix: WER_CONTEXT_SUFFIX,
391384
};
392385

@@ -403,7 +396,7 @@ struct AlignedContext {
403396
unsafe fn walk_thread_stack(
404397
process_handle: HANDLE,
405398
thread_id: u32,
406-
modules: &Vec<ModuleInfo>,
399+
modules: &[ModuleInfo],
407400
) -> Result<StackTrace> {
408401
let mut stacktrace = StackTrace::new_incomplete();
409402
let thread_handle = OpenThread(THREAD_ALL_ACCESS, false, thread_id)?;
@@ -426,11 +419,11 @@ unsafe fn walk_thread_stack(
426419
#[cfg(target_arch = "x86_64")]
427420
{
428421
machine_type = IMAGE_FILE_MACHINE_AMD64.0 as u32;
429-
native_frame.AddrPC.Offset = context.ctx.Rip as u64;
422+
native_frame.AddrPC.Offset = context.ctx.Rip;
430423
native_frame.AddrPC.Mode = AddrModeFlat;
431-
native_frame.AddrStack.Offset = context.ctx.Rsp as u64;
424+
native_frame.AddrStack.Offset = context.ctx.Rsp;
432425
native_frame.AddrStack.Mode = AddrModeFlat;
433-
native_frame.AddrFrame.Offset = context.ctx.Rbp as u64;
426+
native_frame.AddrFrame.Offset = context.ctx.Rbp;
434427
native_frame.AddrFrame.Mode = AddrModeFlat;
435428
}
436429

@@ -508,7 +501,7 @@ struct ModuleInfo {
508501

509502
struct PdbInfo {
510503
age: u32,
511-
signature: GUID,
504+
signature: Guid,
512505
}
513506

514507
unsafe fn list_modules(process_handle: HANDLE) -> anyhow::Result<Vec<ModuleInfo>> {
@@ -517,25 +510,21 @@ unsafe fn list_modules(process_handle: HANDLE) -> anyhow::Result<Vec<ModuleInfo>
517510

518511
// Get the number of bytes required to store the array of module handles
519512
let mut cb_needed = 0;
520-
if !EnumProcessModules(process_handle, std::ptr::null_mut(), 0, &mut cb_needed).is_ok() {
521-
return Err(anyhow!("Failed to get module list size"));
522-
}
513+
EnumProcessModules(process_handle, std::ptr::null_mut(), 0, &mut cb_needed)
514+
.context("Failed to get module list size")?;
523515

524516
// Allocate enough space for the module handles
525517
let modules_count = cb_needed as usize / size_of::<HMODULE>();
526518
let mut hmodules: Vec<HMODULE> = Vec::with_capacity(modules_count);
527519
let mut cb_actual = 0;
528520

529-
if !EnumProcessModules(
521+
EnumProcessModules(
530522
process_handle,
531523
hmodules.as_mut_ptr(),
532524
cb_needed,
533525
&mut cb_actual,
534526
)
535-
.is_ok()
536-
{
537-
return Err(anyhow!("Failed to enumerate process modules"));
538-
}
527+
.context("Failed to enumerate process modules")?;
539528

540529
hmodules.set_len(cb_actual as usize / size_of::<HMODULE>());
541530

@@ -614,14 +603,14 @@ struct IMAGE_NT_HEADERS_GENERIC {
614603

615604
#[repr(C)]
616605
#[derive(Debug)]
617-
struct GUID {
606+
struct Guid {
618607
pub data1: u32,
619608
pub data2: u16,
620609
pub data3: u16,
621610
pub data4: [u8; 8],
622611
}
623612

624-
impl fmt::LowerHex for GUID {
613+
impl fmt::LowerHex for Guid {
625614
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
626615
write!(
627616
f,
@@ -644,7 +633,7 @@ impl fmt::LowerHex for GUID {
644633
#[repr(C)]
645634
struct CV_INFO_PDB70 {
646635
pub signature: u32,
647-
pub guid: GUID,
636+
pub guid: Guid,
648637
pub age: u32,
649638
}
650639

@@ -669,17 +658,13 @@ unsafe fn get_pdb_info(process_handle: HANDLE, base_address: u64) -> Result<PdbI
669658
return Err(anyhow!("Invalid machine type"));
670659
}
671660

672-
let debug_data_dir: IMAGE_DATA_DIRECTORY;
673-
674-
if is_pe32 {
661+
let debug_data_dir: IMAGE_DATA_DIRECTORY = if is_pe32 {
675662
let nt_headers32: IMAGE_NT_HEADERS32 = read_memory(process_handle, nt_headers_address)?;
676-
debug_data_dir =
677-
nt_headers32.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG.0 as usize];
663+
nt_headers32.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG.0 as usize]
678664
} else {
679665
let nt_headers64: IMAGE_NT_HEADERS64 = read_memory(process_handle, nt_headers_address)?;
680-
debug_data_dir =
681-
nt_headers64.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG.0 as usize];
682-
}
666+
nt_headers64.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_DEBUG.0 as usize]
667+
};
683668

684669
let debug_dir_buffer = read_memory_raw(
685670
process_handle,
@@ -722,9 +707,10 @@ unsafe fn get_module_path(
722707
let len = GetModuleFileNameExW(
723708
Some(process_handle),
724709
Some(module_handle),
725-
&mut *module_name_buffer,
710+
&mut module_name_buffer,
726711
);
727-
if len <= 0 {
712+
713+
if len == 0 {
728714
return Err(anyhow!("GetModuleFileNameExW failed: {}", len));
729715
}
730716

@@ -740,16 +726,18 @@ unsafe fn list_threads(pid: u32) -> anyhow::Result<Vec<u32>> {
740726

741727
let snapshot = CreateToolhelp32Snapshot(TH32CS_SNAPTHREAD, pid)?;
742728

743-
let mut thread_entry = THREADENTRY32::default();
744-
thread_entry.dwSize = size_of::<THREADENTRY32>() as u32;
729+
let mut thread_entry = THREADENTRY32 {
730+
dwSize: size_of::<THREADENTRY32>() as u32,
731+
..Default::default()
732+
};
745733

746734
if Thread32First(snapshot, &mut thread_entry).is_ok() {
747735
loop {
748736
if thread_entry.th32OwnerProcessID == pid {
749737
thread_ids.push(thread_entry.th32ThreadID);
750738
}
751739

752-
if !Thread32Next(snapshot, &mut thread_entry).is_ok() {
740+
if Thread32Next(snapshot, &mut thread_entry).is_err() {
753741
break;
754742
}
755743
}

0 commit comments

Comments
 (0)