germangb / minimp3-rs

minimp3 rust bindings + wrapper
MIT License
66 stars 22 forks source link

Unsafe precondition(s) violated: slice::get_unchecked_mut index not within slice #42

Open yuvadm opened 6 months ago

yuvadm commented 6 months ago

next_frame() has some unsafe code that dumps core, not entirely sure why this happens, the stream should be valid mp3, but either way this shouldn't happen.

$ RUST_BACKTRACE=1 cargo run
    Finished `dev` profile [unoptimized + debuginfo] target(s) in 0.04s
     Running `target/debug/foo`
thread 'main' panicked at library/core/src/panicking.rs:156:5:
unsafe precondition(s) violated: slice::get_unchecked_mut requires that the index is within the slice
stack backtrace:
   0: rust_begin_unwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:110:18
   2: core::panicking::panic_nounwind_fmt
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:123:9
   3: core::panicking::panic_nounwind
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/panicking.rs:156:5
   4: <usize as core::slice::index::SliceIndex<[T]>>::get_unchecked_mut::precondition_check
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/intrinsics.rs:2799:21
   5: <usize as core::slice::index::SliceIndex<[T]>>::get_unchecked_mut
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/slice/index.rs:252:9
   6: core::slice::<impl [T]>::get_unchecked_mut
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/slice/mod.rs:728:24
   7: slice_deque::SliceDeque<T>::extend_desugared
             at /home/yuval/.cargo/registry/src/index.crates.io-6f17d22bba15001f/slice-deque-0.3.0/src/lib.rs:1720:28
   8: <slice_deque::SliceDeque<T> as slice_deque::SpecExtend<T,I>>::spec_extend
             at /home/yuval/.cargo/registry/src/index.crates.io-6f17d22bba15001f/slice-deque-0.3.0/src/lib.rs:2614:9
   9: <slice_deque::SliceDeque<T> as slice_deque::SpecExtend<&T,I>>::spec_extend
             at /home/yuval/.cargo/registry/src/index.crates.io-6f17d22bba15001f/slice-deque-0.3.0/src/lib.rs:2702:9
  10: <slice_deque::SliceDeque<T> as core::iter::traits::collect::Extend<&T>>::extend
             at /home/yuval/.cargo/registry/src/index.crates.io-6f17d22bba15001f/slice-deque-0.3.0/src/lib.rs:2856:9
  11: minimp3::Decoder<R>::refill
             at /home/yuval/.cargo/registry/src/index.crates.io-6f17d22bba15001f/minimp3-0.5.1/src/lib.rs:199:9
  12: minimp3::Decoder<R>::next_frame
             at /home/yuval/.cargo/registry/src/index.crates.io-6f17d22bba15001f/minimp3-0.5.1/src/lib.rs:177:22
  13: foo::mp3::Mp3StreamDecoder<R>::new
             at ./src/mp3.rs:32:29
  14: foo::main
             at ./src/main.rs:8:18
  15: core::ops::function::FnOnce::call_once
             at /rustc/9b00956e56009bab2aa15d7bff10916599e3d6d6/library/core/src/ops/function.rs:250:5
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
thread caused non-unwinding panic. aborting.
Aborted (core dumped)
Cerber-Ursi commented 4 months ago

The error seems to be actually inside slice_deque implementation, judging from the backtrace. That crate seems to be unmaintained - https://github.com/gnzlbg/slice_deque/issues/94, so maybe it should be replaced?

AspadaX commented 4 months ago

I got the same issue.

metasophiea commented 4 months ago

The error seems to be actually inside slice_deque implementation, judging from the backtrace. That crate seems to be unmaintained - gnzlbg/slice_deque#94, so maybe it should be replaced?

That issue thread has someone forking slice_deque and crating slice_ring_buffer. I tried replacing slice_deque with slice_ring_buffer but it seems to run into the same error

Another user noticed this in the slice_ring_buffer repo, and created a pull request addressing the issue back in May https://github.com/LiquidityC/slice_ring_buffer/pull/10

I've sent the owner of the slice_ring_buffer repo an email about merging and publishing that change

metasophiea commented 4 months ago

The owner of the slice_ring_buffer repo has performed the required merge and publish actions to update slice_ring_buffer, so I've now made a pull request on this repository to use slice_ring_buffer https://github.com/germangb/minimp3-rs/pull/43

adierking commented 4 months ago

Thanks for contacting the maintainer! 😃