Skip to content

PeekMaxMut::drop overflows when len==1 #16

Closed
@cuviper

Description

When the heap only has one item, there's an overflow trying to find the max's parent.

This can be demonstrated with a test extension:

diff --git a/src/lib.rs b/src/lib.rs
index 6e7c31c00c83..def3301bd67d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -941,6 +941,10 @@ mod tests {
         assert_eq!(1, h.peek_max_mut().unwrap().pop());
         assert_eq!(Some(&0), h.peek_min());
         assert_eq!(Some(&0), h.peek_max());
+
+        *h.peek_max_mut().unwrap() = 1;
+        assert_eq!(Some(&1), h.peek_min());
+        assert_eq!(Some(&1), h.peek_max());
     }
 
     #[test]

Result:

$ RUST_BACKTRACE=1 cargo test --lib peek_max_mut
    Finished test [unoptimized + debuginfo] target(s) in 0.01s
     Running target/debug/deps/min_max_heap-1d5e15bd107018c8

running 1 test
test tests::peek_max_mut ... FAILED

failures:

---- tests::peek_max_mut stdout ----
thread 'tests::peek_max_mut' panicked at 'attempt to subtract with overflow', src/index.rs:19:31
stack backtrace:
   0: rust_begin_unwind
   1: core::panicking::panic_fmt
   2: core::panicking::panic
   3: <usize as min_max_heap::index::HeapIndex>::parent
             at ./src/index.rs:19:31
   4: min_max_heap::hole::Hole<T>::get_parent
             at ./src/hole.rs:82:18
   5: <min_max_heap::PeekMaxMut<T> as core::ops::drop::Drop>::drop
             at ./src/lib.rs:764:33
   6: core::ptr::drop_in_place<min_max_heap::PeekMaxMut<i32>>
             at /builddir/build/BUILD/rustc-1.51.0-src/library/core/src/ptr/mod.rs:179:1
   7: min_max_heap::tests::peek_max_mut
             at ./src/lib.rs:945:39
   8: min_max_heap::tests::peek_max_mut::{{closure}}
             at ./src/lib.rs:931:5
   9: core::ops::function::FnOnce::call_once
             at /builddir/build/BUILD/rustc-1.51.0-src/library/core/src/ops/function.rs:227:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.


failures:
    tests::peek_max_mut

test result: FAILED. 0 passed; 1 failed; 0 ignored; 0 measured; 18 filtered out; finished in 0.00s

error: test failed, to rerun pass '--lib'

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions