corrode icon indicating copy to clipboard operation
corrode copied to clipboard

Unitialized variables in translated lzo code

Open jrmuizel opened this issue 7 years ago • 1 comments

https://people.mozilla.org/~jmuizelaar/tmp/out.c translates with uninitialized variable errors. https://people.mozilla.org/~jmuizelaar/tmp/out-good.c translates fine.

error[E0381]: use of possibly uninitialized variable: `next`
  --> out.rs:46:25
   |
46 |                 state = next;
   |                         ^^^^ use of possibly uninitialized `next`

jrmuizel avatar Apr 06 '17 22:04 jrmuizel

Hmm, yes, whoops.

So when Emscripten translates irreducible control flow like this, since it's targeting JavaScript, nobody cares whether there could be a code path where a variable is used without being initialized unless at runtime that code path actually executes on some input. Rust, of course, is more strict and the compiler insists on proving this statically.

In this example, it's pretty easy to prove the code is safe if you look at the values that _currentBlock can take on. Unfortunately the Rust compiler is not quite that clever, and it probably shouldn't be; that's getting into SMT-solver territory.

I'm not sure what, if anything, Corrode should do about this. The user can work around the problem; in this case I'd probably suggest just initializing the variable to a dummy value rather than duplicating code, but maybe there's a smaller amount of code duplication that avoids irreducible control flow.

Perhaps we should detect this case (I think that's probably straightforward) and add an initializer of std::mem::uninitialized()?

jameysharp avatar Apr 10 '17 18:04 jameysharp