From 72f508c0d8d4d033d7e09aaa192333a0b3c9137b Mon Sep 17 00:00:00 2001 From: Anna Zaks Date: Wed, 6 Jan 2016 00:32:56 +0000 Subject: [PATCH] [analyzer] Fix false warning about memory leak for QApplication::postEvent According to Qt documentation Qt takes care of memory allocated for QEvent: http://doc.qt.io/qt-4.8/qcoreapplication.html#postEvent A patch by Evgeniy Dushistov! Differential Revision: http://reviews.llvm.org/D14170 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@256887 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/StaticAnalyzer/Checkers/MallocChecker.cpp | 10 ++++++++++ test/Analysis/Inputs/qt-simulator.h | 16 ++++++++++++++++ test/Analysis/qt_malloc.cpp | 15 +++++++++++++++ 3 files changed, 41 insertions(+) create mode 100644 test/Analysis/Inputs/qt-simulator.h create mode 100644 test/Analysis/qt_malloc.cpp diff --git a/lib/StaticAnalyzer/Checkers/MallocChecker.cpp b/lib/StaticAnalyzer/Checkers/MallocChecker.cpp index 713d9fe285..ce2c19409d 100644 --- a/lib/StaticAnalyzer/Checkers/MallocChecker.cpp +++ b/lib/StaticAnalyzer/Checkers/MallocChecker.cpp @@ -2508,6 +2508,16 @@ bool MallocChecker::mayFreeAnyEscapedMemoryOrIsModeledExplicitly( return true; } + if (FName == "postEvent" && + FD->getQualifiedNameAsString() == "QCoreApplication::postEvent") { + return true; + } + + if (FName == "postEvent" && + FD->getQualifiedNameAsString() == "QCoreApplication::postEvent") { + 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/test/Analysis/Inputs/qt-simulator.h b/test/Analysis/Inputs/qt-simulator.h new file mode 100644 index 0000000000..d1d6c0356b --- /dev/null +++ b/test/Analysis/Inputs/qt-simulator.h @@ -0,0 +1,16 @@ +#pragma clang system_header + +struct QObject { +}; + +struct QEvent { + enum Type { None }; + QEvent(Type) {} +}; + +struct QCoreApplication : public QObject { + static void postEvent(QObject *receiver, QEvent *event); + static QCoreApplication *instance(); +}; + +struct QApplication : public QCoreApplication {}; diff --git a/test/Analysis/qt_malloc.cpp b/test/Analysis/qt_malloc.cpp new file mode 100644 index 0000000000..d29835f73f --- /dev/null +++ b/test/Analysis/qt_malloc.cpp @@ -0,0 +1,15 @@ +// RUN: %clang_cc1 -analyze -analyzer-checker=core,alpha.deadcode.UnreachableCode,alpha.core.CastSize,unix.Malloc,cplusplus -analyzer-store=region -verify %s +// expected-no-diagnostics +#include "Inputs/qt-simulator.h" + +void send(QObject *obj) +{ + QEvent *e1 = new QEvent(QEvent::None); + static_cast(QCoreApplication::instance())->postEvent(obj, e1); + QEvent *e2 = new QEvent(QEvent::None); + QCoreApplication::instance()->postEvent(obj, e2); + QEvent *e3 = new QEvent(QEvent::None); + QCoreApplication::postEvent(obj, e3); + QEvent *e4 = new QEvent(QEvent::None); + QApplication::postEvent(obj, e4); +} -- 2.40.0