Skip to content

Commit

Permalink
Handle FENs with wrong board size (fairy-stockfish#712)
Browse files Browse the repository at this point in the history
Try to avoid crashing on FENs with wrong board size
and parse them to the extent possible.
  • Loading branch information
ianfab authored Sep 7, 2023
1 parent ceedd61 commit 25bd564
Showing 1 changed file with 11 additions and 6 deletions.
17 changes: 11 additions & 6 deletions src/position.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,7 +264,8 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,

ss >> std::noskipws;

Square sq = SQ_A1 + max_rank() * NORTH;
Rank r = max_rank();
Square sq = SQ_A1 + r * NORTH;

// 1. Piece placement
while ((ss >> token) && !isspace(token))
Expand All @@ -283,11 +284,19 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,

else if (token == '/')
{
sq += 2 * SOUTH + (FILE_MAX - max_file()) * EAST;
sq = SQ_A1 + --r * NORTH;
if (!is_ok(sq))
break;
}

// Stop before pieces in hand
else if (token == '[')
break;

// Ignore pieces outside the board and wait for next / or [ to return to a valid state
else if (!is_ok(sq) || file_of(sq) > max_file() || rank_of(sq) > r)
continue;

// Wall square
else if (token == '*')
{
Expand All @@ -311,10 +320,6 @@ Position& Position::set(const Variant* v, const string& fenStr, bool isChess960,
put_piece(make_piece(color_of(Piece(idx)), promoted_piece_type(type_of(Piece(idx)))), sq, true, Piece(idx));
++sq;
}

// Stop before pieces in hand
else if (token == '[')
break;
}
// Pieces in hand
if (!isspace(token))
Expand Down

0 comments on commit 25bd564

Please sign in to comment.