1
Fork 0

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> { -> option<str> {
let buf = vec::to_mut(vec::from_elem(tmpbuf_sz, 0u8 as c_char)); let buf = vec::to_mut(vec::from_elem(tmpbuf_sz, 0u8 as c_char));
vec::as_mut_buf(buf) { |b| 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)) some(str::from_buf(b as *u8))
} else { } else {
none none

View file

@ -186,7 +186,7 @@ fn from_chars(chs: [char]) -> str {
} }
#[doc = "Create a Rust string from a null-terminated *u8 buffer"] #[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; let mut curr = buf, i = 0u;
while *curr != 0u8 { while *curr != 0u8 {
i += 1u; i += 1u;
@ -196,12 +196,12 @@ fn from_buf(buf: *u8) -> str unsafe {
} }
#[doc = "Create a Rust string from a null-terminated C string"] #[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)) from_buf(::unsafe::reinterpret_cast(c_str))
} }
#[doc = "Create a Rust string from a *u8 buffer of the given length"] #[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] = []; let mut v: [u8] = [];
vec::reserve(v, len + 1u); vec::reserve(v, len + 1u);
vec::as_buf(v) {|b| ptr::memcpy(b, buf, len); } 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"] #[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) from_buf_len(::unsafe::reinterpret_cast(c_str), len)
} }