Skip to content

Commit

Permalink
Fall back to old env arg behavior if CompareStringOrdinal is not av…
Browse files Browse the repository at this point in the history
…ailable

See rust-lang#85270 and rust-lang#87863
  • Loading branch information
seritools authored and mbilker committed Sep 16, 2023
1 parent ec2e522 commit b0a801e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 14 deletions.
12 changes: 12 additions & 0 deletions library/std/src/sys/windows/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,18 @@ compat_fn_with_fallback! {
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD);
FALSE
}

// >= Vista / Server 2008
// https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-comparestringordinal
pub fn CompareStringOrdinal(
lpString1: PCWSTR,
cchCount1: i32,
lpString2: PCWSTR,
cchCount2: i32,
bIgnoreCase: BOOL
) -> COMPARESTRING_RESULT {
rtabort!("unavailable")
}
}

compat_fn_optional! {
Expand Down
1 change: 0 additions & 1 deletion library/std/src/sys/windows/c/windows_sys.lst
Original file line number Diff line number Diff line change
Expand Up @@ -1941,7 +1941,6 @@ Windows.Win32.Foundation.WAIT_OBJECT_0
Windows.Win32.Foundation.WAIT_TIMEOUT
Windows.Win32.Foundation.WIN32_ERROR
Windows.Win32.Globalization.COMPARESTRING_RESULT
Windows.Win32.Globalization.CompareStringOrdinal
Windows.Win32.Globalization.CP_UTF8
Windows.Win32.Globalization.CSTR_EQUAL
Windows.Win32.Globalization.CSTR_GREATER_THAN
Expand Down
10 changes: 0 additions & 10 deletions library/std/src/sys/windows/c/windows_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,6 @@ extern "system" {
pub fn CloseHandle(hobject: HANDLE) -> BOOL;
}
#[link(name = "kernel32")]
extern "system" {
pub fn CompareStringOrdinal(
lpstring1: PCWSTR,
cchcount1: i32,
lpstring2: PCWSTR,
cchcount2: i32,
bignorecase: BOOL,
) -> COMPARESTRING_RESULT;
}
#[link(name = "kernel32")]
extern "system" {
pub fn CreateDirectoryW(
lppathname: PCWSTR,
Expand Down
23 changes: 20 additions & 3 deletions library/std/src/sys/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ impl EnvKey {
// [4] https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-comparestringordinal
impl Ord for EnvKey {
fn cmp(&self, other: &Self) -> cmp::Ordering {
if !c::CompareStringOrdinal::available() {
return self.os_string.cmp(&other.os_string);
}

unsafe {
let result = c::CompareStringOrdinal(
self.utf16.as_ptr(),
Expand All @@ -99,6 +103,10 @@ impl PartialOrd for EnvKey {
}
impl PartialEq for EnvKey {
fn eq(&self, other: &Self) -> bool {
if !c::CompareStringOrdinal::available() {
return self.os_string == other.os_string;
}

if self.utf16.len() != other.utf16.len() {
false
} else {
Expand All @@ -124,7 +132,12 @@ impl PartialEq<str> for EnvKey {
// Environment variable keys should preserve their original case even though
// they are compared using a caseless string mapping.
impl From<OsString> for EnvKey {
fn from(k: OsString) -> Self {
fn from(mut k: OsString) -> Self {
if !c::CompareStringOrdinal::available() {
k.make_ascii_uppercase();
return EnvKey { utf16: Vec::new(), os_string: k };
}

EnvKey { utf16: k.encode_wide().collect(), os_string: k }
}
}
Expand Down Expand Up @@ -818,8 +831,12 @@ fn make_envp(maybe_env: Option<BTreeMap<EnvKey, OsString>>) -> io::Result<(*mut
}

for (k, v) in env {
ensure_no_nuls(k.os_string)?;
blk.extend(k.utf16);
if !c::CompareStringOrdinal::available() {
blk.extend(ensure_no_nuls(k.os_string)?.encode_wide());
} else {
ensure_no_nuls(k.os_string)?;
blk.extend(k.utf16);
}
blk.push('=' as u16);
blk.extend(ensure_no_nuls(v)?.encode_wide());
blk.push(0);
Expand Down

0 comments on commit b0a801e

Please sign in to comment.