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

[Clang] Prevent null pointer dereference in Sema::​CodeCompleteQualifiedId() #90490

Merged
merged 1 commit into from
May 2, 2024

Conversation

smanna12
Copy link
Contributor

@smanna12 smanna12 commented Apr 29, 2024

The null pointer dereference issue seems happening with in the expression NNS->getAsType().

Although dyn_cast_or_null() correctly handles null pointers, it doesn’t prevent the subsequent dereferencing operation.

The fix ensures that NNS pointer is not null before calling the getAsType() method, thus preventing potential runtime errors caused by attempting to access a null pointer.

…edId()

The null pointer dereference issue seems happening with in the expression NNS->getAsType().

Although dyn_cast_or_null<TemplateTypeParmType>() correctly handles null pointers, it doesn’t prevent the subsequent dereferencing operation.

The fix ensures that NNS pointer is not null before calling the getAsType() method, thus preventing potential runtime errors caused by attempting to access a null pointer.
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 29, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 29, 2024

@llvm/pr-subscribers-clang

Author: None (smanna12)

Changes

…edId()

The null pointer dereference issue seems happening with in the expression NNS->getAsType().

Although dyn_cast_or_null<TemplateTypeParmType>() correctly handles null pointers, it doesn’t prevent the subsequent dereferencing operation.

The fix ensures that NNS pointer is not null before calling the getAsType() method, thus preventing potential runtime errors caused by attempting to access a null pointer.


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

1 Files Affected:

  • (modified) clang/lib/Sema/SemaCodeComplete.cpp (+10-8)
diff --git a/clang/lib/Sema/SemaCodeComplete.cpp b/clang/lib/Sema/SemaCodeComplete.cpp
index c335017f243eb2..3f0ab10646fe5d 100644
--- a/clang/lib/Sema/SemaCodeComplete.cpp
+++ b/clang/lib/Sema/SemaCodeComplete.cpp
@@ -6714,14 +6714,16 @@ void Sema::CodeCompleteQualifiedId(Scope *S, CXXScopeSpec &SS,
 
   // If the scope is a concept-constrained type parameter, infer nested
   // members based on the constraints.
-  if (const auto *TTPT =
-          dyn_cast_or_null<TemplateTypeParmType>(NNS->getAsType())) {
-    for (const auto &R : ConceptInfo(*TTPT, S).members()) {
-      if (R.Operator != ConceptInfo::Member::Colons)
-        continue;
-      Results.AddResult(CodeCompletionResult(
-          R.render(*this, CodeCompleter->getAllocator(),
-                   CodeCompleter->getCodeCompletionTUInfo())));
+  if (NNS) {
+    if (const auto *TTPT =
+            dyn_cast_or_null<TemplateTypeParmType>(NNS->getAsType())) {
+      for (const auto &R : ConceptInfo(*TTPT, S).members()) {
+        if (R.Operator != ConceptInfo::Member::Colons)
+          continue;
+        Results.AddResult(CodeCompletionResult(
+            R.render(*this, CodeCompleter->getAllocator(),
+                     CodeCompleter->getCodeCompletionTUInfo())));
+      }
     }
   }
 

@smanna12 smanna12 changed the title [Clang] Prevent null pointer dereference in Sema::CodeCompleteQualifi… [Clang] Prevent null pointer dereference in Sema::​CodeCompleteQualifiedId() Apr 29, 2024
Copy link
Collaborator

@shafik shafik left a comment

Choose a reason for hiding this comment

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

Looks good, I will let Tom make the final accept.

clang/lib/Sema/SemaCodeComplete.cpp Show resolved Hide resolved
@tahonermann
Copy link
Contributor

Adding Sam McCall as an additional reviewer since he originally authored this code in commit a76e68c.

Copy link
Contributor

@tahonermann tahonermann left a comment

Choose a reason for hiding this comment

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

The change looks good to me. I recommend giving @sam-mccall a day or so to comment if he wants to.

@smanna12
Copy link
Contributor Author

Thanks @tahonermann for reviews!

@smanna12
Copy link
Contributor Author

smanna12 commented May 1, 2024

@sam-mccall, could you please review/comment on the fix? Thank you

@smanna12
Copy link
Contributor Author

smanna12 commented May 2, 2024

I will followup any comments from @sam-mccall in a separate PR. Merging this PR. Thanks everyone for reviews!

@smanna12 smanna12 merged commit a2f9797 into llvm:main May 2, 2024
7 checks passed
@smanna12 smanna12 deleted the FixClangStaticAnalyzerNullPointerBugs branch May 2, 2024 13:23
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