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

<xthreads.h>: Use enum class _Thrd_result for type safety #3897

Merged
merged 2 commits into from Jul 26, 2023

Conversation

StephanTLavavej
Copy link
Member

I was briefly investigating #292 again due to a related user report, where it turned out that that specific problem was fixed by #3721. For the general problem of defending the STL against all unconstrained operator overloads, it's unclear whether we can practically solve it, or whether we should devote significant effort towards doing so (considering that it isn't a regression, and that plenty of UCRT/VCRuntime/PPLTasks headers are also affected). I note this because I don't want to encourage a bunch of follow-up PRs in this area.

However, I noticed that <xthreads.h> uses an unnamed, unscoped enumeration for its result codes, which are returned as int. This came to my attention because it leads to ints being compared to these enumerators (hence this can be hijacked by unconstrained operator overloads), but I decided to do something about it because it's a bad practice even aside from the operator overloading issue.

The most important observation is that all of these types and functions (except _Throw_C_error() in thread0.cpp, discussed below) are extern "C", so we can upgrade this unscoped enum to a scoped enum without breaking ABI or affecting the DLL's export surface, as long as we keep the underlying type the same (just int) and don't change the enumerator values (obvious). In addition to this semi-mechanical transformation, I'm shortening the comment // TRANSITION, ABI: Always returns _Thrd_success (with occasional case variation) to // TRANSITION, ABI: Always succeeds in order to avoid wrapping.

Finally, because it throws an exception, the preserved-for-bincompat _Throw_C_error() in thread0.cpp is C++ mangled, not extern "C". Therefore, it must continue to take int code, and must static_cast<_Thrd_result>(code) within.

I've verified with dumpbin /exports that the export surfaces of the release and debug DLLs are unchanged.

@StephanTLavavej StephanTLavavej added the enhancement Something can be improved label Jul 21, 2023
@StephanTLavavej StephanTLavavej requested a review from a team as a code owner July 21, 2023 21:37
@github-actions github-actions bot added this to Initial Review in Code Reviews Jul 21, 2023
@StephanTLavavej StephanTLavavej moved this from Initial Review to Final Review in Code Reviews Jul 21, 2023
stl/inc/xthreads.h Outdated Show resolved Hide resolved
@strega-nil-ms strega-nil-ms moved this from Final Review to Ready To Merge in Code Reviews Jul 24, 2023
@StephanTLavavej StephanTLavavej self-assigned this Jul 25, 2023
@StephanTLavavej
Copy link
Member Author

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

@StephanTLavavej StephanTLavavej merged commit 64a60b4 into microsoft:main Jul 26, 2023
35 checks passed
Code Reviews automation moved this from Ready To Merge to Done Jul 26, 2023
@StephanTLavavej StephanTLavavej deleted the enum-class branch July 26, 2023 19:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Something can be improved
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

None yet

3 participants