Skip to content

[Sema] Check nullness of captured type before use #72230

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

Closed
wants to merge 1 commit into from

Conversation

ecnelises
Copy link
Member

@ecnelises ecnelises commented Nov 14, 2023

No description provided.

@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Nov 14, 2023
@llvmbot
Copy link
Member

llvmbot commented Nov 14, 2023

@llvm/pr-subscribers-clang

Author: Qiu Chaofan (ecnelises)

Changes

Fixes #72198


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

2 Files Affected:

  • (modified) clang/lib/Sema/SemaExpr.cpp (+2)
  • (modified) clang/test/SemaCXX/lambda-invalid-capture.cpp (+6)
diff --git a/clang/lib/Sema/SemaExpr.cpp b/clang/lib/Sema/SemaExpr.cpp
index 5b0c4439fd1710c..cb7383ea8790f67 100644
--- a/clang/lib/Sema/SemaExpr.cpp
+++ b/clang/lib/Sema/SemaExpr.cpp
@@ -19773,6 +19773,8 @@ bool Sema::tryCaptureVariable(
   // declcontext can either capture the variable or have already captured
   // the variable.
   CaptureType = Var->getType();
+  if (CaptureType.isNull())
+    return true;
   DeclRefType = CaptureType.getNonReferenceType();
   bool Nested = false;
   bool Explicit = (Kind != TryCapture_Implicit);
diff --git a/clang/test/SemaCXX/lambda-invalid-capture.cpp b/clang/test/SemaCXX/lambda-invalid-capture.cpp
index 236753871d7018b..b77a844cfbbda50 100644
--- a/clang/test/SemaCXX/lambda-invalid-capture.cpp
+++ b/clang/test/SemaCXX/lambda-invalid-capture.cpp
@@ -23,3 +23,9 @@ int pr43080(int i) { // expected-note {{declared here}}
       i; // expected-error {{variable 'i' cannot be implicitly captured in a lambda with no capture-default specified}}
   }();
 }
+
+void pr72198() {
+  int [_, b] = {0, 0}; // expected-error{{decomposition declaration cannot be declared with type 'int'; declared type must be 'auto' or reference to 'auto'}} \
+                          expected-error{{excess elements in scalar initializer}}
+  [b]{}; // expected-warning{{expression result unused}}
+}

@zyn0217
Copy link
Contributor

zyn0217 commented Nov 14, 2023

Could you please explain why the CaptureType might be null at the time when exercising tryCaptureVariable? I'm worried that doing so would hide a more significant issue.

Comment on lines +28 to +30
int [_, b] = {0, 0}; // expected-error{{decomposition declaration cannot be declared with type 'int'; declared type must be 'auto' or reference to 'auto'}} \
expected-error{{excess elements in scalar initializer}}
[b]{}; // expected-warning{{expression result unused}}
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
int [_, b] = {0, 0}; // expected-error{{decomposition declaration cannot be declared with type 'int'; declared type must be 'auto' or reference to 'auto'}} \
expected-error{{excess elements in scalar initializer}}
[b]{}; // expected-warning{{expression result unused}}
int [_, b] = {0, 0}; // expected-error {{decomposition declaration cannot be declared with type 'int'; declared type must be 'auto' or reference to 'auto'}} \
expected-error {{excess elements in scalar initializer}}
[b]{}; // expected-warning {{expression result unused}}

@@ -19773,6 +19773,8 @@ bool Sema::tryCaptureVariable(
// declcontext can either capture the variable or have already captured
// the variable.
CaptureType = Var->getType();
if (CaptureType.isNull())
Copy link
Collaborator

Choose a reason for hiding this comment

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

I am not sure this is the right fix. I think by the time we are here we should have caught any errors. I made a comment in the bug report describing my alternative.

@ecnelises
Copy link
Member Author

Abandon in favor of #72428

@ecnelises ecnelises closed this Nov 16, 2023
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.

5 participants