Skip to content

Commit 39bcd6f

Browse files
committed
Auto merge of #41684 - jethrogb:feature/ntstatus, r=alexcrichton
Windows io::Error: also format NTSTATUS error codes `NTSTATUS` errors may be encoded as `HRESULT`, see [[MS-ERREF]](https://msdn.microsoft.com/en-us/library/cc231198.aspx). These error codes can still be formatted using `FormatMessageW` but require some different parameters to be passed in. I wasn't sure if this needed a test and if so, how to test it. Presumably we wouldn't want to make our tests dependent on localization-dependent strings returned from `FormatMessageW`. Users that get an `err: NTSTATUS` will need to do `io::Error::from_raw_os_error(err|0x1000_0000)` (the equivalent of [`HRESULT_FROM_NT`](https://msdn.microsoft.com/en-us/library/ms693780(VS.85).aspx))
2 parents e40beb3 + 71de9db commit 39bcd6f

File tree

2 files changed

+38
-3
lines changed

2 files changed

+38
-3
lines changed

src/libstd/sys/windows/c.rs

+3
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,10 @@ pub const ERROR_TIMEOUT: DWORD = 0x5B4;
199199

200200
pub const INVALID_HANDLE_VALUE: HANDLE = !0 as HANDLE;
201201

202+
pub const FACILITY_NT_BIT: DWORD = 0x1000_0000;
203+
202204
pub const FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
205+
pub const FORMAT_MESSAGE_FROM_HMODULE: DWORD = 0x00000800;
203206
pub const FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
204207

205208
pub const TLS_OUT_OF_INDEXES: DWORD = 0xFFFFFFFF;

src/libstd/sys/windows/os.rs

+35-3
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,35 @@ pub fn errno() -> i32 {
3232
}
3333

3434
/// Gets a detailed string description for the given error number.
35-
pub fn error_string(errnum: i32) -> String {
35+
pub fn error_string(mut errnum: i32) -> String {
3636
// This value is calculated from the macro
3737
// MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
3838
let langId = 0x0800 as c::DWORD;
3939

4040
let mut buf = [0 as c::WCHAR; 2048];
4141

4242
unsafe {
43-
let res = c::FormatMessageW(c::FORMAT_MESSAGE_FROM_SYSTEM |
43+
let mut module = ptr::null_mut();
44+
let mut flags = 0;
45+
46+
// NTSTATUS errors may be encoded as HRESULT, which may returned from
47+
// GetLastError. For more information about Windows error codes, see
48+
// `[MS-ERREF]`: https://msdn.microsoft.com/en-us/library/cc231198.aspx
49+
if (errnum & c::FACILITY_NT_BIT as i32) != 0 {
50+
// format according to https://support.microsoft.com/en-us/help/259693
51+
const NTDLL_DLL: &'static [u16] = &['N' as _, 'T' as _, 'D' as _, 'L' as _, 'L' as _,
52+
'.' as _, 'D' as _, 'L' as _, 'L' as _, 0];
53+
module = c::GetModuleHandleW(NTDLL_DLL.as_ptr());
54+
55+
if module != ptr::null_mut() {
56+
errnum ^= c::FACILITY_NT_BIT as i32;
57+
flags = c::FORMAT_MESSAGE_FROM_HMODULE;
58+
}
59+
}
60+
61+
let res = c::FormatMessageW(flags | c::FORMAT_MESSAGE_FROM_SYSTEM |
4462
c::FORMAT_MESSAGE_IGNORE_INSERTS,
45-
ptr::null_mut(),
63+
module,
4664
errnum as c::DWORD,
4765
langId,
4866
buf.as_mut_ptr(),
@@ -299,3 +317,17 @@ pub fn home_dir() -> Option<PathBuf> {
299317
pub fn exit(code: i32) -> ! {
300318
unsafe { c::ExitProcess(code as c::UINT) }
301319
}
320+
321+
#[cfg(test)]
322+
mod tests {
323+
use io::Error;
324+
use sys::c;
325+
326+
// tests `error_string` above
327+
#[test]
328+
fn ntstatus_error() {
329+
const STATUS_UNSUCCESSFUL: u32 = 0xc000_0001;
330+
assert!(!Error::from_raw_os_error((STATUS_UNSUCCESSFUL | c::FACILITY_NT_BIT) as _)
331+
.to_string().contains("FormatMessageW() returned error"));
332+
}
333+
}

0 commit comments

Comments
 (0)