diff options
| author | David Tolnay <dtolnay@gmail.com> | 2022-12-18 09:40:23 -0800 |
|---|---|---|
| committer | David Tolnay <dtolnay@gmail.com> | 2023-01-14 13:28:14 -0800 |
| commit | 4fe167f83a90f3f193c3cb5dafb4c2862d0671e9 (patch) | |
| tree | 8496f5243441583f40266f3d440add96013c76dd /library/alloc | |
| parent | b8f9cb345ab1401f2fbd14cc23f64dda9dd2314e (diff) | |
| download | rust-4fe167f83a90f3f193c3cb5dafb4c2862d0671e9.tar.gz rust-4fe167f83a90f3f193c3cb5dafb4c2862d0671e9.zip | |
Add test of leaking a binary_heap PeekMut
Diffstat (limited to 'library/alloc')
| -rw-r--r-- | library/alloc/src/collections/binary_heap/tests.rs | 19 | ||||
| -rw-r--r-- | library/alloc/src/lib.rs | 1 |
2 files changed, 20 insertions, 0 deletions
diff --git a/library/alloc/src/collections/binary_heap/tests.rs b/library/alloc/src/collections/binary_heap/tests.rs index 59c516374c0..ffbb6c80ac0 100644 --- a/library/alloc/src/collections/binary_heap/tests.rs +++ b/library/alloc/src/collections/binary_heap/tests.rs @@ -1,6 +1,7 @@ use super::*; use crate::boxed::Box; use crate::testing::crash_test::{CrashTestDummy, Panic}; +use core::mem; use std::iter::TrustedLen; use std::panic::{catch_unwind, AssertUnwindSafe}; @@ -147,6 +148,24 @@ fn test_peek_mut() { } #[test] +fn test_peek_mut_leek() { + let data = vec![4, 2, 7]; + let mut heap = BinaryHeap::from(data); + let mut max = heap.peek_mut().unwrap(); + *max = -1; + + // The PeekMut object's Drop impl would have been responsible for moving the + // -1 out of the max position of the BinaryHeap, but we don't run it. + mem::forget(max); + + // Absent some mitigation like leak amplification, the -1 would incorrectly + // end up in the last position of the returned Vec, with the rest of the + // heap's original contents in front of it in sorted order. + let sorted_vec = heap.into_sorted_vec(); + assert!(sorted_vec.is_sorted(), "{:?}", sorted_vec); +} + +#[test] fn test_peek_mut_pop() { let data = vec![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1]; let mut heap = BinaryHeap::from(data); diff --git a/library/alloc/src/lib.rs b/library/alloc/src/lib.rs index 4e812529c2c..afc3a3dc6a8 100644 --- a/library/alloc/src/lib.rs +++ b/library/alloc/src/lib.rs @@ -125,6 +125,7 @@ #![feature(hasher_prefixfree_extras)] #![feature(inline_const)] #![feature(inplace_iteration)] +#![cfg_attr(test, feature(is_sorted))] #![feature(iter_advance_by)] #![feature(iter_next_chunk)] #![feature(iter_repeat_n)] |
