Skip to content

Commit f9047e7

Browse files
committed
Fixed #35 - fixed crash in tactical benchmark.
The tactical benchmark could crash due to accessing invalid chess move in ChessBoard::NumberOfRepetitions(). That function tries to repeat making moves on the board, but the "moves" are not always valid chess moves. NumberOfRepetitions() already had a safety check to make sure the board was not edited before doing anything. The safety check did not work in the case of the benchmark because the benchmark didn't set an initial FEN (Forsyth Edwards Notation) string. A non-null FEN string is required to indicate that a position has been edited. The fix was to make sure the benchmark initializes the FEN string for the starting position before doing each min-max search.
1 parent 6f5a19d commit f9047e7

File tree

3 files changed

+21
-9
lines changed

3 files changed

+21
-9
lines changed

src/board.cpp

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1487,15 +1487,21 @@ void ChessBoard::SaveSpecialMove ( Move special )
14871487

14881488
initialPlyNumber = ++ply_number; // Remember the spot right after the last pseudo-move was saved
14891489

1490-
char fen [200];
1491-
if (GetForsythEdwardsNotation (fen, sizeof(fen)))
1492-
{
1493-
ReplaceString (initialFen, fen);
1494-
}
1490+
MarkInitialPosition();
1491+
}
1492+
1493+
1494+
void ChessBoard::MarkInitialPosition()
1495+
{
1496+
// Record the current position as the initial position for this game.
1497+
// This has the side-effect of indicating that the board has been edited.
1498+
// This is needed to prevent crashes in functions like NumberOfRepetitions().
1499+
1500+
char fen[200];
1501+
if (GetForsythEdwardsNotation(fen, sizeof(fen)))
1502+
ReplaceString(initialFen, fen);
14951503
else
1496-
{
1497-
ChessFatal ("ChessBoard::SaveSpecialMove: Unable to save initial FEN");
1498-
}
1504+
ChessFatal("ChessBoard::MarkInitialPosition: Unable to save initial FEN");
14991505
}
15001506

15011507

src/chess.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1278,7 +1278,8 @@ class ChessBoard
12781278
bool IsDefiniteDraw ( int *numReps = 0 ); // Does NOT find stalemate!
12791279
int NumberOfRepetitions();
12801280

1281-
void SaveSpecialMove ( Move );
1281+
void SaveSpecialMove ( Move );
1282+
void MarkInitialPosition();
12821283

12831284
void MakeWhiteMove (
12841285
Move &move, // can have side-effects!

src/uiwin32.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,7 @@ double ChessUI_win32_gui::runTacticalBenchmark()
21582158
tboard.SetSquareContents ( BROOK, 0, 7 );
21592159
tboard.SetSquareContents ( BQUEEN, 3, 7 );
21602160
tboard.SetSquareContents ( BROOK, 4, 7 );
2161+
tboard.MarkInitialPosition();
21612162
benchmarkExpectedMove.source = 0;
21622163
benchmarkExpectedMove.dest = 0;
21632164
TheBoardDisplayBuffer.update (tboard);
@@ -2180,6 +2181,7 @@ double ChessUI_win32_gui::runTacticalBenchmark()
21802181
tboard.SetSquareContents ( BPAWN, 0, 6 );
21812182
tboard.SetSquareContents ( BPAWN, 1, 6 );
21822183
tboard.SetSquareContents ( BBISHOP, 3, 6 );
2184+
tboard.MarkInitialPosition();
21832185
TheBoardDisplayBuffer.update (tboard);
21842186
TheBoardDisplayBuffer.freshenBoard();
21852187
benchmarkExpectedMove.source = 0;
@@ -2210,6 +2212,7 @@ double ChessUI_win32_gui::runTacticalBenchmark()
22102212
tboard.SetSquareContents ( BPAWN, 7, 5 );
22112213
tboard.SetSquareContents ( BKNIGHT, 4, 6 );
22122214
tboard.SetSquareContents ( BROOK, 5, 7 );
2215+
tboard.MarkInitialPosition();
22132216
move.source = OFFSET(9,3);
22142217
move.dest = OFFSET(8,3);
22152218
tboard.MakeMove ( move, unmove );
@@ -2248,6 +2251,7 @@ double ChessUI_win32_gui::runTacticalBenchmark()
22482251
tboard.SetSquareContents ( BKING, 0, 7 );
22492252
tboard.SetSquareContents ( BROOK, 2, 7 );
22502253
tboard.SetSquareContents ( BROOK, 4, 7 );
2254+
tboard.MarkInitialPosition();
22512255
benchmarkExpectedMove.source = 0;
22522256
benchmarkExpectedMove.dest = 0;
22532257
benchmarkPlayer->SetSearchDepth (4);
@@ -2292,6 +2296,7 @@ double ChessUI_win32_gui::runTacticalBenchmark()
22922296
tboard.SetSquareContents ( BROOK, 4, 7 );
22932297
tboard.SetSquareContents ( BQUEEN, 3, 7 );
22942298
tboard.SetSquareContents ( BROOK, 0, 7 );
2299+
tboard.MarkInitialPosition();
22952300
benchmarkExpectedMove.source = 0;
22962301
benchmarkExpectedMove.dest = 0;
22972302
benchmarkPlayer->SetSearchDepth (4);

0 commit comments

Comments
 (0)