Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix undefined behavior in search. #4877

Closed
wants to merge 1 commit into from

Conversation

locutus2
Copy link
Member

@locutus2 locutus2 commented Nov 16, 2023

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 save 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.

Bench: 1334248

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 save 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.

Bench: 1334248
@Disservin
Copy link
Member

The comment is a bit long for my taste. Maybe smth like To prevent problems when the max value is less than the min value, swapped std::clamp for a safer implementation.

@vondele vondele added the to be merged Will be merged shortly label Nov 16, 2023
@vondele vondele closed this in 7970236 Nov 16, 2023
@locutus2
Copy link
Member Author

@Disservin
On contrare, the commit should explain context. It was always our wish to give more then less information in the commit.

@Disservin
Copy link
Member

I have no problem with a detailed explanation in the commit. However the comment in the code should be short and precise, not tell a story.

@naXa777 naXa777 mentioned this pull request Nov 28, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug to be merged Will be merged shortly
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants