[clang] 3b6a368 - [analyzer] Fix QTimer::singleShot NewDeleteLeaks false positive

Balazs Benics via cfe-commits cfe-commits at lists.llvm.org
Tue May 16 06:40:15 PDT 2023


Author: Michael Klein
Date: 2023-05-16T15:38:55+02:00
New Revision: 3b6a368d763e812024ca6ba4024855603f693291

URL: https://github.com/llvm/llvm-project/commit/3b6a368d763e812024ca6ba4024855603f693291
DIFF: https://github.com/llvm/llvm-project/commit/3b6a368d763e812024ca6ba4024855603f693291.diff

LOG: [analyzer] Fix QTimer::singleShot NewDeleteLeaks false positive

Fixes #39713

fferential Revision: https://reviews.llvm.org/D150552

Added: 
    

Modified: 
    clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
    clang/test/Analysis/Inputs/qt-simulator.h
    clang/test/Analysis/qt_malloc.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
index 67e8afbb432a5..b94b7836fc612 100644
--- a/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
+++ b/clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
@@ -3259,6 +3259,11 @@ bool MallocChecker::mayFreeAnyEscapedMemoryOrIsModeledExplicitly(
     return true;
   }
 
+  if (FName == "singleShotImpl" &&
+      FD->getQualifiedNameAsString() == "QTimer::singleShotImpl") {
+    return true;
+  }
+
   // Handle cases where we know a buffer's /address/ can escape.
   // Note that the above checks handle some special cases where we know that
   // even though the address escapes, it's still our responsibility to free the

diff  --git a/clang/test/Analysis/Inputs/qt-simulator.h b/clang/test/Analysis/Inputs/qt-simulator.h
index a3c7387d2003a..8737aa4d9947f 100644
--- a/clang/test/Analysis/Inputs/qt-simulator.h
+++ b/clang/test/Analysis/Inputs/qt-simulator.h
@@ -6,6 +6,7 @@ struct QSlotObjectBase {};
 
 namespace Qt {
 enum ConnectionType {};
+enum TimerType {};
 }
 
 struct QMetaObject {
@@ -31,3 +32,8 @@ struct QCoreApplication : public QObject {
 };
 
 struct QApplication : public QCoreApplication {};
+
+struct QTimer {
+  static void singleShotImpl(int, Qt::TimerType, const QObject *,
+                             QtPrivate::QSlotObjectBase *);
+};

diff  --git a/clang/test/Analysis/qt_malloc.cpp b/clang/test/Analysis/qt_malloc.cpp
index 0d6fc470f461f..b55ea63271f3d 100644
--- a/clang/test/Analysis/qt_malloc.cpp
+++ b/clang/test/Analysis/qt_malloc.cpp
@@ -19,3 +19,8 @@ void connect(QObject *obj) {
                    new QtPrivate::QSlotObjectBase(), (Qt::ConnectionType)0,
                    nullptr, nullptr);
 }
+
+void singleShot(QTimer *timer) {
+  timer->singleShotImpl(0, (Qt::TimerType)0, nullptr,
+                        new QtPrivate::QSlotObjectBase());
+}


        


More information about the cfe-commits mailing list