Skip to content

Commit 2096afe

Browse files
committed
Fix: Clean history and address all reviewer comments
Create ZST-nthback.rs Delete tests/ui/iterators/ZST-nthback.rs
1 parent 467250d commit 2096afe

File tree

2 files changed

+33
-1
lines changed

2 files changed

+33
-1
lines changed

library/alloc/src/vec/into_iter.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -411,7 +411,12 @@ impl<T, A: Allocator> DoubleEndedIterator for IntoIter<T, A> {
411411
// SAFETY: same as for advance_by()
412412
self.end = unsafe { self.end.sub(step_size) };
413413
}
414-
let to_drop = ptr::slice_from_raw_parts_mut(self.end as *mut T, step_size);
414+
let to_drop = if T::IS_ZST {
415+
// ZST may cause unalignment
416+
ptr::slice_from_raw_parts_mut(ptr::NonNull::<T>::dangling().as_ptr(), step_size)
417+
} else {
418+
ptr::slice_from_raw_parts_mut(self.end as *mut T, step_size)
419+
};
415420
// SAFETY: same as for advance_by()
416421
unsafe {
417422
ptr::drop_in_place(to_drop);
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//@ compile-flags: -Zmiri-symbolic-alignment-check
2+
//@ run-pass
3+
4+
use std::ptr;
5+
6+
// This is a ZST (Zero-Sized Type) that requires high alignment (alignment 8).
7+
// The original bug occurred because IntoIter::nth_back tried to calculate an
8+
// unaligned pointer for drop_in_place based on this high-aligned ZST, leading to UB in Miri.
9+
#[repr(align(8))]
10+
struct Thing;
11+
12+
fn main() {
13+
// Create a Vec containing two high-aligned ZSTs.
14+
let v = vec![Thing, Thing];
15+
16+
// Calling IntoIter::nth_back(1) triggers the problematic drop_in_place logic
17+
// within Vec::IntoIter. The fix (using ptr::NonNull::dangling()) prevents the UB.
18+
let mut iter = v.into_iter();
19+
let _ = iter.nth_back(1);
20+
21+
// Also test nth_back(0) for completeness.
22+
let v2 = vec![Thing, Thing];
23+
let mut iter2 = v2.into_iter();
24+
let _ = iter2.nth_back(0);
25+
26+
// The main assertion is that the program runs to completion without Miri reporting UB.
27+
}

0 commit comments

Comments
 (0)