2009-11-06 05:24:13 +03:00
|
|
|
//== ReturnUndefChecker.cpp -------------------------------------*- C++ -*--==//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file defines ReturnUndefChecker, which is a path-sensitive
|
|
|
|
// check which looks for undefined or garbage values being returned to the
|
|
|
|
// caller.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "GRExprEngineInternalChecks.h"
|
2010-03-28 00:19:47 +03:00
|
|
|
#include "clang/Checker/BugReporter/BugType.h"
|
2010-01-25 07:41:41 +03:00
|
|
|
#include "clang/Checker/PathSensitive/CheckerVisitor.h"
|
2010-03-28 00:19:47 +03:00
|
|
|
#include "clang/Checker/PathSensitive/GRExprEngine.h"
|
2009-11-06 05:24:13 +03:00
|
|
|
|
|
|
|
using namespace clang;
|
|
|
|
|
|
|
|
namespace {
|
2009-11-28 09:07:30 +03:00
|
|
|
class ReturnUndefChecker :
|
2009-11-06 05:24:13 +03:00
|
|
|
public CheckerVisitor<ReturnUndefChecker> {
|
|
|
|
BuiltinBug *BT;
|
|
|
|
public:
|
|
|
|
ReturnUndefChecker() : BT(0) {}
|
|
|
|
static void *getTag();
|
|
|
|
void PreVisitReturnStmt(CheckerContext &C, const ReturnStmt *RS);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
void clang::RegisterReturnUndefChecker(GRExprEngine &Eng) {
|
|
|
|
Eng.registerCheck(new ReturnUndefChecker());
|
|
|
|
}
|
|
|
|
|
|
|
|
void *ReturnUndefChecker::getTag() {
|
|
|
|
static int x = 0; return &x;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ReturnUndefChecker::PreVisitReturnStmt(CheckerContext &C,
|
|
|
|
const ReturnStmt *RS) {
|
|
|
|
|
|
|
|
const Expr *RetE = RS->getRetValue();
|
|
|
|
if (!RetE)
|
|
|
|
return;
|
|
|
|
|
2010-02-08 19:18:51 +03:00
|
|
|
if (!C.getState()->getSVal(RetE).isUndef())
|
2009-11-06 05:24:13 +03:00
|
|
|
return;
|
|
|
|
|
2009-11-24 01:22:01 +03:00
|
|
|
ExplodedNode *N = C.GenerateSink();
|
2009-11-06 05:24:13 +03:00
|
|
|
|
|
|
|
if (!N)
|
|
|
|
return;
|
|
|
|
|
|
|
|
if (!BT)
|
|
|
|
BT = new BuiltinBug("Garbage return value",
|
|
|
|
"Undefined or garbage value returned to caller");
|
|
|
|
|
|
|
|
EnhancedBugReport *report =
|
2009-11-14 15:08:24 +03:00
|
|
|
new EnhancedBugReport(*BT, BT->getDescription(), N);
|
2009-11-06 05:24:13 +03:00
|
|
|
|
|
|
|
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, RetE);
|
|
|
|
|
|
|
|
C.EmitReport(report);
|
|
|
|
}
|