Skip to content

Commit

Permalink
std::vec: Use a valid value as lifetime dummy in iterator
Browse files Browse the repository at this point in the history
The current implementation uses `&v[0]` for the lifetime struct field,
but that is a dangling pointer for iterators derived from zero-length
slices.

Example:

    let v: [int, ..0] = [];  println!("{:?}", v.iter())

    std::vec::VecIterator<,int>{ptr: (0x7f3768626100 as *()), end: (0x7f3768626100 as *()), lifetime: &139875951207128}

To replace this parameter, use a field of type `Option<&'self ()>`
that is simply initialized with `None`, but still allows the iterator to
have a lifetime parameter.
  • Loading branch information
blake2-ppc committed Sep 27, 2013
1 parent 48499c7 commit c0e1c09
Showing 1 changed file with 6 additions and 6 deletions.
12 changes: 6 additions & 6 deletions src/libstd/vec.rs
Expand Up @@ -932,11 +932,11 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
if sys::size_of::<T>() == 0 {
VecIterator{ptr: p,
end: (p as uint + self.len()) as *T,
lifetime: cast::transmute(p)}
lifetime: None}
} else {
VecIterator{ptr: p,
end: p.offset(self.len() as int),
lifetime: cast::transmute(p)}
lifetime: None}
}
}
}
Expand Down Expand Up @@ -1940,11 +1940,11 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
if sys::size_of::<T>() == 0 {
VecMutIterator{ptr: p,
end: (p as uint + self.len()) as *mut T,
lifetime: cast::transmute(p)}
lifetime: None}
} else {
VecMutIterator{ptr: p,
end: p.offset(self.len() as int),
lifetime: cast::transmute(p)}
lifetime: None}
}
}
}
Expand Down Expand Up @@ -2389,7 +2389,7 @@ impl<'self, T> RandomAccessIterator<&'self T> for VecIterator<'self, T> {
pub struct VecIterator<'self, T> {
priv ptr: *T,
priv end: *T,
priv lifetime: &'self T // FIXME: #5922
priv lifetime: Option<&'self ()> // FIXME: #5922
}
iterator!{impl VecIterator -> &'self T}
double_ended_iterator!{impl VecIterator -> &'self T}
Expand All @@ -2407,7 +2407,7 @@ impl<'self, T> Clone for VecIterator<'self, T> {
pub struct VecMutIterator<'self, T> {
priv ptr: *mut T,
priv end: *mut T,
priv lifetime: &'self mut T // FIXME: #5922
priv lifetime: Option<&'self mut ()> // FIXME: #5922
}
iterator!{impl VecMutIterator -> &'self mut T}
double_ended_iterator!{impl VecMutIterator -> &'self mut T}
Expand Down

5 comments on commit c0e1c09

@bors
Copy link
Contributor

@bors bors commented on c0e1c09 Sep 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@bors
Copy link
Contributor

@bors bors commented on c0e1c09 Sep 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

merging blake2-ppc/rust/vec-lifetime-token = c0e1c09 into auto

@bors
Copy link
Contributor

@bors bors commented on c0e1c09 Sep 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

blake2-ppc/rust/vec-lifetime-token = c0e1c09 merged ok, testing candidate = 3ae8953

@bors
Copy link
Contributor

@bors bors commented on c0e1c09 Sep 28, 2013

@bors
Copy link
Contributor

@bors bors commented on c0e1c09 Sep 28, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fast-forwarding master to auto = 3ae8953

Please sign in to comment.