]> git.sesse.net Git - stockfish/commit
Fix undefined behavior in search.
authorStefan Geschwentner <stgeschwentner@gmail.com>
Thu, 16 Nov 2023 07:40:25 +0000 (08:40 +0100)
committerJoost VandeVondele <Joost.VandeVondele@gmail.com>
Thu, 16 Nov 2023 08:10:20 +0000 (09:10 +0100)
commit7970236e9ea64796d5c7597cb1aedde737751f07
tree3f3c095c87d6c7fc4f6395cf8bcafa4c8d3c8803
parentd89217766b748eccd08f58f35209d762d8bf0600
Fix undefined behavior in search.

We use following line to clamp the search depth in some range:
Depth d = std::clamp(newDepth - r, 1, newDepth + 1);

Through negative extension its possible that the maximum value becomes smaller than the minimum value but then the behavior is undefined (see https://en.cppreference.com/w/cpp/algorithm/clamp). So replace this line with a safe implementation.

Remark:
We have in recent master already one line where up to 3 negative extensions are possible which could trigger this undefined behavior but this can only be happen for completed depth > 24 so its not discovered by our default bench. Recent negative extension tests by @fauzi shows then this undefined behavior with wrong bench numbers.

closes https://github.com/official-stockfish/Stockfish/pull/4877

No functional change
src/search.cpp