Skip to content

Commit a9579fd

Browse files
lilyballalexcrichton
authored andcommitted
---
yaml --- r: 102498 b: refs/heads/auto c: cad4fcd h: refs/heads/master v: v3
1 parent ea66cf6 commit a9579fd

File tree

2 files changed

+37
-4
lines changed

2 files changed

+37
-4
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ refs/heads/try3: 9387340aab40a73e8424c48fd42f0c521a4875c0
1313
refs/tags/release-0.3.1: 495bae036dfe5ec6ceafd3312b4dca48741e845b
1414
refs/tags/release-0.4: e828ea2080499553b97dfe33b3f4d472b4562ad7
1515
refs/tags/release-0.5: 7e3bcfbf21278251ee936ad53e92e9b719702d73
16-
refs/heads/auto: a9f73b5e3d947c3b8ef8c3a8fbd30823ba099886
16+
refs/heads/auto: cad4fcd21b0e620830b818037101e523323746ea
1717
refs/heads/servo: af82457af293e2a842ba6b7759b70288da276167
1818
refs/tags/release-0.6: b4ebcfa1812664df5e142f0134a5faea3918544c
1919
refs/tags/0.1: b19db808c2793fe2976759b85a355c3ad8c8b336

branches/auto/src/libstd/c_str.rs

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -162,17 +162,25 @@ impl CString {
162162
}
163163

164164
/// Converts the CString into a `&str` without copying.
165-
/// Returns None if the CString is not UTF-8 or is null.
165+
/// Returns None if the CString is not UTF-8.
166+
///
167+
/// # Failure
168+
///
169+
/// Fails if the CString is null.
166170
#[inline]
167171
pub fn as_str<'a>(&'a self) -> Option<&'a str> {
168-
if self.buf.is_null() { return None; }
169172
let buf = self.as_bytes();
170173
let buf = buf.slice_to(buf.len()-1); // chop off the trailing NUL
171174
str::from_utf8(buf)
172175
}
173176

174177
/// Return a CString iterator.
178+
///
179+
/// # Failure
180+
///
181+
/// Fails if the CString is null.
175182
pub fn iter<'a>(&'a self) -> CChars<'a> {
183+
if self.buf.is_null() { fail!("CString is null!"); }
176184
CChars {
177185
ptr: self.buf,
178186
marker: marker::ContravariantLifetime,
@@ -191,8 +199,14 @@ impl Drop for CString {
191199
}
192200

193201
impl Container for CString {
202+
/// Return the number of bytes in the CString (not including the NUL terminator).
203+
///
204+
/// # Failure
205+
///
206+
/// Fails if the CString is null.
194207
#[inline]
195208
fn len(&self) -> uint {
209+
if self.buf.is_null() { fail!("CString is null!"); }
196210
unsafe {
197211
ptr::position(self.buf, |c| *c == 0)
198212
}
@@ -562,8 +576,27 @@ mod tests {
562576
assert_eq!(c_str.as_str(), Some(""));
563577
let c_str = bytes!("foo", 0xff).to_c_str();
564578
assert_eq!(c_str.as_str(), None);
579+
}
580+
581+
#[test]
582+
#[should_fail]
583+
fn test_as_str_fail() {
565584
let c_str = unsafe { CString::new(ptr::null(), false) };
566-
assert_eq!(c_str.as_str(), None);
585+
c_str.as_str();
586+
}
587+
588+
#[test]
589+
#[should_fail]
590+
fn test_len_fail() {
591+
let c_str = unsafe { CString::new(ptr::null(), false) };
592+
c_str.len();
593+
}
594+
595+
#[test]
596+
#[should_fail]
597+
fn test_iter_fail() {
598+
let c_str = unsafe { CString::new(ptr::null(), false) };
599+
c_str.iter();
567600
}
568601
}
569602

0 commit comments

Comments
 (0)