Skip to content

Commit 1a40aa0

Browse files
committed
core: Make converting from a C string unsafe
1 parent 13ae8e0 commit 1a40aa0

File tree

2 files changed

+5
-5
lines changed

2 files changed

+5
-5
lines changed

src/libcore/os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ fn fill_charp_buf(f: fn(*mutable c_char, size_t) -> bool)
6565
-> option<str> {
6666
let buf = vec::to_mut(vec::from_elem(tmpbuf_sz, 0u8 as c_char));
6767
vec::as_mut_buf(buf) { |b|
68-
if f(b, tmpbuf_sz as size_t) {
68+
if f(b, tmpbuf_sz as size_t) unsafe {
6969
some(str::from_buf(b as *u8))
7070
} else {
7171
none

src/libcore/str.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,7 +186,7 @@ fn from_chars(chs: [char]) -> str {
186186
}
187187

188188
#[doc = "Create a Rust string from a null-terminated *u8 buffer"]
189-
fn from_buf(buf: *u8) -> str unsafe {
189+
unsafe fn from_buf(buf: *u8) -> str {
190190
let mut curr = buf, i = 0u;
191191
while *curr != 0u8 {
192192
i += 1u;
@@ -196,12 +196,12 @@ fn from_buf(buf: *u8) -> str unsafe {
196196
}
197197

198198
#[doc = "Create a Rust string from a null-terminated C string"]
199-
fn from_c_str(c_str: *libc::c_char) -> str unsafe {
199+
unsafe fn from_c_str(c_str: *libc::c_char) -> str {
200200
from_buf(::unsafe::reinterpret_cast(c_str))
201201
}
202202

203203
#[doc = "Create a Rust string from a *u8 buffer of the given length"]
204-
fn from_buf_len(buf: *u8, len: uint) -> str unsafe {
204+
unsafe fn from_buf_len(buf: *u8, len: uint) -> str {
205205
let mut v: [u8] = [];
206206
vec::reserve(v, len + 1u);
207207
vec::as_buf(v) {|b| ptr::memcpy(b, buf, len); }
@@ -215,7 +215,7 @@ fn from_buf_len(buf: *u8, len: uint) -> str unsafe {
215215
}
216216

217217
#[doc = "Create a Rust string from a `*c_char` buffer of the given length"]
218-
fn from_c_str_len(c_str: *libc::c_char, len: uint) -> str unsafe {
218+
unsafe fn from_c_str_len(c_str: *libc::c_char, len: uint) -> str {
219219
from_buf_len(::unsafe::reinterpret_cast(c_str), len)
220220
}
221221

0 commit comments

Comments
 (0)