mirror of
https://github.com/HChaZZY/Stockfish.git
synced 2025-12-20 09:06:45 +08:00
Fix reading of book file
Bug is subtle because appears only under MSVC 32 bits in optimized version, hence was missed before. Bug is due to the fact that evaluation order of terms of a sum is undefined by the standard, so in get_int() we have: return 256 * get_int<n-1>() + bookFile.get(); And if get() is evaluated before get_int() we have a corrupted key. The patch rewrites the code in a more natural and predictable way. No functional change. Signed-off-by: Marco Costalba <mcostalba@gmail.com>
This commit is contained in:
17
src/book.cpp
17
src/book.cpp
@@ -502,6 +502,18 @@ int Book::find_entry(uint64_t key) {
|
||||
}
|
||||
|
||||
|
||||
/// Book::get_number() reads sizeof(T) chars from the file's binary byte
|
||||
/// stream and converts them in a number of type T.
|
||||
template<typename T>
|
||||
void Book::get_number(T& n) {
|
||||
|
||||
n = 0;
|
||||
|
||||
for (size_t i = 0; i < sizeof(T); i++)
|
||||
n = (n << 8) + (T)bookFile.get();
|
||||
}
|
||||
|
||||
|
||||
/// Book::read_entry() takes an integer index, and returns the BookEntry
|
||||
/// at the given index in the book file.
|
||||
|
||||
@@ -514,7 +526,10 @@ BookEntry Book::read_entry(int idx) {
|
||||
|
||||
bookFile.seekg(idx * sizeof(BookEntry), ios_base::beg);
|
||||
|
||||
*this >> e.key >> e.move >> e.count >> e.learn;
|
||||
get_number(e.key);
|
||||
get_number(e.move);
|
||||
get_number(e.count);
|
||||
get_number(e.learn);
|
||||
|
||||
if (!bookFile.good())
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user