mirror of
https://github.com/official-stockfish/Stockfish.git
synced 2026-07-22 12:47:08 +00:00
Strict FEN parsing. Exit on setting invalid position via UCI.
fixes #6663, #6664, and a million others issues raised over the years. This is similar to https://github.com/official-stockfish/Stockfish/pull/4563/changes but more conservative and errors are split between "Unsupported position" and "Invalid FEN". The FEN parser needs to be strict as a foundation for safety. It does not specify much of the semantics, so this step is fairly simple. Parts after the ep square are optional, however, since it's common, for example in EPD notation. Errors arising from positional semantics that were previously bucketed under invalid FENs are now reported as "Unsupported position". Only positions that are potentially problematic are designated as unsupported. It is NOT guided by illegality of the position, but instead by the ability of the engine to handle them correctly. This means that some checks from the previous PR were removed. std::exit is used instead of std::terminate so atexit handles will be called. Probably wise to run copilot on this or smth because I scribbled it without much thought. With these small changes and reduced, less controversial, scope I hope this PR will finally make it and we will be done with these weekly issues. I'll wait with putting it on fishtest until there's approval. closes https://github.com/official-stockfish/Stockfish/pull/6665 No functional change
This commit is contained in:
committed by
Disservin
parent
415f2ef11c
commit
e38f5b2f53
+16
-1
@@ -22,6 +22,7 @@
|
||||
#include <cctype>
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
#include <cstdlib>
|
||||
#include <iterator>
|
||||
#include <optional>
|
||||
#include <sstream>
|
||||
@@ -466,6 +467,8 @@ std::uint64_t UCIEngine::perft(const Search::LimitsType& limits) {
|
||||
}
|
||||
|
||||
void UCIEngine::position(std::istringstream& is) {
|
||||
const std::string fullCommand = is.str();
|
||||
|
||||
std::string token, fen;
|
||||
|
||||
is >> token;
|
||||
@@ -488,7 +491,11 @@ void UCIEngine::position(std::istringstream& is) {
|
||||
moves.push_back(token);
|
||||
}
|
||||
|
||||
engine.set_position(fen, moves);
|
||||
auto err = engine.set_position(fen, moves);
|
||||
if (err.has_value())
|
||||
{
|
||||
terminate_on_critical_error(fullCommand, err->what());
|
||||
}
|
||||
}
|
||||
|
||||
namespace {
|
||||
@@ -658,4 +665,12 @@ void UCIEngine::on_bestmove(std::string_view bestmove, std::string_view ponder)
|
||||
std::cout << sync_endl;
|
||||
}
|
||||
|
||||
void UCIEngine::terminate_on_critical_error(const std::string& fullCommand,
|
||||
const std::string& message) {
|
||||
sync_cout << "info string CRITICAL ERROR: Command `" << fullCommand
|
||||
<< "` failed. Reason: " << message << '\n'
|
||||
<< sync_endl;
|
||||
std::exit(1);
|
||||
}
|
||||
|
||||
} // namespace Stockfish
|
||||
|
||||
Reference in New Issue
Block a user