blob: e51e10e3aae95ca956fec09da6ba5f36aa65277b [file] [log] [blame]
Zhongxing Xu5206f0b2009-11-03 12:13:38 +00001//=== UndefSizedVLAChecker.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 UndefSizedVLAChecker, a builtin check in GRExprEngine that
11// performs checks for declaration of VLA of undefined size.
12//
13//===----------------------------------------------------------------------===//
14
15#include "clang/Analysis/PathSensitive/Checkers/UndefSizedVLAChecker.h"
16#include "clang/Analysis/PathSensitive/GRExprEngine.h"
17#include "clang/Analysis/PathSensitive/BugReporter.h"
18
19using namespace clang;
20
21void *UndefSizedVLAChecker::getTag() {
22 static int x = 0;
23 return &x;
24}
25
26ExplodedNode *UndefSizedVLAChecker::CheckType(QualType T, ExplodedNode *Pred,
27 const GRState *state,
28 Stmt *S, GRExprEngine &Eng) {
29 GRStmtNodeBuilder &Builder = Eng.getBuilder();
30 BugReporter &BR = Eng.getBugReporter();
31
32 if (VariableArrayType* VLA = dyn_cast<VariableArrayType>(T)) {
33 // FIXME: Handle multi-dimensional VLAs.
34 Expr* SE = VLA->getSizeExpr();
35 SVal Size_untested = state->getSVal(SE);
36
37 if (Size_untested.isUndef()) {
38 if (ExplodedNode* N = Builder.generateNode(S, state, Pred)) {
39 N->markAsSink();
40 if (!BT)
41 BT = new BugType("Declare variable-length array (VLA) of undefined "
42 "size", "Logic error");
43
44 EnhancedBugReport *R =
45 new EnhancedBugReport(*BT, BT->getName().c_str(), N);
46 R->addRange(SE->getSourceRange());
47 R->addVisitorCreator(bugreporter::registerTrackNullOrUndefValue, SE);
48 BR.EmitReport(R);
49 }
50 return 0;
51 }
52 }
53 return Pred;
54}