Skip to content

Commit 4fe167f

Browse files
committed
Add test of leaking a binary_heap PeekMut
1 parent b8f9cb3 commit 4fe167f

File tree

2 files changed

+20
-0
lines changed

2 files changed

+20
-0
lines changed

library/alloc/src/collections/binary_heap/tests.rs

+19
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use super::*;
22
use crate::boxed::Box;
33
use crate::testing::crash_test::{CrashTestDummy, Panic};
4+
use core::mem;
45
use std::iter::TrustedLen;
56
use std::panic::{catch_unwind, AssertUnwindSafe};
67

@@ -146,6 +147,24 @@ fn test_peek_mut() {
146147
assert_eq!(heap.peek(), Some(&9));
147148
}
148149

150+
#[test]
151+
fn test_peek_mut_leek() {
152+
let data = vec![4, 2, 7];
153+
let mut heap = BinaryHeap::from(data);
154+
let mut max = heap.peek_mut().unwrap();
155+
*max = -1;
156+
157+
// The PeekMut object's Drop impl would have been responsible for moving the
158+
// -1 out of the max position of the BinaryHeap, but we don't run it.
159+
mem::forget(max);
160+
161+
// Absent some mitigation like leak amplification, the -1 would incorrectly
162+
// end up in the last position of the returned Vec, with the rest of the
163+
// heap's original contents in front of it in sorted order.
164+
let sorted_vec = heap.into_sorted_vec();
165+
assert!(sorted_vec.is_sorted(), "{:?}", sorted_vec);
166+
}
167+
149168
#[test]
150169
fn test_peek_mut_pop() {
151170
let data = vec![2, 4, 6, 2, 1, 8, 10, 3, 5, 7, 0, 9, 1];

library/alloc/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@
125125
#![feature(hasher_prefixfree_extras)]
126126
#![feature(inline_const)]
127127
#![feature(inplace_iteration)]
128+
#![cfg_attr(test, feature(is_sorted))]
128129
#![feature(iter_advance_by)]
129130
#![feature(iter_next_chunk)]
130131
#![feature(iter_repeat_n)]

0 commit comments

Comments
 (0)