From 1a40aa0935357cbfc8bba6e7b0fd7c5461c9732e Mon Sep 17 00:00:00 2001 From: Brian Anderson Date: Mon, 19 Mar 2012 14:51:21 -0700 Subject: [PATCH] core: Make converting from a C string unsafe --- src/libcore/os.rs | 2 +- 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 214c9a4fa9cd..dea86b5af250 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 { 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 4b656bb5725a..e0c15abe590f 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) }