rodrigocfd / winsafe

Windows API and GUI in safe, idiomatic Rust.
https://crates.io/crates/winsafe
MIT License
518 stars 30 forks source link

Panic in wstring/ QueryFullProcessImageName #118

Closed caesay closed 8 months ago

caesay commented 8 months ago

On 0.0.19, QueryFullProcessImageName panic'd. The process it was trying to read was pacman.exe (from mingw64).

image

I tried building from source but there were compile errors, so left it there. Hope it helps. Ideally this would return an error instead of a panic.

Backtrace:

thread 'main' panicked at C:\Users\Caelan\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winsafe-0.0.19\src\kernel\utilities\w_string.rs:26:62:
called `Result::unwrap()` on an `Err` value: FromUtf16Error(())
stack backtrace:
   0: std::panicking::begin_panic_handler
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\std\src\panicking.rs:597
   1: core::panicking::panic_fmt
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\core\src\panicking.rs:72
   2: core::result::unwrap_failed
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962/library\core\src\result.rs:1652
   3: enum2$<core::result::Result<alloc::string::String,alloc::string::FromUtf16Error> >::unwrap<alloc::string::String,alloc::string::FromUtf16Error>
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962\library\core\src\result.rs:1077
   4: winsafe::kernel::utilities::w_string::impl$0::fmt
             at C:\Users\Caelan\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winsafe-0.0.19\src\kernel\utilities\w_string.rs:26
   5: alloc::string::impl$41::to_string<winsafe::kernel::utilities::w_string::WString>
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962\library\alloc\src\string.rs:2459
   6: winsafe::kernel::handles::hprocess::kernel_Hprocess::QueryFullProcessImageName::closure$0<winsafe::kernel::handles::hprocess::HPROCESS>
             at C:\Users\Caelan\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winsafe-0.0.19\src\kernel\handles\hprocess.rs:262
   7: enum2$<core::result::Result<tuple$<>,winsafe::kernel::co::error::ERROR> >::map<tuple$<>,winsafe::kernel::co::error::ERROR,alloc::string::String,winsafe::kernel::handles::hprocess::kernel_Hprocess::QueryFullProcessImageName::closure_env$0<winsafe::kernel::
             at /rustc/79e9716c980570bfd1f666e3b16ac583f0168962\library\core\src\result.rs:746
   8: winsafe::kernel::handles::hprocess::kernel_Hprocess::QueryFullProcessImageName<winsafe::kernel::handles::hprocess::HPROCESS>
             at C:\Users\Caelan\.cargo\registry\src\index.crates.io-6f17d22bba15001f\winsafe-0.0.19\src\kernel\handles\hprocess.rs:253
   9: velopack::shared::util_windows::get_processes_running_in_directory<ref$<std::path::Path> >
             at C:\Source\velopack\src\Rust\src\shared\util_windows.rs:107
rodrigocfd commented 8 months ago

The problem indeed seems to be those garbled characters in pacman.exe description. That's a problem with the executable itself. Then, in order to deal with these, the library should be able to deal with an UTF16 error, which doesn't seem hard to implement.

But anyway, please post your source code, so I can have an idea of what's really going on.

caesay commented 8 months ago

Just open a handle to that process and try to get the image name:

let process = w::HPROCESS::OpenProcess(co::PROCESS::QUERY_LIMITED_INFORMATION, false, pid)?;
let full_path = process.QueryFullProcessImageName(co::PROCESS_NAME::WIN32)?;

Since that function already returns a Result, it should ideally propagate the encoding errors the same way rather than panicking

rodrigocfd commented 8 months ago

I implemented a specific error treatment for invalid UTF-16 strings, which should not panic anymore.

Let me know if this works.