Skip to content

Commit

Permalink
libriichi/state: add more overflow checks
Browse files Browse the repository at this point in the history
  • Loading branch information
Equim-chan committed Jan 29, 2024
1 parent e906667 commit cf318a7
Show file tree
Hide file tree
Showing 9 changed files with 111 additions and 77 deletions.
2 changes: 1 addition & 1 deletion libriichi/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ fn encode_obs(c: &mut Criterion) {
"#;
let mut ps = PlayerState::new(3);
for line in log.trim().split('\n') {
ps.update(&serde_json::from_str(line).unwrap());
ps.update(&serde_json::from_str(line).unwrap()).unwrap();
}

c.bench_function("encode obs", |b| {
Expand Down
5 changes: 2 additions & 3 deletions libriichi/src/arena/board.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ impl Board {
.chain_update(key.to_le_bytes())
.chain_update([self.kyoku, self.honba])
.finalize()
.try_into()
.unwrap();
.into();
let mut rng = ChaCha12Rng::from_seed(kyoku_seed);
let mut seq = UNSHUFFLED;
seq.shuffle(&mut rng);
Expand Down Expand Up @@ -200,7 +199,7 @@ impl BoardState {
#[inline]
fn broadcast(&mut self, ev: &Event) {
for s in &mut self.player_states {
s.update(ev);
s.update(ev).expect("fatal internal bug in BoardState");
}
}

Expand Down
7 changes: 3 additions & 4 deletions libriichi/src/bin/validate_logs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,10 +233,9 @@ fn process_path(path: &Path) -> Result<()> {
_ => (),
}

states
.iter_mut()
.zip(&mut cans)
.for_each(|(s, c)| *c = s.update_with_keep_cans(ev, true));
for (s, c) in states.iter_mut().zip(&mut cans) {
*c = s.update_with_keep_cans(ev, true)?;
}
}

Ok(())
Expand Down
15 changes: 10 additions & 5 deletions libriichi/src/dataset/gameplay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl Gameplay {
// It is guaranteed that there are at least 4 events.
// tsumo/dahai -> ryukyoku/hora -> end kyoku -> end game
for wnd in events.windows(4) {
data.extend_from_event_window(&mut ctx, wnd.try_into().unwrap());
data.extend_from_event_window(&mut ctx, wnd.try_into().unwrap())?;
}

data.dones = data.at_kyoku.windows(2).map(|w| w[1] > w[0]).collect();
Expand All @@ -275,7 +275,11 @@ impl Gameplay {
Ok(data)
}

fn extend_from_event_window(&mut self, ctx: &mut LoaderContext<'_>, wnd: &[Event; 4]) {
fn extend_from_event_window(
&mut self,
ctx: &mut LoaderContext<'_>,
wnd: &[Event; 4],
) -> Result<()> {
let LoaderContext {
config,
invisibles,
Expand Down Expand Up @@ -324,13 +328,13 @@ impl Gameplay {
};

for s in opponent_states {
s.update(cur);
s.update(cur)?;
}
}

let cans = state.update(cur);
let cans = state.update(cur)?;
if !cans.can_act() {
return;
return Ok(());
}

let mut kan_select = None;
Expand Down Expand Up @@ -412,6 +416,7 @@ impl Gameplay {
self.add_entry(ctx, true, kan);
}
}
Ok(())
}

fn add_entry(&mut self, ctx: &LoaderContext<'_>, at_kan_select: bool, label: usize) {
Expand Down
2 changes: 1 addition & 1 deletion libriichi/src/mjai/bot.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl Bot {
}
};

let cans = self.state.update(&data.event);
let cans = self.state.update(&data.event)?;
if !can_act || matches!(data.can_act, Some(false)) || !cans.can_act() {
return Ok(None);
}
Expand Down
2 changes: 1 addition & 1 deletion libriichi/src/stat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,7 +477,7 @@ impl Stat {
.collect::<Result<Vec<Event>, _>>()
.context("failed to parse log")?;

match events.get(0) {
match events.first() {
Some(Event::StartGame { names, .. }) => {
let log_stat = names
.iter()
Expand Down
2 changes: 1 addition & 1 deletion libriichi/src/state/player_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ impl PlayerState {
#[pyo3(name = "update")]
pub(super) fn update_json(&mut self, mjai_json: &str) -> Result<ActionCandidate> {
let event = json::from_str(mjai_json)?;
Ok(self.update(&event))
self.update(&event)
}

/// Raises an exception if the action is not valid.
Expand Down
4 changes: 2 additions & 2 deletions libriichi/src/state/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::mem;

impl PlayerState {
fn test_update(&mut self, event: &Event) -> ActionCandidate {
let cans = self.update(event);
let cans = self.update(event).unwrap();
self.validate();
cans
}
Expand Down Expand Up @@ -671,7 +671,7 @@ fn rule_based_agari_all_last_minogashi() {
assert!(should_hora);
ps.scores = orig_scores;

ps.add_dora_indicator(t!(5m));
ps.add_dora_indicator(t!(5m)).unwrap();
let should_hora = ps.rule_based_agari();
assert!(should_hora);

Expand Down
Loading

0 comments on commit cf318a7

Please sign in to comment.