Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
74648b5
First attempt at fixing #20591
tbelaire Apr 25, 2015
168615f
Now passing in the ImportResolver to check_conflict...
tbelaire Apr 25, 2015
69a5c37
Maybe it works
tbelaire Apr 25, 2015
5c05278
Fixed types, and slimmed down code
tbelaire Apr 25, 2015
372c69d
Fixed test text
tbelaire Apr 27, 2015
db9d018
Fixed some nits
tbelaire May 1, 2015
efb3872
Fix use of UFCS syntax to call methods on associated types.
quantheory May 7, 2015
51a1e83
doc: Remove mention of 30 minute intro
brson May 8, 2015
7d9e605
Add error explanation for E0317.
meqif May 10, 2015
c0412bc
Fix documentation URL in diagnostic message.
meqif May 10, 2015
f3a3684
Add error explanation for E0154.
meqif May 10, 2015
e7fa00a
Add error explanation for E0259.
meqif May 10, 2015
60ec4ab
Add error explanation for E0260.
meqif May 10, 2015
bff1707
Fixed one textual mistake and one casing error.
michal-czardybon May 8, 2015
ef03055
Improve wording in error explanation.
meqif May 11, 2015
aa529ef
Add missing keyword in `extern crate` declarations.
meqif May 11, 2015
52efe55
Handle overflow properly in core::slice
lilyball May 11, 2015
e1e34e9
Reintroduce non-null assumptions in core::slice iterators
lilyball May 11, 2015
f2614f5
Avoid returning a slice with a null pointer from Iter.as_slice()
lilyball May 11, 2015
d13f765
Make mention of `if` more generic
frewsxcv May 12, 2015
8bcb3cb
rustc::metadata: use u64 for DefId's instead of strings.
eddyb May 12, 2015
aeb92ba
rustc: rename ty::populate_implementations_for_type_if_necessary to m…
eddyb May 12, 2015
592165f
Fix ty::populate_implementations_for_trait_if_necessary to load the t…
eddyb May 12, 2015
75cd8f9
rustc_typeck: remove the "preload all impls ever" workaround in coher…
eddyb May 12, 2015
93c21c7
Correct claims about &T's Copyness.
Ms2ger May 12, 2015
feac9f1
Auto merge of #24818 - tbelaire:double-import, r=nrc
bors May 12, 2015
a5cac55
Add wait and waitpid to libc.
mfs May 12, 2015
1d1570a
Add regression test for #18075
May 11, 2015
23300a3
Add regression test for #20413
May 12, 2015
67dfc17
Auto merge of #25323 - eddyb:coherent-coherence, r=pnkfelix
bors May 12, 2015
6faa8d6
Add a link to the error index to the main doc page.
michaelsproul May 12, 2015
0ad2026
Auto merge of #25171 - quantheory:associated_time_long_paths, r=nikom…
bors May 12, 2015
2a5a320
Auto merge of #25300 - kballard:core-slice-overflow, r=Gankro
bors May 12, 2015
e780fb2
TRPL: Borrow and AsRef
steveklabnik Apr 30, 2015
12dc6ee
Rollup merge of #24996 - steveklabnik:gh24163, r=aturon
Manishearth May 12, 2015
bea12a9
Rollup merge of #25220 - brson:doc1, r=steveklabnik
Manishearth May 12, 2015
e91d272
Rollup merge of #25221 - michal-czardybon:master, r=steveklabnik
Manishearth May 12, 2015
bb15c47
Rollup merge of #25267 - meqif:explain_e0317, r=alexcrichton
Manishearth May 12, 2015
e216057
Rollup merge of #25322 - frewsxcv:patch-23, r=steveklabnik
Manishearth May 12, 2015
78cf0f8
Rollup merge of #25327 - Ms2ger:copy-ref, r=pnkfelix
Manishearth May 12, 2015
866991e
Rollup merge of #25329 - jooert:tests, r=alexcrichton
Manishearth May 12, 2015
210f4b9
Rollup merge of #25330 - mfs:add-wait-waitpid, r=alexcrichton
Manishearth May 12, 2015
3981481
Rollup merge of #25331 - michaelsproul:err-idx-doc-link, r=Manishearth
Manishearth May 12, 2015
2581565
Rollup merge of #25335 - nikomatsakis:updates-to-reference-manual, r=…
Manishearth May 12, 2015
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Avoid returning a slice with a null pointer from Iter.as_slice()
core::slice::Iter.ptr can be null when iterating a slice of zero-sized
elements, but the pointer value used for the slice itself cannot. Handle
this case by always returning a dummy pointer for slices of zero-sized
elements.
  • Loading branch information
lilyball committed May 11, 2015
commit f2614f5858fed10e180102def32c60f180e46496
38 changes: 19 additions & 19 deletions src/libcore/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,29 +728,29 @@ macro_rules! iterator {
}

macro_rules! make_slice {
($t: ty => $result: ty: $start: expr, $end: expr) => {{
let diff = ($end as usize).wrapping_sub($start as usize);
let len = if mem::size_of::<T>() == 0 {
diff
($start: expr, $end: expr) => {{
let start = $start;
let diff = ($end as usize).wrapping_sub(start as usize);
if size_from_ptr(start) == 0 {
// use a non-null pointer value
unsafe { from_raw_parts(1 as *const _, diff) }
} else {
diff / mem::size_of::<$t>()
};
unsafe {
from_raw_parts($start, len)
let len = diff / size_from_ptr(start);
unsafe { from_raw_parts(start, len) }
}
}}
}

macro_rules! make_mut_slice {
($t: ty => $result: ty: $start: expr, $end: expr) => {{
let diff = ($end as usize).wrapping_sub($start as usize);
let len = if mem::size_of::<T>() == 0 {
diff
($start: expr, $end: expr) => {{
let start = $start;
let diff = ($end as usize).wrapping_sub(start as usize);
if size_from_ptr(start) == 0 {
// use a non-null pointer value
unsafe { from_raw_parts_mut(1 as *mut _, diff) }
} else {
diff / mem::size_of::<$t>()
};
unsafe {
from_raw_parts_mut($start, len)
let len = diff / size_from_ptr(start);
unsafe { from_raw_parts_mut(start, len) }
}
}}
}
Expand All @@ -773,7 +773,7 @@ impl<'a, T> Iter<'a, T> {
/// iterator can continue to be used while this exists.
#[unstable(feature = "core")]
pub fn as_slice(&self) -> &'a [T] {
make_slice!(T => &'a [T]: self.ptr, self.end)
make_slice!(self.ptr, self.end)
}

// Helper function for Iter::nth
Expand Down Expand Up @@ -841,12 +841,12 @@ impl<'a, T> IterMut<'a, T> {
/// restricted lifetimes that do not consume the iterator.
#[unstable(feature = "core")]
pub fn into_slice(self) -> &'a mut [T] {
make_mut_slice!(T => &'a mut [T]: self.ptr, self.end)
make_mut_slice!(self.ptr, self.end)
}

// Helper function for IterMut::nth
fn iter_nth(&mut self, n: usize) -> Option<&'a mut T> {
match make_mut_slice!(T => &'a mut [T]: self.ptr, self.end).get_mut(n) {
match make_mut_slice!(self.ptr, self.end).get_mut(n) {
Some(elem_ref) => unsafe {
self.ptr = slice_offset!(self.ptr, (n as isize).wrapping_add(1));
Some(slice_ref!(elem_ref))
Expand Down
32 changes: 29 additions & 3 deletions src/test/run-pass/slice-of-zero-size-elements.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,26 @@

// compile-flags: -C debug-assertions

#![feature(core)]

use std::slice;

fn foo<T>(v: &[T]) -> Option<&[T]> {
let mut it = v.iter();
for _ in 0..5 {
let _ = it.next();
}
Some(it.as_slice())
}

fn foo_mut<T>(v: &mut [T]) -> Option<&mut [T]> {
let mut it = v.iter_mut();
for _ in 0..5 {
let _ = it.next();
}
Some(it.into_slice())
}

pub fn main() {
// In a slice of zero-size elements the pointer is meaningless.
// Ensure iteration still works even if the pointer is at the end of the address space.
Expand All @@ -24,11 +42,19 @@ pub fn main() {
assert!(it.nth(5).is_some());
assert_eq!(it.count(), 4);

// Converting Iter to a slice should never have a null pointer
assert!(foo(slice).is_some());

// Test mutable iterators as well
let slice: &mut [()] = unsafe { slice::from_raw_parts_mut(-5isize as *mut (), 10) };
assert_eq!(slice.len(), 10);
assert_eq!(slice.iter_mut().count(), 10);

let mut it = slice.iter_mut();
assert!(it.nth(5).is_some());
assert_eq!(it.count(), 4);
{
let mut it = slice.iter_mut();
assert!(it.nth(5).is_some());
assert_eq!(it.count(), 4);
}

assert!(foo_mut(slice).is_some())
}