Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 3d871b3

Browse files
committedJan 4, 2025
do not in-place-iterate over flatmap/flatten
The implementation is unsound when a partially consumed iterator has some elements buffered in the front/back parts and cloning the Iterator removes the capacity from the backing vec::IntoIter.
1 parent fd127a3 commit 3d871b3

File tree

2 files changed

+8
-51
lines changed

2 files changed

+8
-51
lines changed
 

‎library/alloc/tests/vec.rs

+6-19
Original file line numberDiff line numberDiff line change
@@ -1204,22 +1204,16 @@ fn test_from_iter_specialization_with_iterator_adapters() {
12041204
#[test]
12051205
fn test_in_place_specialization_step_up_down() {
12061206
fn assert_in_place_trait<T: InPlaceIterable>(_: &T) {}
1207-
let src = vec![[0u8; 4]; 256];
1207+
1208+
let src = vec![0u8; 1024];
12081209
let srcptr = src.as_ptr();
1209-
let src_cap = src.capacity();
1210-
let iter = src.into_iter().flatten();
1210+
let src_bytes = src.capacity();
1211+
let iter = src.into_iter().array_chunks::<4>();
12111212
assert_in_place_trait(&iter);
12121213
let sink = iter.collect::<Vec<_>>();
12131214
let sinkptr = sink.as_ptr();
1214-
assert_eq!(srcptr as *const u8, sinkptr);
1215-
assert_eq!(src_cap * 4, sink.capacity());
1216-
1217-
let iter = sink.into_iter().array_chunks::<4>();
1218-
assert_in_place_trait(&iter);
1219-
let sink = iter.collect::<Vec<_>>();
1220-
let sinkptr = sink.as_ptr();
1221-
assert_eq!(srcptr, sinkptr);
1222-
assert_eq!(src_cap, sink.capacity());
1215+
assert_eq!(srcptr.addr(), sinkptr.addr());
1216+
assert_eq!(src_bytes, sink.capacity() * 4);
12231217

12241218
let mut src: Vec<u8> = Vec::with_capacity(17);
12251219
let src_bytes = src.capacity();
@@ -1236,13 +1230,6 @@ fn test_in_place_specialization_step_up_down() {
12361230
let sink: Vec<[u8; 2]> = iter.collect();
12371231
assert_eq!(sink.len(), 8);
12381232
assert!(sink.capacity() <= 25);
1239-
1240-
let src = vec![[0u8; 4]; 256];
1241-
let srcptr = src.as_ptr();
1242-
let iter = src.into_iter().flat_map(|a| a.into_iter().map(|b| b.wrapping_add(1)));
1243-
assert_in_place_trait(&iter);
1244-
let sink = iter.collect::<Vec<_>>();
1245-
assert_eq!(srcptr as *const u8, sink.as_ptr());
12461233
}
12471234

12481235
#[test]

‎library/core/src/iter/adapters/flatten.rs

+2-32
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::iter::adapters::SourceIter;
22
use crate::iter::{
3-
Cloned, Copied, Empty, Filter, FilterMap, Fuse, FusedIterator, InPlaceIterable, Map, Once,
4-
OnceWith, TrustedFused, TrustedLen,
3+
Cloned, Copied, Empty, Filter, FilterMap, Fuse, FusedIterator, Map, Once, OnceWith,
4+
TrustedFused, TrustedLen,
55
};
66
use crate::num::NonZero;
77
use crate::ops::{ControlFlow, Try};
@@ -157,21 +157,6 @@ where
157157
{
158158
}
159159

160-
#[unstable(issue = "none", feature = "inplace_iteration")]
161-
unsafe impl<I, U, F> InPlaceIterable for FlatMap<I, U, F>
162-
where
163-
I: InPlaceIterable,
164-
U: BoundedSize + IntoIterator,
165-
{
166-
const EXPAND_BY: Option<NonZero<usize>> = const {
167-
match (I::EXPAND_BY, U::UPPER_BOUND) {
168-
(Some(m), Some(n)) => m.checked_mul(n),
169-
_ => None,
170-
}
171-
};
172-
const MERGE_BY: Option<NonZero<usize>> = I::MERGE_BY;
173-
}
174-
175160
#[unstable(issue = "none", feature = "inplace_iteration")]
176161
unsafe impl<I, U, F> SourceIter for FlatMap<I, U, F>
177162
where
@@ -386,21 +371,6 @@ where
386371
{
387372
}
388373

389-
#[unstable(issue = "none", feature = "inplace_iteration")]
390-
unsafe impl<I> InPlaceIterable for Flatten<I>
391-
where
392-
I: InPlaceIterable + Iterator,
393-
<I as Iterator>::Item: IntoIterator + BoundedSize,
394-
{
395-
const EXPAND_BY: Option<NonZero<usize>> = const {
396-
match (I::EXPAND_BY, I::Item::UPPER_BOUND) {
397-
(Some(m), Some(n)) => m.checked_mul(n),
398-
_ => None,
399-
}
400-
};
401-
const MERGE_BY: Option<NonZero<usize>> = I::MERGE_BY;
402-
}
403-
404374
#[unstable(issue = "none", feature = "inplace_iteration")]
405375
unsafe impl<I> SourceIter for Flatten<I>
406376
where

0 commit comments

Comments
 (0)
Failed to load comments.