core: Make converting from a C string unsafe

This commit is contained in:
Brian Anderson 2012-03-19 14:51:21 -07:00
parent 13ae8e0626
commit 1a40aa0935
2 changed files with 5 additions and 5 deletions

View File

@ -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

View File

@ -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)
}