diff options
| author | Brian Anderson <banderson@mozilla.com> | 2012-03-19 14:51:21 -0700 |
|---|---|---|
| committer | Brian Anderson <banderson@mozilla.com> | 2012-03-19 15:47:52 -0700 |
| commit | 1a40aa0935357cbfc8bba6e7b0fd7c5461c9732e (patch) | |
| tree | 12fe37e5bd7cf639dccbd1e6b1e2fa0c1bc1445b /src/libcore | |
| parent | 13ae8e0626da4c808e98595fcaeb9689b47d01df (diff) | |
| download | rust-1a40aa0935357cbfc8bba6e7b0fd7c5461c9732e.tar.gz rust-1a40aa0935357cbfc8bba6e7b0fd7c5461c9732e.zip | |
core: Make converting from a C string unsafe
Diffstat (limited to 'src/libcore')
| -rw-r--r-- | src/libcore/os.rs | 2 | ||||
| -rw-r--r-- | src/libcore/str.rs | 8 |
2 files changed, 5 insertions, 5 deletions
diff --git a/src/libcore/os.rs b/src/libcore/os.rs index 214c9a4fa9c..dea86b5af25 100644 --- a/src/libcore/os.rs +++ b/src/libcore/os.rs @@ -65,7 +65,7 @@ fn fill_charp_buf(f: fn(*mutable c_char, size_t) -> bool) -> option<str> { let buf = vec::to_mut(vec::from_elem(tmpbuf_sz, 0u8 as c_char)); vec::as_mut_buf(buf) { |b| - if f(b, tmpbuf_sz as size_t) { + if f(b, tmpbuf_sz as size_t) unsafe { some(str::from_buf(b as *u8)) } else { none diff --git a/src/libcore/str.rs b/src/libcore/str.rs index 4b656bb5725..e0c15abe590 100644 --- a/src/libcore/str.rs +++ b/src/libcore/str.rs @@ -186,7 +186,7 @@ fn from_chars(chs: [char]) -> str { } #[doc = "Create a Rust string from a null-terminated *u8 buffer"] -fn from_buf(buf: *u8) -> str unsafe { +unsafe fn from_buf(buf: *u8) -> str { let mut curr = buf, i = 0u; while *curr != 0u8 { i += 1u; @@ -196,12 +196,12 @@ fn from_buf(buf: *u8) -> str unsafe { } #[doc = "Create a Rust string from a null-terminated C string"] -fn from_c_str(c_str: *libc::c_char) -> str unsafe { +unsafe fn from_c_str(c_str: *libc::c_char) -> str { from_buf(::unsafe::reinterpret_cast(c_str)) } #[doc = "Create a Rust string from a *u8 buffer of the given length"] -fn from_buf_len(buf: *u8, len: uint) -> str unsafe { +unsafe fn from_buf_len(buf: *u8, len: uint) -> str { let mut v: [u8] = []; vec::reserve(v, len + 1u); vec::as_buf(v) {|b| ptr::memcpy(b, buf, len); } @@ -215,7 +215,7 @@ fn from_buf_len(buf: *u8, len: uint) -> str unsafe { } #[doc = "Create a Rust string from a `*c_char` buffer of the given length"] -fn from_c_str_len(c_str: *libc::c_char, len: uint) -> str unsafe { +unsafe fn from_c_str_len(c_str: *libc::c_char, len: uint) -> str { from_buf_len(::unsafe::reinterpret_cast(c_str), len) } |
