aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2024-01-18 22:31:56 +0100
committereug-vs <eugene@eug-vs.xyz>2024-01-18 22:42:37 +0100
commita5561e95ed4e436d3d7ec097f00f0481c4364564 (patch)
treea540bc211afd32bbc923e12139f2f526abd509a4
parentc101f2e230a2ed021ce54d6278ce120ad6d850fe (diff)
downloadchessnost-a5561e95ed4e436d3d7ec097f00f0481c4364564.tar.gz
fix: prevent stack overflow in reconstruct_pv
-rw-r--r--src/grossmeister/search.rs29
1 files changed, 16 insertions, 13 deletions
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<Move> {
- let mut pv = Vec::new();
+ fn reconstruct_pv(&mut self, depth: u8) -> Vec<Move> {
+ 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);