Skip to content

Commit c1c9499

Browse files
aochagaviaalexcrichton
authored andcommitted
---
yaml --- r: 126783 b: refs/heads/snap-stage3 c: 4ea1dd5 h: refs/heads/master i: 126781: add17d8 126779: 3f02fa3 126775: dd893fc 126767: 4ffafb2 126751: 2a0270a 126719: 11a914f v: v3
1 parent 91d7727 commit c1c9499

File tree

3 files changed

+20
-115
lines changed

3 files changed

+20
-115
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
---
22
refs/heads/master: 7be8f0af0393dcdb077c2f6b1653836fd3fba235
33
refs/heads/snap-stage1: e33de59e47c5076a89eadeb38f4934f58a3618a6
4-
refs/heads/snap-stage3: 6988bcd74c6ab85d0a5f85c8b96c61f7fb75ad1d
4+
refs/heads/snap-stage3: 4ea1dd54943bb208d585c7ae8138df5699995a77
55
refs/heads/try: 502e4c045236682e9728539dc0d2b3d0b237f55c
66
refs/tags/release-0.1: 1f5c5126e96c79d22cb7862f75304136e204f105
77
refs/heads/ndm: f3868061cd7988080c30d6d5bf352a5a5fe2460b

branches/snap-stage3/src/libnative/io/addrinfo.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010

1111
use libc::{c_char, c_int};
1212
use libc;
13-
use std::c_str::CString;
1413
use std::mem;
1514
use std::ptr::{null, mut_null};
1615
use std::rt::rtio;
@@ -27,8 +26,8 @@ impl GetAddrInfoRequest {
2726
{
2827
assert!(host.is_some() || servname.is_some());
2928

30-
let c_host = host.map_or(unsafe { CString::new(null(), true) }, |x| x.to_c_str());
31-
let c_serv = servname.map_or(unsafe { CString::new(null(), true) }, |x| x.to_c_str());
29+
let c_host = host.map(|x| x.to_c_str());
30+
let c_serv = servname.map(|x| x.to_c_str());
3231

3332
let hint = hint.map(|hint| {
3433
libc::addrinfo {
@@ -50,8 +49,8 @@ impl GetAddrInfoRequest {
5049

5150
// Make the call
5251
let s = unsafe {
53-
let ch = if c_host.is_null() { null() } else { c_host.as_ptr() };
54-
let cs = if c_serv.is_null() { null() } else { c_serv.as_ptr() };
52+
let ch = if c_host.is_none() { null() } else { c_host.unwrap().as_ptr() };
53+
let cs = if c_serv.is_none() { null() } else { c_serv.unwrap().as_ptr() };
5554
getaddrinfo(ch, cs, hint_ptr, &mut res)
5655
};
5756

@@ -104,6 +103,7 @@ fn get_error(_: c_int) -> IoError {
104103

105104
#[cfg(not(windows))]
106105
fn get_error(s: c_int) -> IoError {
106+
use std::c_str::CString;
107107

108108
let err_str = unsafe {
109109
CString::new(gai_strerror(s), false).as_str().unwrap().to_string()

branches/snap-stage3/src/librustrt/c_str.rs

Lines changed: 14 additions & 109 deletions
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,10 @@ use core::prelude::*;
7070
use alloc::libc_heap::malloc_raw;
7171
use collections::string::String;
7272
use collections::hash;
73+
use core::fmt;
7374
use core::kinds::marker;
7475
use core::mem;
7576
use core::ptr;
76-
use core::fmt;
7777
use core::raw::Slice;
7878
use core::slice;
7979
use core::str;
@@ -93,23 +93,18 @@ impl Clone for CString {
9393
/// reasons, this is always a deep clone, rather than the usual shallow
9494
/// clone.
9595
fn clone(&self) -> CString {
96-
if self.buf.is_null() {
97-
CString { buf: self.buf, owns_buffer_: self.owns_buffer_ }
98-
} else {
99-
let len = self.len() + 1;
100-
let buf = unsafe { malloc_raw(len) } as *mut libc::c_char;
101-
unsafe { ptr::copy_nonoverlapping_memory(buf, self.buf, len); }
102-
CString { buf: buf as *const libc::c_char, owns_buffer_: true }
103-
}
96+
let len = self.len() + 1;
97+
let buf = unsafe { malloc_raw(len) } as *mut libc::c_char;
98+
unsafe { ptr::copy_nonoverlapping_memory(buf, self.buf, len); }
99+
CString { buf: buf as *const libc::c_char, owns_buffer_: true }
104100
}
105101
}
106102

107103
impl PartialEq for CString {
108104
fn eq(&self, other: &CString) -> bool {
105+
// Check if the two strings share the same buffer
109106
if self.buf as uint == other.buf as uint {
110107
true
111-
} else if self.buf.is_null() || other.buf.is_null() {
112-
false
113108
} else {
114109
unsafe {
115110
libc::strcmp(self.buf, other.buf) == 0
@@ -136,7 +131,12 @@ impl<S: hash::Writer> hash::Hash<S> for CString {
136131

137132
impl CString {
138133
/// Create a C String from a pointer.
134+
///
135+
///# Failure
136+
///
137+
/// Fails if `buf` is null
139138
pub unsafe fn new(buf: *const libc::c_char, owns_buffer: bool) -> CString {
139+
assert!(!buf.is_null());
140140
CString { buf: buf, owns_buffer_: owns_buffer }
141141
}
142142

@@ -158,10 +158,6 @@ impl CString {
158158
/// let p = foo.to_c_str().as_ptr();
159159
/// ```
160160
///
161-
/// # Failure
162-
///
163-
/// Fails if the CString is null.
164-
///
165161
/// # Example
166162
///
167163
/// ```rust
@@ -175,8 +171,6 @@ impl CString {
175171
/// }
176172
/// ```
177173
pub fn as_ptr(&self) -> *const libc::c_char {
178-
if self.buf.is_null() { fail!("CString is null!"); }
179-
180174
self.buf
181175
}
182176

@@ -197,44 +191,30 @@ impl CString {
197191
/// // wrong (the CString will be freed, invalidating `p`)
198192
/// let p = foo.to_c_str().as_mut_ptr();
199193
/// ```
200-
///
201-
/// # Failure
202-
///
203-
/// Fails if the CString is null.
204194
pub fn as_mut_ptr(&mut self) -> *mut libc::c_char {
205-
if self.buf.is_null() { fail!("CString is null!") }
206-
207195
self.buf as *mut _
208196
}
209197

210198
/// Calls a closure with a reference to the underlying `*libc::c_char`.
211-
///
212-
/// # Failure
213-
///
214-
/// Fails if the CString is null.
215199
#[deprecated="use `.as_ptr()`"]
216200
pub fn with_ref<T>(&self, f: |*const libc::c_char| -> T) -> T {
217-
if self.buf.is_null() { fail!("CString is null!"); }
218201
f(self.buf)
219202
}
220203

221204
/// Calls a closure with a mutable reference to the underlying `*libc::c_char`.
222-
///
223-
/// # Failure
224-
///
225-
/// Fails if the CString is null.
226205
#[deprecated="use `.as_mut_ptr()`"]
227206
pub fn with_mut_ref<T>(&mut self, f: |*mut libc::c_char| -> T) -> T {
228-
if self.buf.is_null() { fail!("CString is null!"); }
229207
f(self.buf as *mut libc::c_char)
230208
}
231209

232210
/// Returns true if the CString is a null.
211+
#[deprecated="a CString cannot be null"]
233212
pub fn is_null(&self) -> bool {
234213
self.buf.is_null()
235214
}
236215

237216
/// Returns true if the CString is not null.
217+
#[deprecated="a CString cannot be null"]
238218
pub fn is_not_null(&self) -> bool {
239219
self.buf.is_not_null()
240220
}
@@ -246,51 +226,32 @@ impl CString {
246226

247227
/// Converts the CString into a `&[u8]` without copying.
248228
/// Includes the terminating NUL byte.
249-
///
250-
/// # Failure
251-
///
252-
/// Fails if the CString is null.
253229
#[inline]
254230
pub fn as_bytes<'a>(&'a self) -> &'a [u8] {
255-
if self.buf.is_null() { fail!("CString is null!"); }
256231
unsafe {
257232
mem::transmute(Slice { data: self.buf, len: self.len() + 1 })
258233
}
259234
}
260235

261236
/// Converts the CString into a `&[u8]` without copying.
262237
/// Does not include the terminating NUL byte.
263-
///
264-
/// # Failure
265-
///
266-
/// Fails if the CString is null.
267238
#[inline]
268239
pub fn as_bytes_no_nul<'a>(&'a self) -> &'a [u8] {
269-
if self.buf.is_null() { fail!("CString is null!"); }
270240
unsafe {
271241
mem::transmute(Slice { data: self.buf, len: self.len() })
272242
}
273243
}
274244

275245
/// Converts the CString into a `&str` without copying.
276246
/// Returns None if the CString is not UTF-8.
277-
///
278-
/// # Failure
279-
///
280-
/// Fails if the CString is null.
281247
#[inline]
282248
pub fn as_str<'a>(&'a self) -> Option<&'a str> {
283249
let buf = self.as_bytes_no_nul();
284250
str::from_utf8(buf)
285251
}
286252

287253
/// Return a CString iterator.
288-
///
289-
/// # Failure
290-
///
291-
/// Fails if the CString is null.
292254
pub fn iter<'a>(&'a self) -> CChars<'a> {
293-
if self.buf.is_null() { fail!("CString is null!"); }
294255
CChars {
295256
ptr: self.buf,
296257
marker: marker::ContravariantLifetime,
@@ -326,13 +287,8 @@ impl Drop for CString {
326287

327288
impl Collection for CString {
328289
/// Return the number of bytes in the CString (not including the NUL terminator).
329-
///
330-
/// # Failure
331-
///
332-
/// Fails if the CString is null.
333290
#[inline]
334291
fn len(&self) -> uint {
335-
if self.buf.is_null() { fail!("CString is null!"); }
336292
let mut cur = self.buf;
337293
let mut len = 0;
338294
unsafe {
@@ -631,13 +587,6 @@ mod tests {
631587
}
632588
}
633589

634-
#[test]
635-
fn test_is_null() {
636-
let c_str = unsafe { CString::new(ptr::null(), false) };
637-
assert!(c_str.is_null());
638-
assert!(!c_str.is_not_null());
639-
}
640-
641590
#[test]
642591
fn test_unwrap() {
643592
let c_str = "hello".to_c_str();
@@ -648,16 +597,8 @@ mod tests {
648597
fn test_as_ptr() {
649598
let c_str = "hello".to_c_str();
650599
let len = unsafe { libc::strlen(c_str.as_ptr()) };
651-
assert!(!c_str.is_null());
652-
assert!(c_str.is_not_null());
653600
assert_eq!(len, 5);
654601
}
655-
#[test]
656-
#[should_fail]
657-
fn test_as_ptr_empty_fail() {
658-
let c_str = unsafe { CString::new(ptr::null(), false) };
659-
c_str.as_ptr();
660-
}
661602

662603
#[test]
663604
fn test_iterator() {
@@ -716,20 +657,6 @@ mod tests {
716657
assert_eq!(c_str.as_bytes_no_nul(), b"foo\xFF");
717658
}
718659

719-
#[test]
720-
#[should_fail]
721-
fn test_as_bytes_fail() {
722-
let c_str = unsafe { CString::new(ptr::null(), false) };
723-
c_str.as_bytes();
724-
}
725-
726-
#[test]
727-
#[should_fail]
728-
fn test_as_bytes_no_nul_fail() {
729-
let c_str = unsafe { CString::new(ptr::null(), false) };
730-
c_str.as_bytes_no_nul();
731-
}
732-
733660
#[test]
734661
fn test_as_str() {
735662
let c_str = "hello".to_c_str();
@@ -742,23 +669,8 @@ mod tests {
742669

743670
#[test]
744671
#[should_fail]
745-
fn test_as_str_fail() {
746-
let c_str = unsafe { CString::new(ptr::null(), false) };
747-
c_str.as_str();
748-
}
749-
750-
#[test]
751-
#[should_fail]
752-
fn test_len_fail() {
672+
fn test_new_fail() {
753673
let c_str = unsafe { CString::new(ptr::null(), false) };
754-
c_str.len();
755-
}
756-
757-
#[test]
758-
#[should_fail]
759-
fn test_iter_fail() {
760-
let c_str = unsafe { CString::new(ptr::null(), false) };
761-
c_str.iter();
762674
}
763675

764676
#[test]
@@ -791,13 +703,6 @@ mod tests {
791703
// force a copy, reading the memory
792704
c_.as_bytes().to_vec();
793705
}
794-
795-
#[test]
796-
fn test_clone_eq_null() {
797-
let x = unsafe { CString::new(ptr::null(), false) };
798-
let y = x.clone();
799-
assert!(x == y);
800-
}
801706
}
802707

803708
#[cfg(test)]

0 commit comments

Comments
 (0)