35 bool VisitCastExpr(
const CastExpr *CE);
39bool CastToStructVisitor::VisitCastExpr(
const CastExpr *CE) {
48 if (!ToPTy || !OrigPTy)
66 AC->getDecl(),
Checker,
"Cast from non-struct type to struct type",
68 "type and accessing a field can lead to memory "
69 "access errors or data corruption.",
73 const auto *
U = dyn_cast<UnaryOperator>(
E);
74 if (!
U ||
U->getOpcode() != UO_AddrOf)
79 if (
const auto *SE = dyn_cast<DeclRefExpr>(
U->getSubExpr()))
81 else if (
const auto *SE = dyn_cast<MemberExpr>(
U->getSubExpr()))
82 VD = SE->getMemberDecl();
93 if (ToWidth <= OrigWidth)
97 BR.EmitBasicReport(AC->getDecl(),
Checker,
"Widening cast to struct type",
99 "Casting data to a larger structure type and accessing "
100 "a field can lead to memory access errors or data "
109class CastToStructChecker :
public Checker<check::ASTCodeBody> {
114 Visitor.TraverseDecl(
const_cast<Decl *
>(
D));
123bool ento::shouldRegisterCastToStructChecker(
const CheckerManager &mgr) {
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
TypeInfo getTypeInfo(const Type *T) const
Get the size and alignment of the specified complete type in bits.
AnalysisDeclContext contains the context data for the function, method or block under analysis.
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Decl - This represents one declaration (or definition), e.g.
This represents one expression.
PointerType - C99 6.7.5.1 - Pointer Declarators.
QualType getPointeeType() const
A (possibly-)qualified type.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
A class that does preorder or postorder depth-first traversal on the entire Clang AST and visits each...
A trivial tuple used to represent a source range.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
bool isReferenceType() const
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
bool isStructureOrClassType() const
bool isRecordType() const
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
BugReporter is a utility class for generating PathDiagnostics for analysis.
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
const char *const LogicError
The JSON file list parser is used to communicate input to InstallAPI.