Skip to content

[Clang] [NFC] Prevent null pointer dereference in Sema::InstantiateFunctionDefinition #89801

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 2 commits into from
Apr 24, 2024

Conversation

smanna12
Copy link
Contributor

@smanna12 smanna12 commented Apr 23, 2024

In the lambda function within clang::Sema::InstantiateFunctionDefinition, the return value of a function that may return null is now checked before dereferencing to avoid potential null pointer dereference issues which can lead to crashes or undefined behavior in the program.

…nctionDefinition

In the lambda function within clang::Sema::InstantiateFunctionDefinition, the return value of a function that may return null is now checked before dereferencing to avoid potential null pointer dereference issues.
@smanna12 smanna12 requested a review from tahonermann April 23, 2024 17:41
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Apr 23, 2024
@smanna12 smanna12 changed the title [Clang] [NFC] Prevent null pointer dereference in Sema::InstantiateFu… [Clang] [NFC] Prevent null pointer dereference in Sema::InstantiateFunctionDefinition Apr 23, 2024
@llvmbot
Copy link
Member

llvmbot commented Apr 23, 2024

@llvm/pr-subscribers-clang

Author: None (smanna12)

Changes

…nctionDefinition

In the lambda function within clang::Sema::InstantiateFunctionDefinition, the return value of a function that may return null is now checked before dereferencing to avoid potential null pointer dereference issues.


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

1 Files Affected:

  • (modified) clang/lib/Sema/SemaTemplateInstantiateDecl.cpp (+2)
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 787a485e0b2f8c..2490ec4ca614a8 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -5183,7 +5183,9 @@ void Sema::InstantiateFunctionDefinition(SourceLocation PointOfInstantiation,
     Function->setTypeSourceInfo(NewSI);
 
     ParmVarDecl *Parm = Function->getParamDecl(0);
+    assert(Parm && "First parameter not found in function declaration.");
     TypeSourceInfo *NewParmSI = IR.TransformType(Parm->getTypeSourceInfo());
+    assert(NewParmSI && "Type transformation failed.");
     Parm->setType(NewParmSI->getType());
     Parm->setTypeSourceInfo(NewParmSI);
   };

Copy link
Member

@Sirraide Sirraide left a comment

Choose a reason for hiding this comment

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

A pr that just adds assertions and nothing else seems like a very weird thing to do. Moreover, a few lines above in the same function, we return if this isn’t a copy/move ctor/assignment operator, which always has at least one parameter; I don’t think it is possible for Parm to be nullptr here. Do you have any code examples that would trigger this asertion here?

@Sirraide
Copy link
Member

Sirraide commented Apr 23, 2024

And moreover, getParamDecl() already asserts that the index is in bounds.

Thanks @Sirraide for reviews and the pointer. Yes, it is not possible for Param to be nullptr. I have removed that assert

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.

Looks good to me, thanks @smanna12!

@smanna12
Copy link
Contributor Author

Thank you @tahonermann for reviews!

@smanna12 smanna12 merged commit e58dcf1 into llvm:main Apr 24, 2024
@smanna12 smanna12 deleted the Static_Bug branch April 24, 2024 13:43
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