1 //=== PointerArithChecker.cpp - Pointer arithmetic checker -----*- 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 files defines PointerArithChecker, a builtin checker that checks for
11 // pointer arithmetic on locations other than array elements.
13 //===----------------------------------------------------------------------===//
15 #include "ExprEngineInternalChecks.h"
16 #include "clang/StaticAnalyzer/BugReporter/BugType.h"
17 #include "clang/StaticAnalyzer/PathSensitive/CheckerVisitor.h"
19 using namespace clang
;
23 class PointerArithChecker
24 : public CheckerVisitor
<PointerArithChecker
> {
27 PointerArithChecker() : BT(0) {}
28 static void *getTag();
29 void PreVisitBinaryOperator(CheckerContext
&C
, const BinaryOperator
*B
);
33 void *PointerArithChecker::getTag() {
38 void PointerArithChecker::PreVisitBinaryOperator(CheckerContext
&C
,
39 const BinaryOperator
*B
) {
40 if (B
->getOpcode() != BO_Sub
&& B
->getOpcode() != BO_Add
)
43 const GRState
*state
= C
.getState();
44 SVal LV
= state
->getSVal(B
->getLHS());
45 SVal RV
= state
->getSVal(B
->getRHS());
47 const MemRegion
*LR
= LV
.getAsRegion();
49 if (!LR
|| !RV
.isConstant())
52 // If pointer arithmetic is done on variables of non-array type, this often
53 // means behavior rely on memory organization, which is dangerous.
54 if (isa
<VarRegion
>(LR
) || isa
<CodeTextRegion
>(LR
) ||
55 isa
<CompoundLiteralRegion
>(LR
)) {
57 if (ExplodedNode
*N
= C
.generateNode()) {
59 BT
= new BuiltinBug("Dangerous pointer arithmetic",
60 "Pointer arithmetic done on non-array variables "
61 "means reliance on memory layout, which is "
63 RangedBugReport
*R
= new RangedBugReport(*BT
, BT
->getDescription(), N
);
64 R
->addRange(B
->getSourceRange());
70 void ento::RegisterPointerArithChecker(ExprEngine
&Eng
) {
71 Eng
.registerCheck(new PointerArithChecker());