1 //== ReturnUndefChecker.cpp -------------------------------------*- C++ -*--==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines ReturnUndefChecker, which is a path-sensitive
11 // check which looks for undefined or garbage values being returned to the
14 //===----------------------------------------------------------------------===//
16 #include "ClangSACheckers.h"
17 #include "clang/StaticAnalyzer/Core/Checker.h"
18 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
19 #include "clang/StaticAnalyzer/Core/PathSensitive/CallEvent.h"
20 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
21 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
23 using namespace clang;
27 class ReturnUndefChecker :
28 public Checker< check::PreStmt<ReturnStmt> > {
29 mutable OwningPtr<BuiltinBug> BT;
31 void checkPreStmt(const ReturnStmt *RS, CheckerContext &C) const;
35 void ReturnUndefChecker::checkPreStmt(const ReturnStmt *RS,
36 CheckerContext &C) const {
38 const Expr *RetE = RS->getRetValue();
42 if (!C.getState()->getSVal(RetE, C.getLocationContext()).isUndef())
45 // "return;" is modeled to evaluate to an UndefinedValue. Allow UndefinedValue
46 // to be returned in functions returning void to support the following pattern:
53 const StackFrameContext *SFC = C.getStackFrame();
54 QualType RT = CallEvent::getDeclaredResultType(SFC->getDecl());
55 if (!RT.isNull() && RT->isSpecificBuiltinType(BuiltinType::Void))
58 ExplodedNode *N = C.generateSink();
64 BT.reset(new BuiltinBug("Garbage return value",
65 "Undefined or garbage value returned to caller"));
68 new BugReport(*BT, BT->getDescription(), N);
70 report->addRange(RetE->getSourceRange());
71 bugreporter::trackNullOrUndefValue(N, RetE, *report);
76 void ento::registerReturnUndefChecker(CheckerManager &mgr) {
77 mgr.registerChecker<ReturnUndefChecker>();