Skip to content

[clang] Fix missing check for nullptr in CallExpr::getUnusedResultAttr #118636

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
Jan 6, 2025

Conversation

Mick235711
Copy link
Contributor

Fixes #117975, a regression introduced by #112521 due to me forgetting to check for nullptr before dereferencing in CallExpr::getUnusedResultAttr.
A regression test has been added as per the comments on the fixed issue.

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

llvmbot commented Dec 4, 2024

@llvm/pr-subscribers-clang

Author: Yihe Li (Mick235711)

Changes

Fixes #117975, a regression introduced by #112521 due to me forgetting to check for nullptr before dereferencing in CallExpr::getUnusedResultAttr.
A regression test has been added as per the comments on the fixed issue.


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

2 Files Affected:

  • (modified) clang/lib/AST/Expr.cpp (+3-2)
  • (modified) clang/test/SemaCXX/warn-unused-result.cpp (+9)
diff --git a/clang/lib/AST/Expr.cpp b/clang/lib/AST/Expr.cpp
index a4fb4d5a1f2ec4..286f02ded27196 100644
--- a/clang/lib/AST/Expr.cpp
+++ b/clang/lib/AST/Expr.cpp
@@ -1619,8 +1619,9 @@ std::pair<const NamedDecl *, const Attr *>
 CallExpr::getUnusedResultAttr(const ASTContext &Ctx) const {
   // If the callee is marked nodiscard, return that attribute
   const Decl *D = getCalleeDecl();
-  if (const auto *A = D->getAttr<WarnUnusedResultAttr>())
-    return {nullptr, A};
+  if (D != nullptr)
+    if (const auto *A = D->getAttr<WarnUnusedResultAttr>())
+      return {nullptr, A};
 
   // If the return type is a struct, union, or enum that is marked nodiscard,
   // then return the return type attribute.
diff --git a/clang/test/SemaCXX/warn-unused-result.cpp b/clang/test/SemaCXX/warn-unused-result.cpp
index 682c500dc1d96d..5105f347db8b53 100644
--- a/clang/test/SemaCXX/warn-unused-result.cpp
+++ b/clang/test/SemaCXX/warn-unused-result.cpp
@@ -355,3 +355,12 @@ void use2() {
   (void)G{"Hello"};
 }
 } // namespace nodiscard_specialization
+
+namespace GH117975 {
+// Test for a regression for ICE in CallExpr::getUnusedResultAttr
+int f() { return 0; }
+void id_print_name() {
+  (int) // expected-warning {{expression result unused}}
+    ((int(*)())f)();
+}
+} // namespace GH117975

Copy link
Contributor

@cor3ntin cor3ntin left a comment

Choose a reason for hiding this comment

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

LGTM

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.

This was approved, do you need someone to merge this for you?

@Mick235711
Copy link
Contributor Author

This was approved, do you need someone to merge this for you?

Yes, I do; I don't really have merge permissions. Thanks a lot for the helping hand.

@Mick235711
Copy link
Contributor Author

Gentle ping @cor3ntin @Sirraide

@cor3ntin
Copy link
Contributor

cor3ntin commented Jan 6, 2025

Sorry about that, it fell in the cracks. (in the future, feel free to ping every week if no one replies)

@cor3ntin cor3ntin merged commit 3edbe36 into llvm:main Jan 6, 2025
8 checks passed
@Mick235711 Mick235711 deleted the fix-nodicard-nullptr branch January 6, 2025 12:28
paulhuggett pushed a commit to paulhuggett/llvm-project that referenced this pull request Jan 7, 2025
llvm#118636)

Fixes llvm#117975, a regression introduced by llvm#112521 due to forgetting
to check for `nullptr` before dereferencing in
`CallExpr::getUnusedResultAttr`.
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.

Clang-20 crash at clang::CallExpr::getUnusedResultAttr(clang::ASTContext const&) const
5 participants