opnsense-src/lib/StaticAnalyzer/Checkers/ReturnUndefChecker.cpp

65 lines
1.9 KiB
C++
Raw Normal View History

2009-11-18 09:59:57 -05: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 "ClangSACheckers.h"
#include "clang/StaticAnalyzer/Core/Checker.h"
#include "clang/StaticAnalyzer/Core/CheckerManager.h"
#include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
#include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
2009-11-18 09:59:57 -05:00
using namespace clang;
using namespace ento;
2009-11-18 09:59:57 -05:00
namespace {
2009-12-01 06:08:04 -05:00
class ReturnUndefChecker :
public Checker< check::PreStmt<ReturnStmt> > {
mutable llvm::OwningPtr<BuiltinBug> BT;
2009-11-18 09:59:57 -05:00
public:
void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
2009-11-18 09:59:57 -05:00
};
}
void ReturnUndefChecker::checkPreStmt(const ReturnStmt *RS,
CheckerContext &C) const {
2009-11-18 09:59:57 -05:00
const Expr *RetE = RS->getRetValue();
if (!RetE)
return;
if (!C.getState()->getSVal(RetE).isUndef())
return;
ExplodedNode *N = C.generateSink();
2009-11-18 09:59:57 -05:00
if (!N)
return;
if (!BT)
BT.reset(new BuiltinBug("Garbage return value",
"Undefined or garbage value returned to caller"));
2009-11-18 09:59:57 -05:00
EnhancedBugReport *report =
new EnhancedBugReport(*BT, BT->getDescription(), N);
report->addRange(RetE->getSourceRange());
2009-11-18 09:59:57 -05:00
report->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, RetE);
C.EmitReport(report);
}
void ento::registerReturnUndefChecker(CheckerManager &mgr) {
mgr.registerChecker<ReturnUndefChecker>();
}