facebookincubator / fastmod

A fast partial replacement for the codemod tool
Apache License 2.0
1.66k stars 41 forks source link

Panic "out of bounds of" #21

Closed ghuls closed 4 years ago

ghuls commented 4 years ago
$ cat b
#!/bin/bash
t () {
    echo bla;
    return $?
}

# Next command should have been: "fastmod '\$\?' '$?;' b" instead, but:

$ fastmod '\$?' '$?;' b
thread 'main' panicked at 'byte index 18446744073709551615 is out of bounds of `#!/bin/bash
t () {
    echo bla;
    return $?
}
`', src/libcore/str/mod.rs:2131:9
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

$ RUST_BACKTRACE=1 fastmod '\$?' '$?;' b
thread 'main' panicked at 'byte index 18446744073709551615 is out of bounds of `#!/bin/bash
t () {
    echo bla;
    return $?
}
`', src/libcore/str/mod.rs:2131:9
stack backtrace:
   0: backtrace::backtrace::libunwind::trace
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/libunwind.rs:88
   1: backtrace::backtrace::trace_unsynchronized
             at /cargo/registry/src/github.com-1ecc6299db9ec823/backtrace-0.3.40/src/backtrace/mod.rs:66
   2: std::sys_common::backtrace::_print_fmt
             at src/libstd/sys_common/backtrace.rs:77
   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:1052
   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:472
  11: rust_begin_unwind
             at src/libstd/panicking.rs:380
  12: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
  13: core::str::slice_error_fail
             at src/libcore/str/mod.rs:0
  14: core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::RangeTo<usize>>::index::{{closure}}
  15: fastmod::index_to_row_col
  16: fastmod::fastmod
  17: fastmod::main
  18: std::rt::lang_start::{{closure}}
  19: std::rt::lang_start_internal::{{closure}}
             at src/libstd/rt.rs:52
  20: std::panicking::try::do_call
             at src/libstd/panicking.rs:305
  21: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:86
  22: std::panicking::try
             at src/libstd/panicking.rs:281
  23: std::panic::catch_unwind
             at src/libstd/panic.rs:394
  24: std::rt::lang_start_internal
             at src/libstd/rt.rs:51
  25: main
  26: __libc_start_main
  27: <unknown>
note: Some details are omitted, run with `RUST_BACKTRACE=full` for a verbose backtrace.
swolchok commented 4 years ago

I've reproduced the issue. Turns out the problem is that this pattern generates zero length matches (compare the patterns 'a?' and ''), and we assumed that that could never happen. Working on a patch, and will add a warning up front when a pattern matches the empty string because it's going to generate weird behavior.

swolchok commented 4 years ago

Should be fixed.

ghuls commented 4 years ago

Thanks.