From 16ca80a9ad38bfbcfba667c64e610bdf6aecd4ab Mon Sep 17 00:00:00 2001 From: Fabian Fichter Date: Sat, 29 Feb 2020 23:46:01 +0100 Subject: [PATCH] Fix race condition in holding command Prevent race conditions in bughouse by updating the holdings with the captured piece instead of completely overriding them, given that the GUI provides this optional information. Closes #73. --- src/xboard.cpp | 15 +++++++++++++-- 1 files changed, 13 insertions(+), 2 deletions(-) diff --git a/src/xboard.cpp b/src/xboard.cpp index 69e7252..80bd7df 100644 --- a/src/xboard.cpp +++ b/src/xboard.cpp @@ -259,8 +259,19 @@ void StateMachine::process_command(Position& pos, std::string token, std::istrin if ( std::getline(is, token, '[') && std::getline(is, white_holdings, ']') && std::getline(is, token, '[') && std::getline(is, black_holdings, ']')) { - std::transform(black_holdings.begin(), black_holdings.end(), black_holdings.begin(), ::tolower); - std::string fen = pos.fen(false, false, white_holdings + black_holdings); + std::string fen; + char color, pieceType; + // Use the obtained holding if available to avoid race conditions + if (is >> color && is >> pieceType) + { + fen = pos.fen(); + fen.insert(fen.find(']'), 1, toupper(color) == 'B' ? tolower(pieceType) : toupper(pieceType)); + } + else + { + std::transform(black_holdings.begin(), black_holdings.end(), black_holdings.begin(), ::tolower); + fen = pos.fen(false, false, white_holdings + black_holdings); + } setboard(pos, states, fen); } // restart search -- 1.7.0.4