Zhongxing Xu | 246a9ad | 2009-10-31 08:44:33 +0000 | [diff] [blame] | 1 | // UndefDerefChecker.cpp - Undefined dereference checker ----------*- C++ -*--// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This defines UndefDerefChecker, a builtin check in GRExprEngine that performs |
| 11 | // checks for defined pointers at loads and stores. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #include "clang/Analysis/PathSensitive/Checkers/UndefDerefChecker.h" |
| 16 | #include "clang/Analysis/PathSensitive/GRExprEngine.h" |
| 17 | #include "clang/Analysis/PathSensitive/BugReporter.h" |
| 18 | |
| 19 | using namespace clang; |
| 20 | |
| 21 | void *UndefDerefChecker::getTag() { |
| 22 | static int x = 0; |
| 23 | return &x; |
| 24 | } |
| 25 | |
| 26 | ExplodedNode *UndefDerefChecker::CheckLocation(const Stmt *S, |
| 27 | ExplodedNode *Pred, |
| 28 | const GRState *state, SVal V, |
| 29 | GRExprEngine &Eng) { |
| 30 | GRStmtNodeBuilder &Builder = Eng.getBuilder(); |
| 31 | BugReporter &BR = Eng.getBugReporter(); |
| 32 | |
| 33 | if (V.isUndef()) { |
| 34 | ExplodedNode *N = Builder.generateNode(S, state, Pred, |
| 35 | ProgramPoint::PostUndefLocationCheckFailedKind); |
| 36 | if (N) { |
| 37 | N->markAsSink(); |
| 38 | |
| 39 | if (!BT) |
| 40 | BT = new BuiltinBug(0, "Undefined dereference", |
| 41 | "Dereference of undefined pointer value"); |
| 42 | |
| 43 | EnhancedBugReport *R = |
| 44 | new EnhancedBugReport(*BT, BT->getDescription().c_str(), N); |
| 45 | R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, |
| 46 | bugreporter::GetDerefExpr(N)); |
| 47 | BR.EmitReport(R); |
| 48 | } |
| 49 | return 0; |
| 50 | } |
| 51 | |
| 52 | return Pred; |
| 53 | } |