From a5561e95ed4e436d3d7ec097f00f0481c4364564 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Thu, 18 Jan 2024 22:31:56 +0100 Subject: fix: prevent stack overflow in reconstruct_pv --- src/grossmeister/search.rs | 29 ++++++++++++++++------------- 1 file changed, 16 insertions(+), 13 deletions(-) (limited to 'src') diff --git a/src/grossmeister/search.rs b/src/grossmeister/search.rs index c195527..a5696d6 100644 --- a/src/grossmeister/search.rs +++ b/src/grossmeister/search.rs @@ -267,24 +267,27 @@ impl Grossmeister { alpha } - fn reconstruct_pv(&mut self) -> Vec { - let mut pv = Vec::new(); + fn reconstruct_pv(&mut self, depth: u8) -> Vec { + let mut pv = Vec::with_capacity(depth as usize); - if let Some(transposition) = self.transposition() { - if let Some(mov) = transposition.mov { - let ep_target_before = self.board.ep_target; - let castling_rights_before = self.board.castling_rights; - let hash_before = self.board.hash; - let captured = self.board.make_move(mov); + if depth > 0 { + if let Some(transposition) = self.transposition() { + if let Some(mov) = transposition.mov { + let ep_target_before = self.board.ep_target; + let castling_rights_before = self.board.castling_rights; + let hash_before = self.board.hash; + let captured = self.board.make_move(mov); - pv.push(mov); - let mut subtree_pv = self.reconstruct_pv(); - pv.append(&mut subtree_pv); + pv.push(mov); + let mut subtree_pv = self.reconstruct_pv(depth - 1); + pv.append(&mut subtree_pv); - self.board.unmake_move(mov, captured, ep_target_before, castling_rights_before, hash_before); + self.board.unmake_move(mov, captured, ep_target_before, castling_rights_before, hash_before); + } } } + pv } @@ -331,7 +334,7 @@ impl Grossmeister { } best_score = score; - pv = self.reconstruct_pv(); + pv = self.reconstruct_pv(depth); // Print UCI info print!("info depth {} score ", depth); -- cgit v1.2.3