the-lean-crate / cargo-diet

A cargo-companion to become a 'lean crate' (a member of The Lean Crate Initiative)
https://github.com/the-lean-crate/criner
MIT License
156 stars 3 forks source link

format_changeset panic (out of bound) #1

Closed PicoJr closed 4 years ago

PicoJr commented 4 years ago

Hello,

cargo-diet commit: 9e37afb

I ran RUST_BACKTRACE=1 ~/clones/cargo-diet/target/debug/cargo-diet diet -n on https://github.com/greshake/i3status-rust

I modified the following file to pinpoint the panic:

───────┬──────────────────────────────────────────────────────────────────────────────────────────────────
       │ File: /home/<user>/clones/cargo-diet/src/format_changeset.rs
───────┼──────────────────────────────────────────────────────────────────────────────────────────────────
 135   │         let hunk_after_last_change = last_changed_hunk.map(|l| (l + 1).min(diffs.len()));
 136   │         print_context(
 137   │             hunk_after_last_change.map(|l| {
 138 + │                 dbg!(l);
 139 + │                 dbg!(diffs.len());
 140   │                 let skipped_lines_note = lines_of(&diffs[l])
 141   │                     .count()
 142   │                     .checked_sub(context)
 143   │                     .map(|skipped| format!("[…skipped {} lines…]", skipped));
 144   │                 (None, lines_of(&diffs[l]).take(context), skipped_lines_note)
 145   │             }),
 146   │             &mut t,
 147   │             use_color,
 148   │         )?;
───────┴──────────────────────────────────────────────────────────────────────────────────────────────────

It looks like an off by one out of bound ;-)

Backtrace:

[src/format_changeset.rs:138] l = 4
[src/format_changeset.rs:139] diffs.len() = 4
thread 'main' panicked at 'index out of bounds: the len is 4 but the index is 4', /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libcore/slice/mod.rs:2842:10
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/libunwind.rs:86
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.44/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:78
   3: <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt
             at src/libstd/sys_common/backtrace.rs:59
   4: core::fmt::write
             at src/libcore/fmt/mod.rs:1063
   5: std::io::Write::write_fmt
             at src/libstd/io/mod.rs:1426
   6: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:62
   7: std::sys_common::backtrace::print
             at src/libstd/sys_common/backtrace.rs:49
   8: std::panicking::default_hook::{{closure}}
             at src/libstd/panicking.rs:204
   9: std::panicking::default_hook
             at src/libstd/panicking.rs:224
  10: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:470
  11: rust_begin_unwind
             at src/libstd/panicking.rs:378
  12: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
  13: core::panicking::panic_bounds_check
             at src/libcore/panicking.rs:63
  14: <usize as core::slice::SliceIndex<[T]>>::index
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libcore/slice/mod.rs:2842
  15: core::slice::<impl core::ops::index::Index<I> for [T]>::index
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libcore/slice/mod.rs:2707
  16: <alloc::vec::Vec<T> as core::ops::index::Index<I>>::index
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/liballoc/vec.rs:1890
  17: cargo_diet::format_changeset::format_changeset::{{closure}}
             at /home/<user>/clones/cargo-diet/src/format_changeset.rs:140
  18: core::option::Option<T>::map
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libcore/option.rs:456
  19: cargo_diet::format_changeset::format_changeset
             at /home/<user>/clones/cargo-diet/src/format_changeset.rs:137
  20: cargo_diet::write_manifest
             at /home/<user>/clones/cargo-diet/src/lib.rs:277
  21: cargo_diet::execute
             at /home/<user>/clones/cargo-diet/src/lib.rs:373
  22: cargo_diet::main
             at src/bin/main.rs:66
  23: std::rt::lang_start::{{closure}}
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/rt.rs:67
  24: std::rt::lang_start_internal::{{closure}}
             at src/libstd/rt.rs:52
  25: std::panicking::try::do_call
             at src/libstd/panicking.rs:303
  26: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:86
  27: std::panicking::try
             at src/libstd/panicking.rs:281
  28: std::panic::catch_unwind
             at src/libstd/panic.rs:394
  29: std::rt::lang_start_internal
             at src/libstd/rt.rs:51
  30: std::rt::lang_start
             at /rustc/8d69840ab92ea7f4d323420088dd8c9775f180cd/src/libstd/rt.rs:67
  31: main
  32: __libc_start_main
  33: _start
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.

Best regards,

PicoJr

Byron commented 4 years ago

What a beautiful bug report, a pleasure to read! Thanks a lot!

The issue indeed is an off-by-one error, which Rust unfortunately won't prevent (yet) :D.