Skip to content

Commit 02bd38f

Browse files
committed
Fall back to old env arg behavior if CompareStringOrdinal is not available
See rust-lang#85270 and rust-lang#87863
1 parent 0bb64a8 commit 02bd38f

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

library/std/src/sys/windows/c.rs

+10
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,16 @@ compat_fn_lazy! {
464464
pbcancel: *mut BOOL,
465465
dwcopyflags: u32,
466466
) -> BOOL;
467+
468+
// >= Vista / Server 2008
469+
// https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-comparestringordinal
470+
pub fn CompareStringOrdinal(
471+
lpstring1: PCWSTR,
472+
cchcount1: i32,
473+
lpstring2: PCWSTR,
474+
cchcount2: i32,
475+
bignorecase: BOOL,
476+
) -> COMPARESTRING_RESULT;
467477
}
468478

469479
compat_fn_optional! {

library/std/src/sys/windows/process.rs

+16-3
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ impl EnvKey {
7474
// [4] https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-comparestringordinal
7575
impl Ord for EnvKey {
7676
fn cmp(&self, other: &Self) -> cmp::Ordering {
77+
if !c::CompareStringOrdinal::option().is_some() {
78+
return self.os_string.cmp(&other.os_string);
79+
}
80+
7781
unsafe {
7882
let result = c::CompareStringOrdinal(
7983
self.utf16.as_ptr(),
@@ -124,7 +128,12 @@ impl PartialEq<str> for EnvKey {
124128
// Environment variable keys should preserve their original case even though
125129
// they are compared using a caseless string mapping.
126130
impl From<OsString> for EnvKey {
127-
fn from(k: OsString) -> Self {
131+
fn from(mut k: OsString) -> Self {
132+
if !c::CompareStringOrdinal::option().is_some() {
133+
k.make_ascii_uppercase();
134+
return EnvKey { utf16: Vec::new(), os_string: k };
135+
}
136+
128137
EnvKey { utf16: k.encode_wide().collect(), os_string: k }
129138
}
130139
}
@@ -857,8 +866,12 @@ fn make_envp(maybe_env: Option<BTreeMap<EnvKey, OsString>>) -> io::Result<(*mut
857866
}
858867

859868
for (k, v) in env {
860-
ensure_no_nuls(k.os_string)?;
861-
blk.extend(k.utf16);
869+
if !c::CompareStringOrdinal::option().is_some() {
870+
blk.extend(ensure_no_nuls(k.os_string)?.encode_wide());
871+
} else {
872+
ensure_no_nuls(k.os_string)?;
873+
blk.extend(k.utf16);
874+
}
862875
blk.push('=' as u16);
863876
blk.extend(ensure_no_nuls(v)?.encode_wide());
864877
blk.push(0);

0 commit comments

Comments
 (0)