Skip to content

Commit f3c8882

Browse files
committed
std::os: Use unicode for last_os_error() on Win32
FormatMessageA may return non-ascii message, which is encoded as system code page, not utf8. This may cause `assert!(is_utf8(v))` failure on some non-English machines. This patch replaces it with FormatMessageW, which returns utf-16 message. Fixes `make check-stage2-std` failure on my machine. :)
1 parent 1e745f1 commit f3c8882

File tree

1 file changed

+9
-10
lines changed

1 file changed

+9
-10
lines changed

src/libstd/os.rs

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1135,18 +1135,19 @@ pub fn last_os_error() -> ~str {
11351135
#[fixed_stack_segment]; #[inline(never)];
11361136

11371137
use libc::types::os::arch::extra::DWORD;
1138-
use libc::types::os::arch::extra::LPSTR;
1138+
use libc::types::os::arch::extra::LPWSTR;
11391139
use libc::types::os::arch::extra::LPVOID;
1140+
use libc::types::os::arch::extra::WCHAR;
11401141

11411142
#[cfg(target_arch = "x86")]
11421143
#[link_name = "kernel32"]
11431144
#[abi = "stdcall"]
11441145
extern "stdcall" {
1145-
fn FormatMessageA(flags: DWORD,
1146+
fn FormatMessageW(flags: DWORD,
11461147
lpSrc: LPVOID,
11471148
msgId: DWORD,
11481149
langId: DWORD,
1149-
buf: LPSTR,
1150+
buf: LPWSTR,
11501151
nsize: DWORD,
11511152
args: *c_void)
11521153
-> DWORD;
@@ -1155,11 +1156,11 @@ pub fn last_os_error() -> ~str {
11551156
#[cfg(target_arch = "x86_64")]
11561157
#[link_name = "kernel32"]
11571158
extern {
1158-
fn FormatMessageA(flags: DWORD,
1159+
fn FormatMessageW(flags: DWORD,
11591160
lpSrc: LPVOID,
11601161
msgId: DWORD,
11611162
langId: DWORD,
1162-
buf: LPSTR,
1163+
buf: LPWSTR,
11631164
nsize: DWORD,
11641165
args: *c_void)
11651166
-> DWORD;
@@ -1173,11 +1174,11 @@ pub fn last_os_error() -> ~str {
11731174
let langId = 0x0800 as DWORD;
11741175
let err = errno() as DWORD;
11751176

1176-
let mut buf = [0 as c_char, ..TMPBUF_SZ];
1177+
let mut buf = [0 as WCHAR, ..TMPBUF_SZ];
11771178

11781179
unsafe {
11791180
do buf.as_mut_buf |buf, len| {
1180-
let res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
1181+
let res = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM |
11811182
FORMAT_MESSAGE_IGNORE_INSERTS,
11821183
ptr::mut_null(),
11831184
err,
@@ -1190,9 +1191,7 @@ pub fn last_os_error() -> ~str {
11901191
}
11911192
}
11921193

1193-
do buf.as_imm_buf |buf, _len| {
1194-
str::raw::from_c_str(buf)
1195-
}
1194+
str::from_utf16(buf)
11961195
}
11971196
}
11981197

0 commit comments

Comments
 (0)