-2

Here's the error I'm getting:

error[E0381]: use of possibly uninitialized variable: `mbinfo.flags`
   --> kernel/src/loader/mod.rs:256:20
    |
256 |     mbinfo.flags = mbinfo.flags | multiboot::MULTIBOOT_INFO_CMDLINE;
    |                    ^^^^^^^^^^^^ use of possibly uninitialized `mbinfo.flags`

and this is the code:

let mut mbinfo: multiboot::multiboot_info;
mbinfo.flags = 0 as u32;
mbinfo.flags = mbinfo.flags | multiboot::MULTIBOOT_INFO_CMDLINE

Even though I am explicitly initializing it, I get the error. I've tried making the struct derive Default, but the problem is that the struct contains unions, and when I try to derive Default, I get

error: this trait cannot be derived for unions 

Any easy way out? Thanks.

The multiboot module was auto generated by bindgen from a header file.

MWE on Rust Playground, code is at the end:

https://play.rust-lang.org/?version=stable&mode=debug&edition=2018&gist=464b7fb21fc75a54618b14619076d152

hfingler
  • 1,931
  • 4
  • 29
  • 36
  • 1
    You only init `flags`, not `mbinfo`. Why not construct a `multiboot_info` properly ? Please provide a [mcve], this question need it. – Stargateur Apr 12 '19 at 22:20
  • Because it's generated by bindgen from a large linux header file, reimplementing it would be a waste of effort. Added MWE to question. – hfingler Apr 12 '19 at 23:02
  • The word **Minimal** in [MCVE] is just as important as the other words. The fact that your code cannot even be included on Stack Overflow is a really good sign that it's not **minimal**. – Shepmaster Apr 13 '19 at 00:25
  • Your playground code does not produce the error you are asking about — not only is it not *minimal*, it's not **verifiable**. – Shepmaster Apr 13 '19 at 00:27
  • I believe your question is answered by the answers of [Initializing sigset_t in Rust](https://stackoverflow.com/q/34377051/155423); [Why does the compiler warn about an uninitialized variable even though I've assigned each field of that variable?](https://stackoverflow.com/q/46578050/155423); [Is it possible to mark a potentially uninitialized variable as good without using unsafe or panicking?](https://stackoverflow.com/q/45371062/155423). If you disagree, please **[edit]** your question to explain the differences. Otherwise, we can mark this question as already answered. – Shepmaster Apr 13 '19 at 00:29
  • 2
    @Shepmaster first one probably fixes it and it doesn't show on my google search for the error, thanks. – hfingler Apr 13 '19 at 08:35

1 Answers1

2
error[E0381]: assign to part of possibly uninitialized variable: `mbinfo`
    --> src/lib.rs:1178:4
     |
1178 |    mbinfo.flags = 0;
     |    ^^^^^^^^^^^^^^^^ use of possibly uninitialized `mbinfo`

The compiler tells you the exact problem: you are trying to work with an uninitialized struct. You are trying to initialize only one field of struct, Rust doesn't allow that.

multiboot_info doesn't implement the Default trait, so you need to figure out how to create it using its API.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
magras
  • 1,709
  • 21
  • 32