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

Deserialization in serde panics in rustc 1.48+ with some enum inner types #812

Closed
nicodemus26 opened this issue Dec 17, 2020 · 3 comments · Fixed by #813
Closed

Deserialization in serde panics in rustc 1.48+ with some enum inner types #812

nicodemus26 opened this issue Dec 17, 2020 · 3 comments · Fixed by #813

Comments

@nicodemus26
Copy link

This error presents as: attempted to leave type `nalgebra::ArrayStorage<...>` uninitialized, which is invalid

This line of code panics:

let mut out: Self::Value = unsafe { mem::uninitialized() };

This panic is mentioned in the 1.48 detailed notes as

mem::uninitialized will now panic if any inner types inside a struct or enum disallow zero-initialization.

The tracking issue for this is here: rust-lang/rust#66151

The way visit_seq is implemented looks like it should correctly initialize the whole matrix or drop the partially initialized matrix and return an Err. Can this function use a compiler version switch to use MaybeUninit::uninit().assume_init() mentioned in the std::mem::uninitialized deprecation notes?

@nicodemus26 nicodemus26 changed the title Deserialization in serde panics in rustc 1.48+ with Deserialization in serde panics in rustc 1.48+ with enum interior types Dec 17, 2020
@nicodemus26
Copy link
Author

nicodemus26 commented Dec 17, 2020

This minimal example triggers the failure:

src/main.rs

extern crate nalgebra;
extern crate serde;
extern crate serde_json;

use nalgebra::Vector2;
use serde_derive::{Serialize, Deserialize};
use serde_json::from_str;

#[derive(Serialize, Deserialize, Debug, PartialEq, Copy, Clone)]
#[serde(tag = "letter", content = "value")]
enum Stuff {
    A(f64),
    B(f64),
}

fn main() {
    let json = r#"[{"letter":"A", "value":123.4}, {"letter":"B", "value":567.8}]"#;
    
    // Boom:
    // thread 'main' panicked at 'attempted to leave type `nalgebra::ArrayStorage<Stuff, nalgebra::U2, nalgebra::U1>` uninitialized, which is invalid', /home/nicodemus/.rustup/toolchains/stable-x86_64-unknown-linux-gnu/lib/rustlib/src/rust/library/core/src/mem/mod.rs:658:9
    let parsed: Result<Vector2<Stuff>, _> = from_str(json);
    
    println!("parsed: {:?}", parsed);
}

Cargo.toml

[package]
name = "nalgebra_issue_812"
version = "0.1.0"
authors = ["Nicodemus Paradiso <nicodemus26@gmail.com>"]
edition = "2018"

[dependencies]
serde = "1.0.118"
serde_json = "1.0.60"
serde_derive = "1.0.118"

[dependencies.nalgebra]
version = "0.23.1"
features = [
    "std", "serde-serialize"
]

@nicodemus26
Copy link
Author

nicodemus26 commented Dec 17, 2020

With 1.47:

$ rustup override set 1.47                                                                  
info: using existing install for '1.47-x86_64-unknown-linux-gnu'
info: override toolchain set to '1.47-x86_64-unknown-linux-gnu'

  1.47-x86_64-unknown-linux-gnu unchanged - rustc 1.47.0 (18bf6b4f0 2020-10-07)

$ cargo run
...
   Compiling nalgebra_issue_812 v0.1.0
    Finished dev [unoptimized + debuginfo] target(s) in 39.54s
     Running `target/debug/nalgebra_issue_812`
parsed: Ok(Matrix { data: [A(123.4), B(567.8)] })

@nicodemus26 nicodemus26 changed the title Deserialization in serde panics in rustc 1.48+ with enum interior types Deserialization in serde panics in rustc 1.48+ with some enum inner types Dec 17, 2020
@sebcrozet
Copy link
Member

Thank you for noticing this. I will release the version 0.23.2 with a fix.

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.

2 participants