Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions clang/lib/StaticAnalyzer/Checkers/MallocChecker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1371,6 +1371,19 @@ void MallocChecker::checkIfFreeNameIndex(ProgramStateRef State,
C.addTransition(State);
}

bool isVoidStar(QualType T) {
return !T.isNull() && T->isPointerType() && T->getPointeeType()->isVoidType();
}

const Expr* getPlacementNewBufferArg(const CallExpr *CE, const FunctionDecl *FD) {
if (CE->getNumArgs() == 1)
return nullptr;
// Second argument of placement new must be void*
if (!isVoidStar(FD->getParamDecl(1)->getType()))
return nullptr;
return CE->getArg(1);
}

void MallocChecker::checkCXXNewOrCXXDelete(ProgramStateRef State,
const CallEvent &Call,
CheckerContext &C) const {
Expand All @@ -1386,6 +1399,14 @@ void MallocChecker::checkCXXNewOrCXXDelete(ProgramStateRef State,
// processed by the checkPostStmt callbacks for CXXNewExpr and
// CXXDeleteExpr.
const FunctionDecl *FD = C.getCalleeDecl(CE);
if (const auto *BufArg = getPlacementNewBufferArg(CE, FD)) {
// Placement new does not allocate memory
auto RetVal = State->getSVal(BufArg, Call.getLocationContext());
State = State->BindExpr(CE, C.getLocationContext(), RetVal);
C.addTransition(State);
return;
}

switch (FD->getOverloadedOperator()) {
case OO_New:
State = MallocMemAux(C, Call, CE->getArg(0), UndefinedVal(), State,
Expand Down
41 changes: 39 additions & 2 deletions clang/test/Analysis/NewDelete-checker-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks
//
// RUN: %clang_analyze_cc1 -std=c++17 -fblocks -verify %s \
// RUN: -verify=expected,leak \
// RUN: -verify=expected,leak,inspection \
// RUN: -analyzer-checker=core \
// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks
// RUN: -analyzer-checker=cplusplus.NewDeleteLeaks \
// RUN: -analyzer-checker=debug.ExprInspection

#include "Inputs/system-header-simulator-cxx.h"

Expand Down Expand Up @@ -63,6 +64,42 @@ void testGlobalNoThrowPlacementExprNewBeforeOverload() {
int *p = new(std::nothrow) int;
} // leak-warning{{Potential leak of memory pointed to by 'p'}}

//----- Standard pointer placement operators
void testGlobalPointerPlacementNew() {
int i;
void *p1 = operator new(0, &i); // no warn

void *p2 = operator new[](0, &i); // no warn

int *p3 = new(&i) int; // no warn

int *p4 = new(&i) int[0]; // no warn
}

template<typename T>
void clang_analyzer_dump(T x);

void testPlacementNewBufValue() {
int i = 10;
int *p = new(&i) int;
clang_analyzer_dump(p); // inspection-warning{{&i}}
clang_analyzer_dump(*p); // inspection-warning{{10}}
}

void testPlacementNewBufValueExplicitOp() {
int i = 10;
int *p = (int*)operator new(sizeof(int), &i);
clang_analyzer_dump(p); // inspection-warning{{&i}}
clang_analyzer_dump(*p); // inspection-warning{{10}}
}

void testPlacementArrNewBufValueExplicitArrOp() {
int i = 10;
int *p = (int*)operator new[](sizeof(int), &i);
clang_analyzer_dump(p); // inspection-warning{{&i}}
clang_analyzer_dump(*p); // inspection-warning{{10}}
}

//----- Other cases
void testNewMemoryIsInHeap() {
int *p = new int;
Expand Down
Loading