rhysd / hgrep

Grep with human-friendly search results

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Panic when eating input from `rg --vimgrep` and max context=0

lincheney opened this issue · comments

This happens especially when running with rg --vimgrep which can result in a line with more than one match will be printed more than once.
When running hgrep -C0 this will result in a panic on the duplicate line.
(Theoretically this could be considered invalid input, but I guess hgrep should handle it more gracefully than panicking)

I can reproduce this using the 0.2.6 build on github release and also compiling latest from main branch.

  1. Create a file:
abc
def
  1. Then run rg --vimgrep . file | hgrep -c0 -C0
  2. Resulting in the following panic message:
thread '<unnamed>' panicked at 'line 2 > chunk 1', /home/runner/work/hgrep/hgrep/src/chunk.rs:162:13
stack backtrace:
   0:     0x5623558a1d34 - std::backtrace_rs::backtrace::libunwind::trace::h22893a5306c091b4
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/../../backtrace/src/backtrace/libunwind.rs:93:5
   1:     0x5623558a1d34 - std::backtrace_rs::backtrace::trace_unsynchronized::h29c3bc6f9e91819d
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x5623558a1d34 - std::sys_common::backtrace::_print_fmt::he497d8a0ec903793
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:66:5
   3:     0x5623558a1d34 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::h9c2a9d2774d81873
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:45:22
   4:     0x562355725d1c - core::fmt::write::hba4337c43d992f49
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/fmt/mod.rs:1194:17
   5:     0x56235589f295 - std::io::Write::write_fmt::heb73de6e02cfabed
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/io/mod.rs:1655:15
   6:     0x5623558a37ae - std::sys_common::backtrace::_print::h63c8b24acdd8e8ce
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:48:5
   7:     0x5623558a37ae - std::sys_common::backtrace::print::h426700d6240cdcc2
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:35:9
   8:     0x5623558a37ae - std::panicking::default_hook::{{closure}}::hc9a76eed0b18f82b
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:295:22
   9:     0x5623558a34dd - std::panicking::default_hook::h2e88d02087fae196
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:314:9
  10:     0x5623558a3d55 - std::panicking::rust_panic_with_hook::habfdcc2e90f9fd4c
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:698:17
  11:     0x5623558a3c64 - std::panicking::begin_panic_handler::{{closure}}::he054b2a83a51d2cd
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:588:13
  12:     0x5623558a2264 - std::sys_common::backtrace::__rust_end_short_backtrace::ha48b94ab49b30915
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys_common/backtrace.rs:138:18
  13:     0x5623558a39cd - rust_begin_unwind
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/panicking.rs:584:5
  14:     0x562355664d83 - core::panicking::panic_fmt::h366d3a309ae17c94
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/core/src/panicking.rs:143:14
  15:     0x5623557c3a9a - <hgrep::chunk::Files<I> as core::iter::traits::iterator::Iterator>::next::h5ee4291e70bebdc3
  16:     0x56235579be59 - <rayon::iter::par_bridge::IterParallelProducer<Iter> as rayon::iter::plumbing::UnindexedProducer>::fold_with::h0dca500009f9e028
  17:     0x5623557955a2 - rayon::iter::plumbing::bridge_unindexed_producer_consumer::h223e4423a9933835
  18:     0x5623557a2ca7 - std::panicking::try::hf0836b1c957853cc
  19:     0x5623557cb66d - rayon_core::registry::in_worker::he45b25a872d14469
  20:     0x562355795507 - rayon::iter::plumbing::bridge_unindexed_producer_consumer::h223e4423a9933835
  21:     0x5623557a2ca7 - std::panicking::try::hf0836b1c957853cc
  22:     0x5623557cb66d - rayon_core::registry::in_worker::he45b25a872d14469
  23:     0x562355795507 - rayon::iter::plumbing::bridge_unindexed_producer_consumer::h223e4423a9933835
  24:     0x5623557a2ca7 - std::panicking::try::hf0836b1c957853cc
  25:     0x5623557cb66d - rayon_core::registry::in_worker::he45b25a872d14469
  26:     0x562355795507 - rayon::iter::plumbing::bridge_unindexed_producer_consumer::h223e4423a9933835
  27:     0x5623557a2ca7 - std::panicking::try::hf0836b1c957853cc
  28:     0x5623557b5895 - <core::panic::unwind_safe::AssertUnwindSafe<F> as core::ops::function::FnOnce<()>>::call_once::h0b572bc3ae82dd95
  29:     0x5623557a260f - std::panicking::try::h5cef0bae059587cf
  30:     0x5623557b8ff3 - <rayon_core::job::StackJob<L,F,R> as rayon_core::job::Job>::execute::h5b2d03e421fd52dd
  31:     0x56235566e2e5 - rayon_core::registry::WorkerThread::wait_until_cold::h8e688ed76ab0d924
  32:     0x562355806d36 - rayon_core::registry::ThreadBuilder::run::h5b1fb0c08159930b
  33:     0x5623558092d1 - std::sys_common::backtrace::__rust_begin_short_backtrace::hff0e2583cc1c2414
  34:     0x562355804cad - core::ops::function::FnOnce::call_once{{vtable.shim}}::h0d99182cc1cda66c
  35:     0x5623558a7435 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::ha99802c2c52ada61
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/alloc/src/boxed.rs:1861:9
  36:     0x5623558a7435 - <alloc::boxed::Box<F,A> as core::ops::function::FnOnce<Args>>::call_once::ha39aea1c57e28a15
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/alloc/src/boxed.rs:1861:9
  37:     0x5623558a7435 - std::sys::unix::thread::Thread::new::thread_start::h9f8e3d72b1f7662f
                               at /rustc/fe5b13d681f25ee6474be29d748c65adcd91f69e/library/std/src/sys/unix/thread.rs:108:17
  38:     0x7fec1b9f08fd - <unknown>
  39:     0x7fec1ba72a60 - <unknown>
  40:                0x0 - <unknown>

--vimgrep is not assumed. Please use -nH as described in README.

https://github.com/rhysd/hgrep#eat-grep--nh-output

This crash was caused because --vimgrep reports matches not in line wise. Here is one of example I could make to reproduce this issue:

./src/chunk.rs:78:21:pub struct Files<I: Iterator> {
./src/chunk.rs:86:9:impl<I: Iterator> Files<I> {
./src/chunk.rs:115:10:impl<'a> Iterator for Lines<'a> {
./src/chunk.rs:144:9:impl<I: Iterator<Item = Result<GrepMatch>>> Files<I> {
./src/chunk.rs:149:26:        lines: &mut impl Iterator<Item = Line<'contents>>,
./src/chunk.rs:199:9:impl<I: Iterator<Item = Result<GrepMatch>>> Iterator for Files<I> {
./src/chunk.rs:199:45:impl<I: Iterator<Item = Result<GrepMatch>>> Iterator for Files<I> {

Line 199 appears twice since the line matched twice for given query. This is not what hgrep expects.

Fixed at aa2c0a9

The fix was included in the v0.2.7 release.

https://github.com/rhysd/hgrep/releases/tag/v0.2.7