Skip to content
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

Using "mem::unitialized()" can lead to "Attempted to instantiate uninhabited type" #139

Closed
tomaka opened this issue Feb 1, 2019 · 2 comments · Fixed by #170
Closed

Using "mem::unitialized()" can lead to "Attempted to instantiate uninhabited type" #139

tomaka opened this issue Feb 1, 2019 · 2 comments · Fixed by #170

Comments

@tomaka
Copy link
Contributor

tomaka commented Feb 1, 2019

The following code panics:

fn main() {
    enum Void {}
    let sv = smallvec::SmallVec::<[Void; 8]>::new();
}

With:

thread 'main' panicked at 'Attempted to instantiate uninhabited type [main::Void; 8]', /rustc/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/libcore/mem.rs:630:5
stack backtrace:
   0: std::sys::unix::backtrace::tracing::imp::unwind_backtrace
             at src/libstd/sys/unix/backtrace/tracing/gcc_s.rs:39
   1: std::sys_common::backtrace::_print
             at src/libstd/sys_common/backtrace.rs:70
   2: std::panicking::default_hook::{{closure}}
             at src/libstd/sys_common/backtrace.rs:58
             at src/libstd/panicking.rs:200
   3: std::panicking::default_hook
             at src/libstd/panicking.rs:215
   4: std::panicking::rust_panic_with_hook
             at src/libstd/panicking.rs:478
   5: std::panicking::continue_panic_fmt
             at src/libstd/panicking.rs:385
   6: rust_begin_unwind
             at src/libstd/panicking.rs:312
   7: core::panicking::panic_fmt
             at src/libcore/panicking.rs:85
   8: core::panicking::panic
             at src/libcore/panicking.rs:49
   9: core::mem::uninitialized
             at /rustc/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/libcore/mem.rs:630
  10: <smallvec::SmallVec<A>>::new
             at /home/pierre/.cargo/registry/src/github.com-1ecc6299db9ec823/smallvec-0.6.8/lib.rs:402
  11: playpen::main
             at src/main.rs:3
  12: std::rt::lang_start::{{closure}}
             at /rustc/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/libstd/rt.rs:64
  13: std::panicking::try::do_call
             at src/libstd/rt.rs:49
             at src/libstd/panicking.rs:297
  14: __rust_maybe_catch_panic
             at src/libpanic_unwind/lib.rs:92
  15: std::rt::lang_start_internal
             at src/libstd/panicking.rs:276
             at src/libstd/panic.rs:388
             at src/libstd/rt.rs:48
  16: std::rt::lang_start
             at /rustc/ceb2512144d1fc26330e85fb9d41c22ba1866259/src/libstd/rt.rs:64
  17: main
  18: __libc_start_main
  19: _start

The culprit is here:

data: SmallVecData::from_inline(mem::uninitialized()),

"Why would someone create a SmallVec of Void?" you may ask.
In my situation, the type (Void here) is a template parameter of one of my structs.
When the user uses a Void, the SmallVec will of course always remain empty. However creating an empty SmallVec shouldn't itself lead to a panic.

@nox
Copy link
Contributor

nox commented Feb 1, 2019

Relevant (IIRC): #126

@RalfJung
Copy link
Contributor

RalfJung commented May 5, 2019

This also shows that e.g. smallvec::SmallVec::<[bool; 8]>::new() causes UB -- it creates a bool that is not properly initialized.

Once rust-lang/rust#60445 lands, the ManuallyDrop here should be replaced by MaybeUninit. That should help with these cases.

bors-servo pushed a commit that referenced this issue Oct 21, 2019
Add a test for #139.

Works now on trunk.

Closes #139
dpc pushed a commit to dpc/rust-smallvec that referenced this issue Oct 30, 2019
Works now on trunk.

Closes servo#139
mbrubeck pushed a commit to mbrubeck/rust-smallvec that referenced this issue Nov 4, 2019
Works now on trunk.

Closes servo#139
mbrubeck pushed a commit to mbrubeck/rust-smallvec that referenced this issue Nov 4, 2019
Works now on trunk.

Closes servo#139
mbrubeck pushed a commit to mbrubeck/rust-smallvec that referenced this issue Nov 4, 2019
Works now on trunk.

Closes servo#139
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants