Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 1 addition & 21 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 1 addition & 10 deletions crates/vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,16 +112,6 @@ num_cpus = "1.17.0"
[target.'cfg(windows)'.dependencies]
junction = { workspace = true }

[target.'cfg(windows)'.dependencies.windows]
version = "0.52.0"
features = [
"Win32_Foundation",
"Win32_System_LibraryLoader",
"Win32_System_Threading",
"Win32_System_Time",
"Win32_UI_Shell",
]

[target.'cfg(windows)'.dependencies.windows-sys]
workspace = true
features = [
Expand All @@ -143,6 +133,7 @@ features = [
"Win32_System_SystemInformation",
"Win32_System_SystemServices",
"Win32_System_Threading",
"Win32_System_Time",
"Win32_System_WindowsProgramming",
"Win32_UI_Shell",
"Win32_UI_WindowsAndMessaging",
Expand Down
37 changes: 20 additions & 17 deletions crates/vm/src/stdlib/nt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -556,23 +556,26 @@ pub(crate) mod module {
String::from_utf16(wstr).map_err(|e| vm.new_unicode_decode_error(e.to_string()))
}

let wbuf = windows::core::PCWSTR::from_raw(backslashed.as_ptr());
let (root, path) = match unsafe { windows::Win32::UI::Shell::PathCchSkipRoot(wbuf) } {
Ok(end) => {
assert!(!end.is_null());
let len: usize = unsafe { end.as_ptr().offset_from(wbuf.as_ptr()) }
.try_into()
.expect("len must be non-negative");
assert!(
len < backslashed.len(), // backslashed is null-terminated
"path: {:?} {} < {}",
std::path::PathBuf::from(std::ffi::OsString::from_wide(&backslashed)),
len,
backslashed.len()
);
(from_utf16(&orig[..len], vm)?, from_utf16(&orig[len..], vm)?)
}
Err(_) => ("".to_owned(), from_utf16(&orig, vm)?),
let mut end: *const u16 = std::ptr::null();
let hr = unsafe {
windows_sys::Win32::UI::Shell::PathCchSkipRoot(backslashed.as_ptr(), &mut end)
};
let (root, path) = if hr == 0 {
// S_OK
assert!(!end.is_null());
let len: usize = unsafe { end.offset_from(backslashed.as_ptr()) }
.try_into()
.expect("len must be non-negative");
assert!(
len < backslashed.len(), // backslashed is null-terminated
"path: {:?} {} < {}",
std::path::PathBuf::from(std::ffi::OsString::from_wide(&backslashed)),
len,
backslashed.len()
);
(from_utf16(&orig[..len], vm)?, from_utf16(&orig[len..], vm)?)
} else {
("".to_owned(), from_utf16(&orig, vm)?)
};
Ok((root, path))
}
Expand Down
9 changes: 4 additions & 5 deletions crates/vm/src/stdlib/time.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ mod decl {
use std::time::Duration;
#[cfg(target_env = "msvc")]
#[cfg(not(target_arch = "wasm32"))]
use windows::Win32::System::Time;
use windows_sys::Win32::System::Time::{GetTimeZoneInformation, TIME_ZONE_INFORMATION};

#[allow(dead_code)]
pub(super) const SEC_TO_MS: i64 = 1000;
Expand Down Expand Up @@ -186,10 +186,9 @@ mod decl {

#[cfg(target_env = "msvc")]
#[cfg(not(target_arch = "wasm32"))]
fn get_tz_info() -> Time::TIME_ZONE_INFORMATION {
let mut info = Time::TIME_ZONE_INFORMATION::default();
let info_ptr = &mut info as *mut Time::TIME_ZONE_INFORMATION;
let _ = unsafe { Time::GetTimeZoneInformation(info_ptr) };
fn get_tz_info() -> TIME_ZONE_INFORMATION {
let mut info: TIME_ZONE_INFORMATION = unsafe { std::mem::zeroed() };
unsafe { GetTimeZoneInformation(&mut info) };
info
}
Comment on lines +189 to 193
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟡 Minor

Missing error check for GetTimeZoneInformation.

The return value of GetTimeZoneInformation is discarded. This function returns TIME_ZONE_ID_INVALID (0xFFFFFFFF) on failure, which should be checked to avoid returning zeroed/invalid timezone data.

Consider adding error handling:

 fn get_tz_info() -> TIME_ZONE_INFORMATION {
     let mut info: TIME_ZONE_INFORMATION = unsafe { std::mem::zeroed() };
-    unsafe { GetTimeZoneInformation(&mut info) };
+    let result = unsafe { GetTimeZoneInformation(&mut info) };
+    debug_assert!(result != 0xFFFFFFFF, "GetTimeZoneInformation failed");
     info
 }

Alternatively, if the callers can handle errors, consider returning a Result type.

📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
fn get_tz_info() -> TIME_ZONE_INFORMATION {
let mut info: TIME_ZONE_INFORMATION = unsafe { std::mem::zeroed() };
unsafe { GetTimeZoneInformation(&mut info) };
info
}
fn get_tz_info() -> TIME_ZONE_INFORMATION {
let mut info: TIME_ZONE_INFORMATION = unsafe { std::mem::zeroed() };
let result = unsafe { GetTimeZoneInformation(&mut info) };
debug_assert!(result != 0xFFFFFFFF, "GetTimeZoneInformation failed");
info
}


Expand Down
Loading
Loading