Skip to content

[Clang][NFC] Add nullptr check in InitializationSequence::InitializeFrom #143067

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

Merged
merged 1 commit into from
Jun 9, 2025

Conversation

shafik
Copy link
Collaborator

@shafik shafik commented Jun 6, 2025

Static analysis flagged that Var could be nullptr but we were not checking in the branch and unconditionally dereferences the pointer.

Note, code was added by 576161c

Static analysis flagged that Var could be nullptr but we were not checking in
the branch and unconditionally dereferences the pointer.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Jun 6, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 6, 2025

@llvm/pr-subscribers-clang

Author: Shafik Yaghmour (shafik)

Changes

Static analysis flagged that Var could be nullptr but we were not checking in the branch and unconditionally dereferences the pointer.

Note, code was added by 576161c


Full diff: https://github.com/llvm/llvm-project/pull/143067.diff

1 Files Affected:

  • (modified) clang/lib/Sema/SemaInit.cpp (+1-1)
diff --git a/clang/lib/Sema/SemaInit.cpp b/clang/lib/Sema/SemaInit.cpp
index 5d870f07093ec..da56225b2f926 100644
--- a/clang/lib/Sema/SemaInit.cpp
+++ b/clang/lib/Sema/SemaInit.cpp
@@ -6620,7 +6620,7 @@ void InitializationSequence::InitializeFrom(Sema &S,
       // initializer present. However, we only do this for structure types, not
       // union types, because an unitialized field in a union is generally
       // reasonable, especially in C where unions can be used for type punning.
-      if (!Initializer && !Rec->isUnion() && !Rec->isInvalidDecl()) {
+      if (Var && !Initializer && !Rec->isUnion() && !Rec->isInvalidDecl()) {
         if (const FieldDecl *FD = getConstField(Rec)) {
           unsigned DiagID = diag::warn_default_init_const_field_unsafe;
           if (Var->getStorageDuration() == SD_Static ||

@cor3ntin
Copy link
Contributor

cor3ntin commented Jun 6, 2025

If this were a bug, I suspect it would have been reported.
Most likely, dyn_cast_or_null<VarDecl>(Entity.getDecl()); is never null, and could be replaced by cast? Did you try that?

@shafik
Copy link
Collaborator Author

shafik commented Jun 6, 2025

If this were a bug, I suspect it would have been reported. Most likely, dyn_cast_or_null<VarDecl>(Entity.getDecl()); is never null, and could be replaced by cast? Did you try that?

It could but that feel like a weak argument, I would like something stronger.

We do dyn_cast_or_null<VarDecl>(...->getDecl()) in a lot of place, maybe it is cargo culting and it is not needed but I can't really tell.

@AaronBallman
Copy link
Collaborator

If this were a bug, I suspect it would have been reported. Most likely, dyn_cast_or_null<VarDecl>(Entity.getDecl()); is never null, and could be replaced by cast? Did you try that?

getDecl() can definitely return null:

return nullptr;

Copy link
Collaborator

@AaronBallman AaronBallman left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@shafik shafik merged commit 28b753b into llvm:main Jun 9, 2025
10 checks passed
@shafik shafik deleted the initializeFrom_var_check_nullptr branch June 10, 2025 17:10
rorth pushed a commit to rorth/llvm-project that referenced this pull request Jun 11, 2025
…rom (llvm#143067)

Static analysis flagged that Var could be nullptr but we were not
checking in the branch and unconditionally dereferences the pointer.

Note, code was added by 576161c
DhruvSrivastavaX pushed a commit to DhruvSrivastavaX/lldb-for-aix that referenced this pull request Jun 12, 2025
…rom (llvm#143067)

Static analysis flagged that Var could be nullptr but we were not
checking in the branch and unconditionally dereferences the pointer.

Note, code was added by 576161c
tomtor pushed a commit to tomtor/llvm-project that referenced this pull request Jun 14, 2025
…rom (llvm#143067)

Static analysis flagged that Var could be nullptr but we were not
checking in the branch and unconditionally dereferences the pointer.

Note, code was added by 576161c
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants