From 5ba43735221105108ea0a5d8204e9a2925b6aa7e Mon Sep 17 00:00:00 2001 From: syzygy Date: Sat, 2 Sep 2017 04:14:02 +0200 Subject: [PATCH] Prevent Stockfish from exiting if DTZ table is not present During TB initialisation, Stockfish checks for the presence of WDL tables but not for the presence of DTZ tables. When attempting to probe a DTZ table, it is therefore possible that the table is not present. In that case, Stockfish should neither exit nor report an error. To verify the bug: $ ./stockfish setoption name SyzygyTable value position fen 8/8/4r3/4k3/8/1K2P3/3P4/6R1 w - - go infinite Could not mmap() /opt/tb/regular/KRPPvKR.rtbz $ (On my system, the WDL tables are in one directory and the DTZ tables in another. If they are in the same directory, it will be difficult to trigger the bug.) The fix is trivial: check the file descriptor/handle after opening the file. No functional change. --- src/syzygy/tbprobe.cpp | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/src/syzygy/tbprobe.cpp b/src/syzygy/tbprobe.cpp index 8b87c250..4aff3b2d 100644 --- a/src/syzygy/tbprobe.cpp +++ b/src/syzygy/tbprobe.cpp @@ -341,6 +341,10 @@ public: #ifndef _WIN32 struct stat statbuf; int fd = ::open(fname.c_str(), O_RDONLY); + + if (fd == -1) + return *baseAddress = nullptr, nullptr; + fstat(fd, &statbuf); *mapping = statbuf.st_size; *baseAddress = mmap(nullptr, statbuf.st_size, PROT_READ, MAP_SHARED, fd, 0); @@ -353,6 +357,10 @@ public: #else HANDLE fd = CreateFile(fname.c_str(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr); + + if (fd == INVALID_HANDLE_VALUE) + return *baseAddress = nullptr, nullptr; + DWORD size_high; DWORD size_low = GetFileSize(fd, &size_high); HANDLE mmap = CreateFileMapping(fd, nullptr, PAGE_READONLY, size_high, size_low, nullptr); @@ -380,8 +388,7 @@ public: || *data++ != *TB_MAGIC) { std::cerr << "Corrupted table in file " << fname << std::endl; unmap(*baseAddress, *mapping); - *baseAddress = nullptr; - return nullptr; + return *baseAddress = nullptr, nullptr; } return data; @@ -482,7 +489,7 @@ void HashTable::insert(const std::vector& pieces) { TBFile file(code.insert(code.find('K', 1), "v") + ".rtbw"); // KRK -> KRvK - if (!file.is_open()) + if (!file.is_open()) // Only WDL file is checked return; file.close(); -- 2.39.2