This was flagged in https://www.viva64.com/en/b/0629/ under "Snippet No.
14" (see under #13). It looks like PVS studio flags nullptr checks where
the ptr is used inbetween creation and checking against nullptr.
Details
- Reviewers
rsmith
Diff Detail
- Repository
- rG LLVM Github Monorepo
- Build Status
Buildable 32130 Build 32129: arc lint + arc unit
Event Timeline
Note that this changes when the destructor for CXXThisScopeRAII runs. It's not clear to me why ThisScope is constructed at all.
A similar case exists and was flagged in https://www.viva64.com/en/b/0629/ under "Snippet No. 16" (see under #13), clang/lib/Sema/SemaTemplateInstantiate.cpp Sema::InstantiateClass(). I'll wait for feedback on this patch, then either roll up the additional fix into this or create it as a separate patch (or change both, abandon, etc).
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | ||
---|---|---|
557–558 | The purpose of creating this is to change the type of this in the scope of the instantiation below, so this change is incorrect. The right fix would be to change the initializer of ThisContext to handle ND being null. (I suspect that actually can't happen at the moment because we happen to only attach late-parsed attributes to NamedDecls, but I don't think there's any fundamental reason why that should be the case so it makes some sense to retain the check here.) |
clang/lib/Sema/SemaTemplateInstantiateDecl.cpp | ||
---|---|---|
557–558 |
In this case I agree. The } added on L559 should be extended to L564.
I assume PVS studio is complaining about dereferencing ND on L556, then doing what looks like a nullptr check on L558 (reminiscent of -fno-delete-null-pointer-checks). I assume dyn_cast on L554 can fail at runtime and return nullptr? Should that happen, how should we proceed? |
The purpose of creating this is to change the type of this in the scope of the instantiation below, so this change is incorrect. The right fix would be to change the initializer of ThisContext to handle ND being null. (I suspect that actually can't happen at the moment because we happen to only attach late-parsed attributes to NamedDecls, but I don't think there's any fundamental reason why that should be the case so it makes some sense to retain the check here.)