pleco-rs / Pleco

A Rust-based re-write of the Stockfish Chess Engine

Home Page:https://crates.io/crates/pleco

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Reduce unsafe code

terrorfisch opened this issue · comments

There are mutliple uses of unsafe that can be either replaced with safe code or with an external crate. I propose using this issue to discuss these cases and then document in the code why each unsafe is fine. This makes code review much easier.

  • Transmutes to enums. Totally fine although the compiler generates the same assembly for safe match statements.
  • Custom Arc without weak counter from servo_arc. The discussion to include this in std died here. Why not use the crate? The currently published version has possible UB servo/servo#26358
  • Custom TranspositionTable that allows to trigger undefined behaviour from safe code (if I understand the code correctly):
let tt = TranspositionTable::new_num_entries(40000);
let prng = PRNG::init(932445561);
let key: u64 = prng.rand();
let (found1, entry1): (bool, &mut Entry) = tt.probe(key);
let (found2, entry2) = tt.probe(key); // second mutable reference to the same object -> UB

I did not look into the usecases yet. There are probably alternatives available in the ecosystem.