about summary refs log tree commit diff
path: root/src/liballoc/tests
diff options
context:
space:
mode:
authorStein Somers <git@steinsomers.be>2020-03-06 14:32:54 +0100
committerStein Somers <git@steinsomers.be>2020-03-06 14:50:09 +0100
commit44c97c43b5d3df5d76381f80fb8ad0042c6ccf55 (patch)
tree98abd6c59b19239937f9c0d740aa8a894ae0b3fa /src/liballoc/tests
parent865b44a3e330f3ef8be0f6edf69896c9ed957ac0 (diff)
downloadrust-44c97c43b5d3df5d76381f80fb8ad0042c6ccf55.tar.gz
rust-44c97c43b5d3df5d76381f80fb8ad0042c6ccf55.zip
Fix & test leak of some BTreeMap nodes on panic during `into_iter`
Diffstat (limited to 'src/liballoc/tests')
-rw-r--r--src/liballoc/tests/btree/map.rs26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/liballoc/tests/btree/map.rs b/src/liballoc/tests/btree/map.rs
index fd07a4d3926..d05eec19346 100644
--- a/src/liballoc/tests/btree/map.rs
+++ b/src/liballoc/tests/btree/map.rs
@@ -1021,7 +1021,7 @@ fn test_split_off_large_random_sorted() {
 }
 
 #[test]
-fn test_into_iter_drop_leak() {
+fn test_into_iter_drop_leak_1() {
     static DROPS: AtomicU32 = AtomicU32::new(0);
 
     struct D;
@@ -1045,3 +1045,27 @@ fn test_into_iter_drop_leak() {
 
     assert_eq!(DROPS.load(Ordering::SeqCst), 5);
 }
+
+#[test]
+fn test_into_iter_drop_leak_2() {
+    let size = 12; // to obtain tree with 2 levels (having edges to leaf nodes)
+    static DROPS: AtomicU32 = AtomicU32::new(0);
+    static PANIC_POINT: AtomicU32 = AtomicU32::new(0);
+
+    struct D;
+    impl Drop for D {
+        fn drop(&mut self) {
+            if DROPS.fetch_add(1, Ordering::SeqCst) == PANIC_POINT.load(Ordering::SeqCst) {
+                panic!("panic in `drop`");
+            }
+        }
+    }
+
+    for panic_point in vec![0, 1, size - 2, size - 1] {
+        DROPS.store(0, Ordering::SeqCst);
+        PANIC_POINT.store(panic_point, Ordering::SeqCst);
+        let map: BTreeMap<_, _> = (0..size).map(|i| (i, D)).collect();
+        catch_unwind(move || drop(map.into_iter())).ok();
+        assert_eq!(DROPS.load(Ordering::SeqCst), size);
+    }
+}