Description
Hi there, we (Rust group @sslab-gatech) are scanning crates on crates.io for potential soundness bugs. We noticed that in swap_index
, the length returned by the iterator is used to set the length of the vector:
Lines 46 to 56 in 59ad9be
However, as noted in the documentation for ExactSizeIterator
's len()
function:
This function has the same safety guarantees as the Iterator::size_hint function.
and then size_hint
's documentation says:
size_hint()
is primarily intended to be used for optimizations such as reserving space for the elements of the iterator, but must not be trusted to e.g., omit bounds checks in unsafe code. An incorrect implementation ofsize_hint()
should not lead to memory safety violations.
Here's an example of some code that will use uninitialized memory through this method:
#![forbid(unsafe_code)]
use reorder::swap_index;
struct IteratorWithWrongLength();
impl Iterator for IteratorWithWrongLength {
type Item = u32;
fn next(&mut self) -> Option<Self::Item> { None }
}
impl ExactSizeIterator for IteratorWithWrongLength {
fn len(&self) -> usize { 1024 }
}
fn main() {
let v = swap_index(IteratorWithWrongLength());
println!("{:?}", v);
}