From fd4ff7fd905a0e555380c31b202f260d13fba533 Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 16 Aug 2026 12:40:07 +0200 Subject: fix(uci): validate moves before accepting them --- src/uci.cpp | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'src/uci.cpp') diff --git a/src/uci.cpp b/src/uci.cpp index b78ffea..b98e6ce 100644 --- a/src/uci.cpp +++ b/src/uci.cpp @@ -32,7 +32,24 @@ uint16_t UciToMove(const std::string &uci) { std::cout << "expected valid uci string\n"; exit(1); } - // TODO: validate before using + bool isValid = true; + if (uci[0] < 'a' || uci[0] > 'h') { + isValid = false; + } + if (uci[1] < '1' || uci[1] > '8') { + isValid = false; + } + if (uci[2] < 'a' || uci[2] > 'h') { + isValid = false; + } + if (uci[3] < '1' || uci[3] > '8') { + isValid = false; + } + if (!isValid) { + std::cout << "Move is not valid: " << uci << "\n"; + exit(1); + return 0; + } from.file = static_cast(uci[0] - 'a'); from.rank = static_cast(uci[1] - '1'); @@ -53,8 +70,7 @@ uint16_t UciToMove(const std::string &uci) { promotion = BISHOP; break; default: - assert(false && "Unexpected promotion type"); - std::cout << "Unexpected promotion type"; + std::cout << "Move is not valid: " << uci << "\n"; exit(1); } } -- cgit v1.2.3