blob: eeaed970b53380648a8c43c0d232a8497b4008b7 [file] [log] [blame]
Zhongxing Xu4f3dc692009-11-09 08:07:38 +00001//=== CastToStructChecker.cpp - Fixed address usage 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 CastToStructChecker, a builtin checker that checks for
Zhongxing Xu36897912010-01-20 07:57:45 +000011// cast from non-struct pointer to struct pointer.
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000012// This check corresponds to CWE-588.
13//
14//===----------------------------------------------------------------------===//
15
Benjamin Kramer5e2d2c22010-03-27 21:19:47 +000016#include "clang/Checker/BugReporter/BugType.h"
Ted Kremenek1309f9a2010-01-25 04:41:41 +000017#include "clang/Checker/PathSensitive/CheckerVisitor.h"
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000018#include "GRExprEngineInternalChecks.h"
19
20using namespace clang;
21
22namespace {
Kovarththanan Rajaratnamba5fb5a2009-11-28 06:07:30 +000023class CastToStructChecker
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000024 : public CheckerVisitor<CastToStructChecker> {
25 BuiltinBug *BT;
26public:
27 CastToStructChecker() : BT(0) {}
28 static void *getTag();
29 void PreVisitCastExpr(CheckerContext &C, const CastExpr *B);
30};
31}
32
33void *CastToStructChecker::getTag() {
34 static int x;
35 return &x;
36}
37
38void CastToStructChecker::PreVisitCastExpr(CheckerContext &C,
39 const CastExpr *CE) {
40 const Expr *E = CE->getSubExpr();
41 ASTContext &Ctx = C.getASTContext();
42 QualType OrigTy = Ctx.getCanonicalType(E->getType());
43 QualType ToTy = Ctx.getCanonicalType(CE->getType());
44
45 PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.getTypePtr());
46 PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr());
47
48 if (!ToPTy || !OrigPTy)
49 return;
50
51 QualType OrigPointeeTy = OrigPTy->getPointeeType();
52 QualType ToPointeeTy = ToPTy->getPointeeType();
53
Douglas Gregorfb87b892010-04-26 21:31:17 +000054 if (!ToPointeeTy->isStructureOrClassType())
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000055 return;
56
57 // We allow cast from void*.
58 if (OrigPointeeTy->isVoidType())
59 return;
60
61 // Now the cast-to-type is struct pointer, the original type is not void*.
62 if (!OrigPointeeTy->isRecordType()) {
Ted Kremenek19d67b52009-11-23 22:22:01 +000063 if (ExplodedNode *N = C.GenerateNode()) {
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000064 if (!BT)
65 BT = new BuiltinBug("Cast from non-struct type to struct type",
66 "Casting a non-structure type to a structure type "
67 "and accessing a field can lead to memory access "
68 "errors or data corruption.");
Benjamin Kramerd02e2322009-11-14 12:08:24 +000069 RangedBugReport *R = new RangedBugReport(*BT,BT->getDescription(), N);
Zhongxing Xu4f3dc692009-11-09 08:07:38 +000070 R->addRange(CE->getSourceRange());
71 C.EmitReport(R);
72 }
73 }
74}
75
76void clang::RegisterCastToStructChecker(GRExprEngine &Eng) {
77 Eng.registerCheck(new CastToStructChecker());
78}