Skip to content

Commit c73d5ce

Browse files
committed
Use str::from_utf8_lossy() in os::args(), add os::args_as_bytes()
os::args() was using str::raw::from_c_str(), which would assert if the C-string wasn't valid UTF-8. Switch to using from_utf8_lossy() instead, and add a separate function os::args_as_bytes() that returns the ~[u8] byte-vectors instead.
1 parent 8cc8eb7 commit c73d5ce

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

src/libstd/os.rs

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,8 @@ use ptr::RawPtr;
5353

5454
#[cfg(unix)]
5555
use c_str::ToCStr;
56+
#[cfg(windows)]
57+
use str::OwnedStr;
5658

5759
/// Delegates to the libc close() function, returning the same return value.
5860
pub fn close(fd: int) -> int {
@@ -722,10 +724,12 @@ pub fn get_exit_status() -> int {
722724
}
723725

724726
#[cfg(target_os = "macos")]
725-
unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> ~[~str] {
727+
unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> ~[~[u8]] {
728+
use c_str::CString;
729+
726730
let mut args = ~[];
727731
for i in range(0u, argc as uint) {
728-
args.push(str::raw::from_c_str(*argv.offset(i as int)));
732+
args.push(CString::new(*argv.offset(i as int), false).as_bytes_no_nul().to_owned())
729733
}
730734
args
731735
}
@@ -736,7 +740,7 @@ unsafe fn load_argc_and_argv(argc: int, argv: **c_char) -> ~[~str] {
736740
* Returns a list of the command line arguments.
737741
*/
738742
#[cfg(target_os = "macos")]
739-
fn real_args() -> ~[~str] {
743+
fn real_args_as_bytes() -> ~[~[u8]] {
740744
unsafe {
741745
let (argc, argv) = (*_NSGetArgc() as int,
742746
*_NSGetArgv() as **c_char);
@@ -747,7 +751,7 @@ fn real_args() -> ~[~str] {
747751
#[cfg(target_os = "linux")]
748752
#[cfg(target_os = "android")]
749753
#[cfg(target_os = "freebsd")]
750-
fn real_args() -> ~[~str] {
754+
fn real_args_as_bytes() -> ~[~[u8]] {
751755
use rt;
752756

753757
match rt::args::clone() {
@@ -756,6 +760,11 @@ fn real_args() -> ~[~str] {
756760
}
757761
}
758762

763+
#[cfg(not(windows))]
764+
fn real_args() -> ~[~str] {
765+
real_args_as_bytes().move_iter().map(|v| str::from_utf8_lossy(v).into_owned()).collect()
766+
}
767+
759768
#[cfg(windows)]
760769
fn real_args() -> ~[~str] {
761770
use vec;
@@ -786,6 +795,11 @@ fn real_args() -> ~[~str] {
786795
return args;
787796
}
788797

798+
#[cfg(windows)]
799+
fn real_args_as_bytes() -> ~[~[u8]] {
800+
real_args().move_iter().map(|s| s.into_bytes()).collect()
801+
}
802+
789803
type LPCWSTR = *u16;
790804

791805
#[cfg(windows)]
@@ -803,10 +817,19 @@ extern "system" {
803817

804818
/// Returns the arguments which this program was started with (normally passed
805819
/// via the command line).
820+
///
821+
/// The arguments are interpreted as utf-8, with invalid bytes replaced with \uFFFD.
822+
/// See `str::from_utf8_lossy` for details.
806823
pub fn args() -> ~[~str] {
807824
real_args()
808825
}
809826

827+
/// Returns the arguments which this program was started with (normally passed
828+
/// via the command line) as byte vectors.
829+
pub fn args_as_bytes() -> ~[~[u8]] {
830+
real_args_as_bytes()
831+
}
832+
810833
#[cfg(target_os = "macos")]
811834
extern {
812835
// These functions are in crt_externs.h.

src/libstd/rt/args.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
3636
#[cfg(test)] pub unsafe fn cleanup() { realargs::cleanup() }
3737

3838
/// Take the global arguments from global storage.
39-
#[cfg(not(test))] pub fn take() -> Option<~[~str]> { imp::take() }
40-
#[cfg(test)] pub fn take() -> Option<~[~str]> {
39+
#[cfg(not(test))] pub fn take() -> Option<~[~[u8]]> { imp::take() }
40+
#[cfg(test)] pub fn take() -> Option<~[~[u8]]> {
4141
match realargs::take() {
4242
realstd::option::Some(a) => Some(a),
4343
realstd::option::None => None,
@@ -47,12 +47,12 @@ pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
4747
/// Give the global arguments to global storage.
4848
///
4949
/// It is an error if the arguments already exist.
50-
#[cfg(not(test))] pub fn put(args: ~[~str]) { imp::put(args) }
51-
#[cfg(test)] pub fn put(args: ~[~str]) { realargs::put(args) }
50+
#[cfg(not(test))] pub fn put(args: ~[~[u8]]) { imp::put(args) }
51+
#[cfg(test)] pub fn put(args: ~[~[u8]]) { realargs::put(args) }
5252

5353
/// Make a clone of the global arguments.
54-
#[cfg(not(test))] pub fn clone() -> Option<~[~str]> { imp::clone() }
55-
#[cfg(test)] pub fn clone() -> Option<~[~str]> {
54+
#[cfg(not(test))] pub fn clone() -> Option<~[~[u8]]> { imp::clone() }
55+
#[cfg(test)] pub fn clone() -> Option<~[~[u8]]> {
5656
match realargs::clone() {
5757
realstd::option::Some(a) => Some(a),
5858
realstd::option::None => None,
@@ -65,15 +65,12 @@ pub unsafe fn init(argc: int, argv: **u8) { realargs::init(argc, argv) }
6565
mod imp {
6666
use cast;
6767
use clone::Clone;
68-
#[cfg(not(test))] use libc;
6968
use option::{Option, Some, None};
7069
use ptr::RawPtr;
7170
use iter::Iterator;
72-
#[cfg(not(test))] use str;
7371
use unstable::finally::Finally;
7472
use unstable::mutex::{Mutex, MUTEX_INIT};
7573
use mem;
76-
#[cfg(not(test))] use vec;
7774

7875
static mut global_args_ptr: uint = 0;
7976
static mut lock: Mutex = MUTEX_INIT;
@@ -90,26 +87,26 @@ mod imp {
9087
lock.destroy();
9188
}
9289

93-
pub fn take() -> Option<~[~str]> {
90+
pub fn take() -> Option<~[~[u8]]> {
9491
with_lock(|| unsafe {
9592
let ptr = get_global_ptr();
9693
let val = mem::replace(&mut *ptr, None);
97-
val.as_ref().map(|s: &~~[~str]| (**s).clone())
94+
val.as_ref().map(|s: &~~[~[u8]]| (**s).clone())
9895
})
9996
}
10097

101-
pub fn put(args: ~[~str]) {
98+
pub fn put(args: ~[~[u8]]) {
10299
with_lock(|| unsafe {
103100
let ptr = get_global_ptr();
104101
rtassert!((*ptr).is_none());
105102
(*ptr) = Some(~args.clone());
106103
})
107104
}
108105

109-
pub fn clone() -> Option<~[~str]> {
106+
pub fn clone() -> Option<~[~[u8]]> {
110107
with_lock(|| unsafe {
111108
let ptr = get_global_ptr();
112-
(*ptr).as_ref().map(|s: &~~[~str]| (**s).clone())
109+
(*ptr).as_ref().map(|s: &~~[~[u8]]| (**s).clone())
113110
})
114111
}
115112

@@ -126,15 +123,20 @@ mod imp {
126123
})
127124
}
128125

129-
fn get_global_ptr() -> *mut Option<~~[~str]> {
126+
fn get_global_ptr() -> *mut Option<~~[~[u8]]> {
130127
unsafe { cast::transmute(&global_args_ptr) }
131128
}
132129

133130
// Copied from `os`.
134131
#[cfg(not(test))]
135-
unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~str] {
132+
unsafe fn load_argc_and_argv(argc: int, argv: **u8) -> ~[~[u8]] {
133+
use c_str::CString;
134+
use {vec, libc};
135+
use vec::CloneableVector;
136+
136137
vec::from_fn(argc as uint, |i| {
137-
str::raw::from_c_str(*(argv as **libc::c_char).offset(i as int))
138+
let cs = CString::new(*(argv as **libc::c_char).offset(i as int), false);
139+
cs.as_bytes_no_nul().to_owned()
138140
})
139141
}
140142

@@ -149,7 +151,7 @@ mod imp {
149151
// Preserve the actual global state.
150152
let saved_value = take();
151153

152-
let expected = ~[~"happy", ~"today?"];
154+
let expected = ~[bytes!("happy").to_owned(), bytes!("today?").to_owned()];
153155

154156
put(expected.clone());
155157
assert!(clone() == Some(expected.clone()));
@@ -179,15 +181,15 @@ mod imp {
179181
pub fn cleanup() {
180182
}
181183

182-
pub fn take() -> Option<~[~str]> {
184+
pub fn take() -> Option<~[~[u8]]> {
183185
fail!()
184186
}
185187

186-
pub fn put(_args: ~[~str]) {
188+
pub fn put(_args: ~[~[u8]]) {
187189
fail!()
188190
}
189191

190-
pub fn clone() -> Option<~[~str]> {
192+
pub fn clone() -> Option<~[~[u8]]> {
191193
fail!()
192194
}
193195
}

0 commit comments

Comments
 (0)