llvm-project/clang/test/Analysis/malloc-free-after-return.cpp
Reka Kovacs 122171e235 [analyzer] Detect pointers escaped after ReturnStmt execution in MallocChecker.
Objects local to a function are destroyed right after the statement returning
(part of) them is executed in the analyzer. This patch enables MallocChecker to
warn in these cases.

Differential Revision: https://reviews.llvm.org/D49361

llvm-svn: 338780
2018-08-02 23:02:08 +00:00

22 lines
476 B
C++

// RUN: %clang_analyze_cc1 -analyzer-checker=cplusplus.NewDelete -verify %s
#include "Inputs/system-header-simulator-cxx.h"
struct S {
S() : Data(new int) {}
~S() { delete Data; }
int *getData() { return Data; }
private:
int *Data;
};
int *freeAfterReturnTemp() {
return S().getData(); // expected-warning {{Use of memory after it is freed}}
}
int *freeAfterReturnLocal() {
S X;
return X.getData();
} // expected-warning {{Use of memory after it is freed}}