Skip to content

Commit

Permalink
std::os: Handle FormatMessage failure
Browse files Browse the repository at this point in the history
`FormatMessageW()` is called by `std::os::last_os_error()` to convert
errno into string, but the function may fail on non-english locale.
I don't know why it fails, but anyway it's better to return errno
than to `fail!()` in the case.

Fixes #13075
Fixes #13073
  • Loading branch information
klutzy committed Mar 22, 2014
1 parent 30165e0 commit cffe9e0
Showing 1 changed file with 8 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/libstd/os.rs
Original file line number Diff line number Diff line change
Expand Up @@ -740,11 +740,16 @@ pub fn last_os_error() -> ~str {
buf.len() as DWORD,
ptr::null());
if res == 0 {
fail!("[{}] FormatMessage failure", errno());
// Sometimes FormatMessageW can fail e.g. system doesn't like langId,
let fm_err = errno();
return format!("OS Error {} (FormatMessageW() returned error {})", err, fm_err);
}

str::from_utf16(str::truncate_utf16_at_nul(buf))
.expect("FormatMessageW returned invalid UTF-16")
let msg = str::from_utf16(str::truncate_utf16_at_nul(buf));
match msg {
Some(msg) => format!("OS Error {}: {}", err, msg),
None => format!("OS Error {} (FormatMessageW() returned invalid UTF-16)", err),
}
}
}

Expand Down

5 comments on commit cffe9e0

@bors
Copy link
Contributor

@bors bors commented on cffe9e0 Mar 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

saw approval from alexcrichton
at klutzy@cffe9e0

@bors
Copy link
Contributor

@bors bors commented on cffe9e0 Mar 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging klutzy/rust/issue-13075 = cffe9e0 into auto

@bors
Copy link
Contributor

@bors bors commented on cffe9e0 Mar 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

klutzy/rust/issue-13075 = cffe9e0 merged ok, testing candidate = 5e8e1b5

@bors
Copy link
Contributor

@bors bors commented on cffe9e0 Mar 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on cffe9e0 Mar 22, 2014

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 5e8e1b5

Please sign in to comment.