-
Notifications
You must be signed in to change notification settings - Fork 13.4k
fix Zip unsoundness (again) #141076
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
the8472
wants to merge
1
commit into
rust-lang:master
Choose a base branch
from
the8472:fix-zip-panic-safety2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−75
Open
fix Zip unsoundness (again) #141076
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
|
@@ -107,9 +107,19 @@ fn test_zip_next_back_side_effects_exhausted() { | |||||||
iter.next(); | ||||||||
iter.next(); | ||||||||
iter.next(); | ||||||||
iter.next(); | ||||||||
assert_eq!(iter.next(), None); | ||||||||
assert_eq!(iter.next_back(), None); | ||||||||
assert_eq!(a, vec![1, 2, 3, 4, 6, 5]); | ||||||||
|
||||||||
assert!(a.starts_with(&[1, 2, 3])); | ||||||||
let a_len = a.len(); | ||||||||
// Tail-side-effects of forward-iteration are "at most one" per next(). | ||||||||
// And for reverse iteration we don't guarantee much either. | ||||||||
// But we can put some bounds on the possible behaviors. | ||||||||
assert!(a_len <= 6); | ||||||||
assert!(a_len >= 3); | ||||||||
a.sort(); | ||||||||
assert_eq!(a, &[1, 2, 3, 4, 5, 6][..a.len()]); | ||||||||
|
||||||||
assert_eq!(b, vec![200, 300, 400]); | ||||||||
} | ||||||||
|
||||||||
|
@@ -120,7 +130,8 @@ fn test_zip_cloned_sideffectful() { | |||||||
|
||||||||
for _ in xs.iter().cloned().zip(ys.iter().cloned()) {} | ||||||||
|
||||||||
assert_eq!(&xs, &[1, 1, 1, 0][..]); | ||||||||
// Zip documentation permits either case. | ||||||||
assert!([&[1, 1, 1, 0], &[1, 1, 0, 0]].iter().any(|v| &xs == *v)); | ||||||||
assert_eq!(&ys, &[1, 1][..]); | ||||||||
|
||||||||
let xs = [CountClone::new(), CountClone::new()]; | ||||||||
|
@@ -139,7 +150,8 @@ fn test_zip_map_sideffectful() { | |||||||
|
||||||||
for _ in xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)) {} | ||||||||
|
||||||||
assert_eq!(&xs, &[1, 1, 1, 1, 1, 0]); | ||||||||
// Zip documentation permits either case. | ||||||||
assert!([&[1, 1, 1, 1, 1, 0], &[1, 1, 1, 1, 0, 0]].iter().any(|v| &xs == *v)); | ||||||||
assert_eq!(&ys, &[1, 1, 1, 1]); | ||||||||
|
||||||||
let mut xs = [0; 4]; | ||||||||
|
@@ -168,7 +180,8 @@ fn test_zip_map_rev_sideffectful() { | |||||||
|
||||||||
{ | ||||||||
let mut it = xs.iter_mut().map(|x| *x += 1).zip(ys.iter_mut().map(|y| *y += 1)); | ||||||||
(&mut it).take(5).count(); | ||||||||
// the current impl only trims the tails if the iterator isn't exhausted | ||||||||
(&mut it).take(3).count(); | ||||||||
it.next_back(); | ||||||||
} | ||||||||
assert_eq!(&xs, &[1, 1, 1, 1, 1, 1]); | ||||||||
|
@@ -211,9 +224,18 @@ fn test_zip_nth_back_side_effects_exhausted() { | |||||||
iter.next(); | ||||||||
iter.next(); | ||||||||
iter.next(); | ||||||||
iter.next(); | ||||||||
assert_eq!(iter.next(), None); | ||||||||
assert_eq!(iter.nth_back(0), None); | ||||||||
assert_eq!(a, vec![1, 2, 3, 4, 6, 5]); | ||||||||
assert!(a.starts_with(&[1, 2, 3])); | ||||||||
let a_len = a.len(); | ||||||||
// Tail-side-effects of forward-iteration are "at most one" per next(). | ||||||||
// And for reverse iteration we don't guarantee much either. | ||||||||
// But we can put some bounds on the possible behaviors. | ||||||||
assert!(a_len <= 6); | ||||||||
assert!(a_len >= 3); | ||||||||
a.sort(); | ||||||||
assert_eq!(a, &[1, 2, 3, 4, 5, 6][..a.len()]); | ||||||||
|
||||||||
assert_eq!(b, vec![200, 300, 400]); | ||||||||
} | ||||||||
|
||||||||
|
@@ -237,32 +259,6 @@ fn test_zip_trusted_random_access_composition() { | |||||||
assert_eq!(z2.next().unwrap(), ((1, 1), 1)); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
#[cfg(panic = "unwind")] | ||||||||
fn test_zip_trusted_random_access_next_back_drop() { | ||||||||
use std::panic::{AssertUnwindSafe, catch_unwind}; | ||||||||
|
||||||||
let mut counter = 0; | ||||||||
|
||||||||
let it = [42].iter().map(|e| { | ||||||||
let c = counter; | ||||||||
counter += 1; | ||||||||
if c == 0 { | ||||||||
panic!("bomb"); | ||||||||
} | ||||||||
|
||||||||
e | ||||||||
}); | ||||||||
let it2 = [(); 0].iter(); | ||||||||
let mut zip = it.zip(it2); | ||||||||
catch_unwind(AssertUnwindSafe(|| { | ||||||||
zip.next_back(); | ||||||||
})) | ||||||||
.unwrap_err(); | ||||||||
assert!(zip.next().is_none()); | ||||||||
assert_eq!(counter, 1); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
fn test_double_ended_zip() { | ||||||||
let xs = [1, 2, 3, 4, 5, 6]; | ||||||||
|
@@ -275,6 +271,41 @@ fn test_double_ended_zip() { | |||||||
assert_eq!(it.next(), None); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
#[cfg(panic = "unwind")] | ||||||||
fn test_nested_zip_panic_safety() { | ||||||||
use std::panic::{AssertUnwindSafe, catch_unwind, resume_unwind}; | ||||||||
use std::sync::atomic::{AtomicUsize, Ordering}; | ||||||||
|
||||||||
let mut panic = true; | ||||||||
let witness = [8, 9, 10, 11, 12].map(|i| (i, AtomicUsize::new(0))); | ||||||||
let a = witness.as_slice().iter().map(|e| { | ||||||||
e.1.fetch_add(1, Ordering::Relaxed); | ||||||||
if panic { | ||||||||
panic = false; | ||||||||
resume_unwind(Box::new(())) | ||||||||
} | ||||||||
e.0 | ||||||||
}); | ||||||||
let b = [1, 2, 3, 4].as_slice().iter().copied(); | ||||||||
let c = [5, 6, 7].as_slice().iter().copied(); | ||||||||
let ab = zip(a, b); | ||||||||
|
||||||||
let mut abc = zip(ab, c); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this correct?
Suggested change
|
||||||||
|
||||||||
assert_eq!(abc.len(), 3); | ||||||||
catch_unwind(AssertUnwindSafe(|| abc.next_back())).ok(); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ...feels like there should probably be a comment here also, maybe on the incorrectness we used to encounter from doing this? |
||||||||
assert_eq!(abc.len(), 2); | ||||||||
assert_eq!(abc.next(), Some(((8, 1), 5))); | ||||||||
assert_eq!(abc.next_back(), Some(((9, 2), 6))); | ||||||||
for (i, (_, w)) in witness.iter().enumerate() { | ||||||||
let v = w.load(Ordering::Relaxed); | ||||||||
assert!(v <= 1, "expected idx {i} to be visited at most once, actual: {v}"); | ||||||||
} | ||||||||
// backwards trimming panicked and should only run once, so this one won't be visited. | ||||||||
assert_eq!(witness[3].1.load(Ordering::Relaxed), 0); | ||||||||
} | ||||||||
|
||||||||
#[test] | ||||||||
fn test_issue_82282() { | ||||||||
fn overflowed_zip(arr: &[i32]) -> impl Iterator<Item = (i32, &())> { | ||||||||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.