Skip to content

Conversation

@rniwa
Copy link
Contributor

@rniwa rniwa commented Dec 20, 2024

Added a nullptr check.

@rniwa rniwa requested a review from t-rasmud December 20, 2024 09:33
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:static analyzer labels Dec 20, 2024
@llvmbot
Copy link
Member

llvmbot commented Dec 20, 2024

@llvm/pr-subscribers-clang

@llvm/pr-subscribers-clang-static-analyzer-1

Author: Ryosuke Niwa (rniwa)

Changes

Added a nullptr check.


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

2 Files Affected:

  • (modified) clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp (+2)
  • (added) clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures-find-lambda-crash.cpp (+16)
diff --git a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp
index da9698e327562e..a57499d52acd0c 100644
--- a/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/WebKit/UncountedLambdaCapturesChecker.cpp
@@ -155,6 +155,8 @@ class UncountedLambdaCapturesChecker
         if (!Init)
           return nullptr;
         TempExpr = dyn_cast<CXXBindTemporaryExpr>(Init->IgnoreParenCasts());
+        if (!TempExpr)
+          return nullptr;
         return dyn_cast_or_null<LambdaExpr>(TempExpr->getSubExpr());
       }
 
diff --git a/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures-find-lambda-crash.cpp b/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures-find-lambda-crash.cpp
new file mode 100644
index 00000000000000..4d9edb75b7ff36
--- /dev/null
+++ b/clang/test/Analysis/Checkers/WebKit/uncounted-lambda-captures-find-lambda-crash.cpp
@@ -0,0 +1,16 @@
+// RUN: %clang_analyze_cc1 -analyzer-checker=webkit.UncountedLambdaCapturesChecker -verify %s
+// expected-no-diagnostics
+
+struct Foo {
+  int x;
+  int y;
+  Foo(int x, int y) : x(x) , y(y) { }
+  ~Foo() { }
+};
+
+Foo bar(const Foo&);
+void foo() {
+  int x = 7;
+  int y = 5;
+  bar(Foo(x, y));
+}

Copy link
Contributor

@t-rasmud t-rasmud left a comment

Choose a reason for hiding this comment

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

LGTM!

@rniwa rniwa merged commit 5162fde into llvm:main Dec 20, 2024
11 checks passed
@rniwa rniwa deleted the fix-crash-in-find-lambda branch December 20, 2024 18:18
@rniwa
Copy link
Contributor Author

rniwa commented Dec 20, 2024

Thanks for the review!

rniwa added a commit to rniwa/llvm-project that referenced this pull request Feb 3, 2025
devincoughlin pushed a commit to swiftlang/llvm-project that referenced this pull request Feb 25, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

clang:static analyzer clang Clang issues not falling into any other category

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants