Skip to content

Commit b0a801e

Browse files
seritoolsmbilker
authored andcommitted
Fall back to old env arg behavior if CompareStringOrdinal is not available
See rust-lang#85270 and rust-lang#87863
1 parent ec2e522 commit b0a801e

File tree

4 files changed

+32
-14
lines changed

4 files changed

+32
-14
lines changed

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

+12
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,18 @@ compat_fn_with_fallback! {
551551
SetLastError(ERROR_CALL_NOT_IMPLEMENTED as DWORD);
552552
FALSE
553553
}
554+
555+
// >= Vista / Server 2008
556+
// https://docs.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-comparestringordinal
557+
pub fn CompareStringOrdinal(
558+
lpString1: PCWSTR,
559+
cchCount1: i32,
560+
lpString2: PCWSTR,
561+
cchCount2: i32,
562+
bIgnoreCase: BOOL
563+
) -> COMPARESTRING_RESULT {
564+
rtabort!("unavailable")
565+
}
554566
}
555567

556568
compat_fn_optional! {

library/std/src/sys/windows/c/windows_sys.lst

-1
Original file line numberDiff line numberDiff line change
@@ -1941,7 +1941,6 @@ Windows.Win32.Foundation.WAIT_OBJECT_0
19411941
Windows.Win32.Foundation.WAIT_TIMEOUT
19421942
Windows.Win32.Foundation.WIN32_ERROR
19431943
Windows.Win32.Globalization.COMPARESTRING_RESULT
1944-
Windows.Win32.Globalization.CompareStringOrdinal
19451944
Windows.Win32.Globalization.CP_UTF8
19461945
Windows.Win32.Globalization.CSTR_EQUAL
19471946
Windows.Win32.Globalization.CSTR_GREATER_THAN

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

-10
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,6 @@ extern "system" {
1616
pub fn CloseHandle(hobject: HANDLE) -> BOOL;
1717
}
1818
#[link(name = "kernel32")]
19-
extern "system" {
20-
pub fn CompareStringOrdinal(
21-
lpstring1: PCWSTR,
22-
cchcount1: i32,
23-
lpstring2: PCWSTR,
24-
cchcount2: i32,
25-
bignorecase: BOOL,
26-
) -> COMPARESTRING_RESULT;
27-
}
28-
#[link(name = "kernel32")]
2919
extern "system" {
3020
pub fn CreateDirectoryW(
3121
lppathname: PCWSTR,

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

+20-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::available() {
78+
return self.os_string.cmp(&other.os_string);
79+
}
80+
7781
unsafe {
7882
let result = c::CompareStringOrdinal(
7983
self.utf16.as_ptr(),
@@ -99,6 +103,10 @@ impl PartialOrd for EnvKey {
99103
}
100104
impl PartialEq for EnvKey {
101105
fn eq(&self, other: &Self) -> bool {
106+
if !c::CompareStringOrdinal::available() {
107+
return self.os_string == other.os_string;
108+
}
109+
102110
if self.utf16.len() != other.utf16.len() {
103111
false
104112
} else {
@@ -124,7 +132,12 @@ impl PartialEq<str> for EnvKey {
124132
// Environment variable keys should preserve their original case even though
125133
// they are compared using a caseless string mapping.
126134
impl From<OsString> for EnvKey {
127-
fn from(k: OsString) -> Self {
135+
fn from(mut k: OsString) -> Self {
136+
if !c::CompareStringOrdinal::available() {
137+
k.make_ascii_uppercase();
138+
return EnvKey { utf16: Vec::new(), os_string: k };
139+
}
140+
128141
EnvKey { utf16: k.encode_wide().collect(), os_string: k }
129142
}
130143
}
@@ -818,8 +831,12 @@ fn make_envp(maybe_env: Option<BTreeMap<EnvKey, OsString>>) -> io::Result<(*mut
818831
}
819832

820833
for (k, v) in env {
821-
ensure_no_nuls(k.os_string)?;
822-
blk.extend(k.utf16);
834+
if !c::CompareStringOrdinal::available() {
835+
blk.extend(ensure_no_nuls(k.os_string)?.encode_wide());
836+
} else {
837+
ensure_no_nuls(k.os_string)?;
838+
blk.extend(k.utf16);
839+
}
823840
blk.push('=' as u16);
824841
blk.extend(ensure_no_nuls(v)?.encode_wide());
825842
blk.push(0);

0 commit comments

Comments
 (0)