Fix race condition in holding command
authorFabian Fichter <ianfab@users.noreply.github.com>
Sat, 29 Feb 2020 22:46:01 +0000 (23:46 +0100)
committerFabian Fichter <ianfab@users.noreply.github.com>
Sat, 29 Feb 2020 22:46:01 +0000 (23:46 +0100)
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

index 69e7252..80bd7df 100644 (file)
@@ -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