diff options
| author | Adam <adammegarules1@gmail.com> | 2026-08-16 12:40:07 +0200 |
|---|---|---|
| committer | Adam <adammegarules1@gmail.com> | 2026-08-16 12:40:07 +0200 |
| commit | fd4ff7fd905a0e555380c31b202f260d13fba533 (patch) | |
| tree | 06c3c48fa6d7f0aec56152c669decb342e48dbc7 | |
| parent | e17be4b503855e27f1bb3242c2e4ce79a872dc20 (diff) | |
fix(uci): validate moves before accepting them
| -rw-r--r-- | src/uci.cpp | 22 |
1 files changed, 19 insertions, 3 deletions
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<uint8_t>(uci[0] - 'a'); from.rank = static_cast<uint8_t>(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); } } |
