1 //=== CastToStructChecker.cpp ----------------------------------*- 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 CastToStructChecker, a builtin checker that checks for
11 // cast from non-struct pointer to struct pointer and widening struct data cast.
12 // This check corresponds to CWE-588.
14 //===----------------------------------------------------------------------===//
16 #include "ClangSACheckers.h"
17 #include "clang/AST/RecursiveASTVisitor.h"
18 #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h"
19 #include "clang/StaticAnalyzer/Core/Checker.h"
20 #include "clang/StaticAnalyzer/Core/CheckerManager.h"
21 #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h"
23 using namespace clang;
27 class CastToStructVisitor : public RecursiveASTVisitor<CastToStructVisitor> {
29 const CheckerBase *Checker;
30 AnalysisDeclContext *AC;
33 explicit CastToStructVisitor(BugReporter &B, const CheckerBase *Checker,
34 AnalysisDeclContext *A)
35 : BR(B), Checker(Checker), AC(A) {}
36 bool VisitCastExpr(const CastExpr *CE);
40 bool CastToStructVisitor::VisitCastExpr(const CastExpr *CE) {
41 const Expr *E = CE->getSubExpr();
42 ASTContext &Ctx = AC->getASTContext();
43 QualType OrigTy = Ctx.getCanonicalType(E->getType());
44 QualType ToTy = Ctx.getCanonicalType(CE->getType());
46 const PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.getTypePtr());
47 const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.getTypePtr());
49 if (!ToPTy || !OrigPTy)
52 QualType OrigPointeeTy = OrigPTy->getPointeeType();
53 QualType ToPointeeTy = ToPTy->getPointeeType();
55 if (!ToPointeeTy->isStructureOrClassType())
58 // We allow cast from void*.
59 if (OrigPointeeTy->isVoidType())
62 // Now the cast-to-type is struct pointer, the original type is not void*.
63 if (!OrigPointeeTy->isRecordType()) {
64 SourceRange Sr[1] = {CE->getSourceRange()};
65 PathDiagnosticLocation Loc(CE, BR.getSourceManager(), AC);
67 AC->getDecl(), Checker, "Cast from non-struct type to struct type",
68 categories::LogicError, "Casting a non-structure type to a structure "
69 "type and accessing a field can lead to memory "
70 "access errors or data corruption.",
73 // Don't warn when size of data is unknown.
74 const auto *U = dyn_cast<UnaryOperator>(E);
75 if (!U || U->getOpcode() != UO_AddrOf)
78 // Don't warn for references
79 const ValueDecl *VD = nullptr;
80 if (const auto *SE = dyn_cast<DeclRefExpr>(U->getSubExpr()))
81 VD = dyn_cast<ValueDecl>(SE->getDecl());
82 else if (const auto *SE = dyn_cast<MemberExpr>(U->getSubExpr()))
83 VD = SE->getMemberDecl();
84 if (!VD || VD->getType()->isReferenceType())
87 if (ToPointeeTy->isIncompleteType() ||
88 OrigPointeeTy->isIncompleteType())
91 // Warn when there is widening cast.
92 unsigned ToWidth = Ctx.getTypeInfo(ToPointeeTy).Width;
93 unsigned OrigWidth = Ctx.getTypeInfo(OrigPointeeTy).Width;
94 if (ToWidth <= OrigWidth)
97 PathDiagnosticLocation Loc(CE, BR.getSourceManager(), AC);
98 BR.EmitBasicReport(AC->getDecl(), Checker, "Widening cast to struct type",
99 categories::LogicError,
100 "Casting data to a larger structure type and accessing "
101 "a field can lead to memory access errors or data "
103 Loc, CE->getSourceRange());
110 class CastToStructChecker : public Checker<check::ASTCodeBody> {
112 void checkASTCodeBody(const Decl *D, AnalysisManager &Mgr,
113 BugReporter &BR) const {
114 CastToStructVisitor Visitor(BR, this, Mgr.getAnalysisDeclContext(D));
115 Visitor.TraverseDecl(const_cast<Decl *>(D));
118 } // end anonymous namespace
120 void ento::registerCastToStructChecker(CheckerManager &mgr) {
121 mgr.registerChecker<CastToStructChecker>();