KEMBAR78
Fix UB in `vector<bool>` by adding missing special case by AlexGuteniev · Pull Request #5726 · microsoft/STL · GitHub
Skip to content

Conversation

@AlexGuteniev
Copy link
Contributor

Fixes #5720.

_IsSingleBlockDest has special case when that single block touches edge, and _DestEnd is greater that _Dest, and _DestEnd._Myoff == 0. This is handled elsewhere:

const bool _IsSingleBlockDest = _VbDest == _DestEnd._Myptr - (_DestEnd._Myoff == 0 ? 1 : 0);

const auto _DestMask = _FirstDestMask | (_DestEnd._Myoff == 0 ? 0 : _LastDestMask);

const auto _DestMask = _FirstDestMask | (_DestEnd._Myoff == 0 ? 0 : _LastDestMask);

Now we need to add missing handling in the UB causing line. If _DestEnd._Myoff == 0, we have the only case when the subtraction result is negative. We actually need to subtract from full 32 bits in this case.

The UB didn't cause runtime behavior difference, as the shift instruction just ignores high bits.

@AlexGuteniev AlexGuteniev requested a review from a team as a code owner September 16, 2025 19:21
@github-project-automation github-project-automation bot moved this to Initial Review in STL Code Reviews Sep 16, 2025
@AlexGuteniev AlexGuteniev changed the title Fix UB in vector<bool> by adding missing special case Fix UB in vector<bool> by adding missing special case Sep 16, 2025
@frederick-vs-ja
Copy link
Contributor

Looks like that the affect statements can be executed in constant execution. Can we add a constexpr-friendly test case?

@AlexGuteniev
Copy link
Contributor Author

Looks like that the affect statements can be executed in constant execution. Can we add a constexpr-friendly test case?

Adding constexpr coverage sounds like a good idea.

But I'm against a test case for this specific branch. Think that we should be systematic, and make the entire coverage constexpr, except huge and random.

And this larger change looks like out of scope, I want to fix specific bug, for which there is already expected ubsan coverage.

@StephanTLavavej StephanTLavavej added the bug Something isn't working label Sep 17, 2025
@StephanTLavavej StephanTLavavej self-assigned this Sep 17, 2025
@StephanTLavavej
Copy link
Member

Thanks for the quick fix and clear explanation! 😻

@StephanTLavavej StephanTLavavej removed their assignment Sep 17, 2025
@StephanTLavavej StephanTLavavej moved this from Initial Review to Ready To Merge in STL Code Reviews Sep 17, 2025
@StephanTLavavej StephanTLavavej moved this from Ready To Merge to Merging in STL Code Reviews Sep 19, 2025
@StephanTLavavej
Copy link
Member

I'm mirroring this to the MSVC-internal repo - please notify me if any further changes are pushed.

@StephanTLavavej StephanTLavavej merged commit 3effbb0 into microsoft:main Sep 22, 2025
39 checks passed
@github-project-automation github-project-automation bot moved this from Merging to Done in STL Code Reviews Sep 22, 2025
@StephanTLavavej
Copy link
Member

Thanks for fixing this UB so quickly! 😻 🛠️ 🛡️

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

Archived in project

Development

Successfully merging this pull request may close these issues.

<vector>: The vector<bool> optimization for copy() performs a forbidden negative shift

3 participants