New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crash: "byte index 50 is out of bounds of ` static ONCE: Once = ONCE_INIT;`" #2689

Closed
little-dude opened this Issue Apr 21, 2018 · 5 comments

Comments

Projects
None yet
3 participants
@little-dude
Copy link

little-dude commented Apr 21, 2018

Hi!

Clippy crashes when I try to run it against the v3-master branch of clap.

  • rust: rustc 1.27.0-nightly (ac3c2288f 2018-04-18)
  • clippy: 0.0.195

Steps to reproduce:

git clone https://github.com/kbknapp/clap-rs/
cd clap-rs
git checkout v3-master
cargo clippy

Stacktrace:

thread 'main' panicked at 'byte index 50 is out of bounds of `                static ONCE: Once = ONCE_INIT;`', libcore/str/mod.rs:2256:9
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at libstd/sys/unix/backtrace/tracing/gcc_s.rs:49
   1: std::sys_common::backtrace::print
             at libstd/sys_common/backtrace.rs:71
             at libstd/sys_common/backtrace.rs:59
   2: std::panicking::default_hook::{{closure}}
             at libstd/panicking.rs:205
   3: std::panicking::default_hook
             at libstd/panicking.rs:221
   4: rustc::util::common::panic_hook
   5: std::panicking::rust_panic_with_hook
             at libstd/panicking.rs:461
   6: std::panicking::begin_panic_fmt
             at libstd/panicking.rs:344
   7: rust_begin_unwind
             at libstd/panicking.rs:322
   8: core::panicking::panic_fmt
             at libcore/panicking.rs:71
   9: core::str::slice_error_fail
             at libcore/str/mod.rs:0
  10: core::str::traits::<impl core::slice::SliceIndex<str> for core::ops::range::RangeTo<usize>>::index::{{closure}}
  11: core::ops::function::impls::<impl core::ops::function::FnOnce<A> for &'a mut F>::call_once
  12: <alloc::vec::Vec<T> as alloc::vec::SpecExtend<T, I>>::from_iter
  13: <rustc_errors::emitter::EmitterWriter as rustc_errors::emitter::Emitter>::emit
  14: rustc_errors::Handler::emit_db
  15: rustc_errors::diagnostic_builder::DiagnosticBuilder::emit
  16: clippy_lints::utils::span_lint_and_then
  17: <clippy_lints::eq_op::EqOp as rustc::lint::LateLintPass<'a, 'tcx>>::check_expr
  18: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  19: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_body
  20: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_nested_body
  21: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_fn
  22: rustc::hir::intravisit::walk_expr
  23: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  24: rustc::hir::intravisit::walk_expr
  25: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  26: rustc::hir::intravisit::walk_expr
  27: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  28: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_local
  29: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_block
  30: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  31: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_arm
  32: rustc::hir::intravisit::walk_expr
  33: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  34: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_block
  35: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  36: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  37: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_block
  38: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  39: rustc::hir::intravisit::walk_expr
  40: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  41: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_block
  42: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  43: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_arm
  44: rustc::hir::intravisit::walk_expr
  45: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  46: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_block
  47: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  48: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_block
  49: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_expr
  50: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_body
  51: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_nested_body
  52: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_fn
  53: rustc::hir::intravisit::walk_impl_item
  54: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_impl_item
  55: rustc::hir::intravisit::walk_impl_item_ref
  56: rustc::hir::intravisit::walk_item
  57: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_item
  58: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_mod
  59: rustc::hir::intravisit::walk_item
  60: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_item
  61: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_mod
  62: rustc::hir::intravisit::walk_item
  63: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_item
  64: <rustc::lint::context::LateContext<'a, 'tcx> as rustc::hir::intravisit::Visitor<'tcx>>::visit_mod
  65: rustc::hir::intravisit::walk_crate
  66: rustc::lint::context::check_crate
  67: rustc::ty::context::tls::enter_context
  68: <std::thread::local::LocalKey<T>>::with
  69: rustc::ty::context::TyCtxt::create_and_enter
  70: rustc_driver::driver::compile_input
  71: rustc_driver::run_compiler_impl
  72: <scoped_tls::ScopedKey<T>>::set
  73: syntax::with_globals
  74: rustc_driver::run_compiler
  75: rustc_driver::run
  76: clippy_driver::main
  77: std::rt::lang_start::{{closure}}
  78: std::panicking::try::do_call
             at libstd/rt.rs:59
             at libstd/panicking.rs:304
  79: __rust_maybe_catch_panic
             at libpanic_unwind/lib.rs:105
  80: std::rt::lang_start_internal
             at libstd/panicking.rs:283
             at libstd/panic.rs:361
             at libstd/rt.rs:58
  81: main
  82: __libc_start_main
  83: _start
query stack during panic:
end of query stack
error: Could not compile `clap`.

Caused by:
  process didn't exit successfully: `/home/corentih/.cargo/bin/clippy-driver rustc --crate-name clap src/lib.rs --crate-type lib --emit=dep-info,metadata -C codegen-units=4 -C debuginfo=2 --cfg feature="ansi_term" --cfg feature="atty" --cfg feature="color" --cfg feature="default" --cfg feature="strsim" --cfg feature="suggestions" --cfg feature="vec_map" -C metadata=3d857ea9199bf8e8 -C extra-filename=-3d857ea9199bf8e8 --out-dir /home/corentih/rust/clap-rs/target/debug/deps -C incremental=/home/corentih/rust/clap-rs/target/debug/incremental -L dependency=/home/corentih/rust/clap-rs/target/debug/deps --extern atty=/home/corentih/rust/clap-rs/target/debug/deps/libatty-bf22ac3c49701b9a.rmeta --extern textwrap=/home/corentih/rust/clap-rs/target/debug/deps/libtextwrap-336ab6e3c0deee78.rmeta --extern bitflags=/home/corentih/rust/clap-rs/target/debug/deps/libbitflags-4463c41903136498.rmeta --extern ansi_term=/home/corentih/rust/clap-rs/target/debug/deps/libansi_term-e7961f92d6024c7f.rmeta --extern vec_map=/home/corentih/rust/clap-rs/target/debug/deps/libvec_map-4f6af3620676d2fd.rmeta --extern strsim=/home/corentih/rust/clap-rs/target/debug/deps/libstrsim-d00845f87ad26640.rmeta --extern ordermap=/home/corentih/rust/clap-rs/target/debug/deps/libordermap-461231bdcb021996.rmeta --extern unicode_width=/home/corentih/rust/clap-rs/target/debug/deps/libunicode_width-524c73cc87cda6bc.rmeta` (exit code: 101)
@little-dude

This comment has been minimized.

Copy link
Author

little-dude commented Apr 22, 2018

@phansch

This comment has been minimized.

Copy link
Collaborator

phansch commented May 21, 2018

I just took a look at this, and I'm not really sure where the error is caused, yet.

byte index 50 is out of bounds of ' static ONCE: Once = ONCE_INIT;' suggests that libcore/str/mod.rs#1972 somehow hits the or_else path with end being 50, while the slice is only 46 long.

@little-dude

This comment has been minimized.

Copy link
Author

little-dude commented May 22, 2018

fwiw here is the piece of code that makes rustc crash (link to playground, but it does not crash there :()

#[cfg(not(feature = "no_cargo"))]
#[macro_export]
macro_rules! crate_authors {
    ($sep:expr) => {{
        use std::ops::Deref;
        use std::sync::{ONCE_INIT, Once};

        #[allow(missing_copy_implementations)]
        #[allow(dead_code)]
        struct CargoAuthors { __private_field: () };

        impl Deref for CargoAuthors {
            type Target = str;
    
            #[allow(unsafe_code)]
            fn deref(&self) -> &'static str {
                static ONCE: Once = ONCE_INIT;                                                     
                static mut VALUE: *const String = 0 as *const String;

                unsafe {
                    ONCE.call_once(|| {
                        let s = env!("CARGO_PKG_AUTHORS").replace(':', $sep);
                        VALUE = Box::into_raw(Box::new(s));
                    });

                    &(*VALUE)[..]
                }
            }
        }

        &*CargoAuthors { __private_field: () }
    }};
    () => {
        env!("CARGO_PKG_AUTHORS")
    };
}
@zayenz

This comment has been minimized.

Copy link
Contributor

zayenz commented May 29, 2018

Copying some information from #2812.

When trying to reduce the code, I observed that changing any code before the line complained about (even just removing unrelated empty lines) changed the span that was out of bounds, so it seems to be a more global problem of spans than just the specific code that was reported on. For example, replacing the first part with

$sep:expr) => {{
        "foo"
    }};

in the clap code gives a crash that instead references

thread 'main' panicked at 'byte index 50 is out of bounds of `/// # use clap::App;`', libcore/str/mod.rs:2125:9

which is a comment that is 13 lines below the original place that was reported as a failure.

zayenz added a commit to zayenz/rust-clippy that referenced this issue May 29, 2018

avoid op-ref in macros
Avoid running op-ref inspection in macros since the macro
may be invoked in many different types of contexts.

Solves rust-lang#2818 and incidentally avoids rust-lang#2689.
@little-dude

This comment has been minimized.

Copy link
Author

little-dude commented Jun 11, 2018

Fixed in 0.0.207.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment