28 const CheckerBase *Checker;
29 AnalysisDeclContext *AC;
32 explicit CastToStructVisitor(BugReporter &B,
const CheckerBase *Checker,
33 AnalysisDeclContext *A)
34 : BR(B), Checker(Checker), AC(A) {}
35 bool VisitCastExpr(
CastExpr *CE)
override;
39bool CastToStructVisitor::VisitCastExpr(
CastExpr *CE) {
45 const PointerType *OrigPTy = dyn_cast<PointerType>(OrigTy.
getTypePtr());
46 const PointerType *ToPTy = dyn_cast<PointerType>(ToTy.
getTypePtr());
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)
78 const ValueDecl *VD =
nullptr;
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)
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> {
111 void checkASTCodeBody(
const Decl *D, AnalysisManager &Mgr,
112 BugReporter &BR)
const {
114 Visitor.TraverseDecl(
const_cast<Decl *
>(D));
119void ento::registerCastToStructChecker(CheckerManager &mgr) {
123bool ento::shouldRegisterCastToStructChecker(
const CheckerManager &mgr) {
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.
const Decl * getDecl() const
ASTContext & getASTContext() const
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
QualType getPointeeType() const
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
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
AnalysisDeclContext * getAnalysisDeclContext(const Decl *D)
const SourceManager & getSourceManager()
void EmitBasicReport(const Decl *DeclWithIssue, const CheckerFrontend *Checker, StringRef BugName, StringRef BugCategory, StringRef BugStr, PathDiagnosticLocation Loc, ArrayRef< SourceRange > Ranges={}, ArrayRef< FixItHint > Fixits={})
CHECKER * registerChecker(AT &&...Args)
Register a single-part checker (derived from Checker): construct its singleton instance,...
const char *const LogicError
std::variant< struct RequiresDecl, struct HeaderDecl, struct UmbrellaDirDecl, struct ModuleDecl, struct ExcludeDecl, struct ExportDecl, struct ExportAsDecl, struct ExternModuleDecl, struct UseDecl, struct LinkDecl, struct ConfigMacrosDecl, struct ConflictDecl > Decl
All declarations that can appear in a module declaration.
The JSON file list parser is used to communicate input to InstallAPI.
DynamicRecursiveASTVisitorBase< false > DynamicRecursiveASTVisitor