Add SAL annotations to safecast.h helper methods#620
Merged
jonwis merged 2 commits intomicrosoft:masterfrom Feb 24, 2026
Merged
Add SAL annotations to safecast.h helper methods#620jonwis merged 2 commits intomicrosoft:masterfrom
jonwis merged 2 commits intomicrosoft:masterfrom
Conversation
dunhor
reviewed
Feb 24, 2026
| // This conversion is unsafe, therefore the two parameter version of safe_cast_nothrow must be used | ||
| template <typename NewT, typename OldT, wistd::enable_if_t<details::is_supported_unsafe_cast_v<NewT, OldT>, int> = 0> | ||
| NewT safe_cast_nothrow(const OldT /*var*/) | ||
| _Out_range_(==, _Param_(1)) NewT safe_cast_nothrow(const OldT /*var*/) |
Member
There was a problem hiding this comment.
Just out of curiosity, if this is absent, does the analyzer error still trigger? This is a static assert that should always fire (and if it doesn't, the function doesn't have a return statement and should warn). Just curious if the analyzer is smart enough to determine the correct overload.
Collaborator
Author
There was a problem hiding this comment.
This variant wasn't part of the false positive. I included the annotation for completeness. It probably isn't necessary but it also doesn't hurt to include.
dunhor
approved these changes
Feb 24, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Why is this change being made?
After adding some usage of these helpers in the OS we received a static analysis bug that was a false positive. The underlying problem is that these methods lack annotations on the result so the static analysis tooling had to assume the worst.
Briefly summarize what changed
Add SAL annotations so that static analysis understands that upon success the input and output values are unchanged.
How was this change tested?
As part of the OS. This is a port to GitHub after the fact.