cowboy8625 / rusty-rain

A cross platform matrix rain made with Rust.
https://rusty-rain.xyz
Apache License 2.0
359 stars 22 forks source link

Use of random number generation causes thread panic #25

Closed richbl closed 1 month ago

richbl commented 1 year ago

Does

Running current version (0.3.6) on Ubuntu 23.04 in terminal application will cause the following thread panic message:

thread 'main' panicked at 'cannot sample empty range', /home/user/.cargo/registry/src/github.com-1ecc6299db9ec823/rand-0.8.4/src/rng.rs:134:9

To repro:

  1. Open the terminal application
  2. Run rusty-rain
  3. Resize the window such that the height approaches ~15 characters
  4. rusty-rain will fail at this point, throwing the above panic message

I initially discovered this issue when running codium and opening a terminal window from within the IDE, which triggers this issue often given the confines of the editor windows.

A quick walk of the code logic suggests that the issue is in pub fn lengths() in the gen.rs file. If the height argument passed into the function equals the hard-coded min range value of 4, then the rnd::rng.gen_range() function will fail, given that the range provided is now empty (i.e., 4..4).

There's a good discussion of this issue in the Rust project, here: https://github.com/rust-lang/rust/issues/54204.

It's possible that if bounds checks prior to random number generation calls are not made elsewhere in the codebase, similar edge case conditions could trigger this same fault.

Should

Running rusty-rain in a terminal window should not fail regardless of window dimensions.