blob: acc848ac8edb1156971a9f95ca291df1c2fc6f0e [file] [log] [blame]
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +00001//=== PointerSubChecker.cpp - Pointer subtraction 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 files defines PointerSubChecker, a builtin checker that checks for
11// pointer subtractions on two pointers pointing to different memory chunks.
12// This check corresponds to CWE-469.
13//
14//===----------------------------------------------------------------------===//
15
Ted Kremenek1309f9a2010-01-25 04:41:41 +000016#include "clang/Checker/PathSensitive/CheckerVisitor.h"
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000017#include "GRExprEngineInternalChecks.h"
18
19using namespace clang;
20
21namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000022class PointerSubChecker
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000023 : public CheckerVisitor<PointerSubChecker> {
24 BuiltinBug *BT;
25public:
26 PointerSubChecker() : BT(0) {}
27 static void *getTag();
28 void PreVisitBinaryOperator(CheckerContext &C, const BinaryOperator *B);
29};
30}
31
32void *PointerSubChecker::getTag() {
33 static int x;
34 return &x;
35}
36
37void PointerSubChecker::PreVisitBinaryOperator(CheckerContext &C,
38 const BinaryOperator *B) {
39 // When doing pointer subtraction, if the two pointers do not point to the
40 // same memory chunk, emit a warning.
41 if (B->getOpcode() != BinaryOperator::Sub)
42 return;
43
44 const GRState *state = C.getState();
45 SVal LV = state->getSVal(B->getLHS());
46 SVal RV = state->getSVal(B->getRHS());
47
48 const MemRegion *LR = LV.getAsRegion();
49 const MemRegion *RR = RV.getAsRegion();
50
Zhongxing Xuadca2712009-11-10 02:37:53 +000051 if (!(LR && RR))
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000052 return;
53
Zhongxing Xuadca2712009-11-10 02:37:53 +000054 const MemRegion *BaseLR = LR->getBaseRegion();
55 const MemRegion *BaseRR = RR->getBaseRegion();
56
57 if (BaseLR == BaseRR)
58 return;
59
60 // Allow arithmetic on different symbolic regions.
61 if (isa<SymbolicRegion>(BaseLR) || isa<SymbolicRegion>(BaseRR))
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000062 return;
63
Ted Kremenek19d67b52009-11-23 22:22:01 +000064 if (ExplodedNode *N = C.GenerateNode()) {
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000065 if (!BT)
66 BT = new BuiltinBug("Pointer subtraction",
Zhongxing Xudfed7a12009-11-09 07:29:39 +000067 "Subtraction of two pointers that do not point to "
68 "the same memory chunk may cause incorrect result.");
Benjamin Kramerd02e2322009-11-14 12:08:24 +000069 RangedBugReport *R = new RangedBugReport(*BT, BT->getDescription(), N);
Zhongxing Xu3ce2dc32009-11-09 05:34:10 +000070 R->addRange(B->getSourceRange());
71 C.EmitReport(R);
72 }
73}
74
75void clang::RegisterPointerSubChecker(GRExprEngine &Eng) {
76 Eng.registerCheck(new PointerSubChecker());
77}