Skip to content

Commit 6a24ca3

Browse files
committed
---
yaml --- r: 83510 b: refs/heads/try c: b1ee87f h: refs/heads/master v: v3
1 parent 7b559d0 commit 6a24ca3

File tree

2 files changed

+21
-29
lines changed

2 files changed

+21
-29
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
refs/heads/master: 0e4d1fc8cae42e15e00f71d9f439b01bb25a86ae
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
44
refs/heads/snap-stage3: 6c08cc2db4f98e9f07ae7d50338396c4123c2f0a
5-
refs/heads/try: 2d878033fd77f13a41bb9142ba2a4e9b976d0089
5+
refs/heads/try: b1ee87f402f929689fc028010c450184f661db3b
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b
88
refs/heads/try2: 147ecfdd8221e4a4d4e090486829a06da1e0ca3c

branches/try/src/libstd/c_str.rs

Lines changed: 20 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ use ptr;
7171
use str::StrSlice;
7272
use str;
7373
use vec::{CopyableVector, ImmutableVector, MutableVector};
74+
use vec;
7475
use unstable::intrinsics;
7576

7677
/// Resolution options for the `null_byte` condition
@@ -283,41 +284,32 @@ impl<'self> ToCStr for &'self [u8] {
283284
}
284285

285286
fn with_c_str<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
286-
if self.len() < BUF_LEN {
287-
do self.as_imm_buf |self_buf, self_len| {
288-
unsafe {
289-
let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit();
290-
291-
do buf.as_mut_buf |buf, _| {
292-
ptr::copy_memory(buf, self_buf, self_len);
293-
*ptr::mut_offset(buf, self_len as int) = 0;
294-
295-
check_for_null(*self, buf as *mut libc::c_char);
296-
297-
f(buf as *libc::c_char)
298-
}
299-
}
300-
}
301-
} else {
302-
self.to_c_str().with_ref(f)
303-
}
287+
unsafe { with_c_str(*self, true, f) }
304288
}
305289

306290
unsafe fn with_c_str_unchecked<T>(&self, f: &fn(*libc::c_char) -> T) -> T {
307-
if self.len() < BUF_LEN {
308-
do self.as_imm_buf |self_buf, self_len| {
309-
let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit();
291+
with_c_str(*self, false, f)
292+
}
293+
}
310294

311-
do buf.as_mut_buf |buf, _| {
312-
ptr::copy_memory(buf, self_buf, self_len);
313-
*ptr::mut_offset(buf, self_len as int) = 0;
295+
// Unsafe function that handles possibly copying the &[u8] into a stack array.
296+
unsafe fn with_c_str<T>(v: &[u8], checked: bool, f: &fn(*libc::c_char) -> T) -> T {
297+
if v.len() < BUF_LEN {
298+
let mut buf: [u8, .. BUF_LEN] = intrinsics::uninit();
299+
vec::bytes::copy_memory(buf, v, v.len());
300+
buf[v.len()] = 0;
314301

315-
f(buf as *libc::c_char)
316-
}
302+
do buf.as_mut_buf |buf, _| {
303+
if checked {
304+
check_for_null(v, buf as *mut libc::c_char);
317305
}
318-
} else {
319-
self.to_c_str().with_ref(f)
306+
307+
f(buf as *libc::c_char)
320308
}
309+
} else if checked {
310+
v.to_c_str().with_ref(f)
311+
} else {
312+
v.to_c_str_unchecked().with_ref(f)
321313
}
322314
}
323315

0 commit comments

Comments
 (0)