chalharu / rust-compression

Compression libraries implemented by pure Rust.
Mozilla Public License 2.0
34 stars 6 forks source link

Panic on dev mode due to UB #35

Open Polish-Civil opened 6 months ago

Polish-Civil commented 6 months ago

https://github.com/chalharu/rust-compression/blob/62afc98d69ff835fa70deb58feb208833fbf1764/src/cbuffer.rs#L39

get_unchecked(0) will panic in dev build, if that slice is empty.

thread 'main' panicked at library/core/src/panicking.rs:215:5:
unsafe precondition(s) violated: slice::get_unchecked requires that the index is within the slice
stack backtrace:
   0: rust_begin_unwind
             at /rustc/becebb3158149a115cad8a402612e25436a7e37b/library/std/src/panicking.rs:645:5
   1: core::panicking::panic_nounwind_fmt::runtime
             at /rustc/becebb3158149a115cad8a402612e25436a7e37b/library/core/src/panicking.rs:110:18
   2: core::panicking::panic_nounwind_fmt
             at /rustc/becebb3158149a115cad8a402612e25436a7e37b/library/core/src/panicking.rs:120:5
   3: core::panicking::panic_nounwind
             at /rustc/becebb3158149a115cad8a402612e25436a7e37b/library/core/src/panicking.rs:215:5
   4: <usize as core::slice::index::SliceIndex<[T]>>::get_unchecked::precondition_check
             at /rustc/becebb3158149a115cad8a402612e25436a7e37b/library/core/src/ub_checks.rs:66:21
   5: <usize as core::slice::index::SliceIndex<[T]>>::get_unchecked
             at /rustc/becebb3158149a115cad8a402612e25436a7e37b/library/core/src/slice/index.rs:218:9
   6: core::slice::<impl [T]>::get_unchecked
             at /rustc/becebb3158149a115cad8a402612e25436a7e37b/library/core/src/slice/mod.rs:686:20
   7: compression::cbuffer::CircularBuffer<T>::append
             at ./third_party/rust-compression/src/cbuffer.rs:39:25
   8: <compression::bitio::reader::BitReader<D> as compression::bitio::reader::BitRead>::peek_bits
             at ./third_party/rust-compression/src/bitio/reader.rs:95:17
   9: compression::bzip2::decoder::BZip2DecoderBase::init_block

I think instead of that using as_ptr should be fine.

lbirkert commented 2 months ago

same issue here. Cannot really use the library in its current state