From 25bd564eef4cc07c736633cf57fc669f5315d9ee Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Thu, 7 Sep 2023 22:46:29 +0200 Subject: [PATCH] Handle FENs with wrong board size (#712) Try to avoid crashing on FENs with wrong board size and parse them to the extent possible. --- src/position.cpp | 17 +++++++++++------ 1 file changed, 11 insertions(+), 6 deletions(-) diff --git a/src/position.cpp b/src/position.cpp index 5d5048826..de08457e7 100644 --- a/src/position.cpp +++ b/src/position.cpp @@ -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)) @@ -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 == '*') { @@ -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))