Add test of leaking a binary_heap PeekMut

This commit is contained in:
David Tolnay 2022-12-18 09:40:23 -08:00
parent b8f9cb345a
commit 4fe167f83a
No known key found for this signature in database
GPG Key ID: F9BA143B95FF6D82
2 changed files with 20 additions and 0 deletions

View File

@ -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};
@ -146,6 +147,24 @@ fn test_peek_mut() {
assert_eq!(heap.peek(), Some(&9));
}
#[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];

View File

@ -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)]