22 using namespace clang;
26 class CastSizeChecker :
public Checker< check::PreStmt<CastExpr> > {
27 const BugType BT{
this,
"Cast region with wrong size."};
62 assert(
Last &&
"empty structs should already be handled");
64 const Type *ElemType =
Last->getType()->getArrayElementTypeNoTypeQual();
69 if (ArrayTy->getSize() == 1 && TypeSize > FlexSize)
71 else if (!ArrayTy->isZeroSize())
83 if (Left.isNegative())
86 return Left % FlexSize == 0;
105 const MemRegion *R =
C.getSVal(E).getAsRegion();
121 CharUnits typeSize =
C.getASTContext().getTypeSizeInChars(ToPointeeTy);
127 if (regionSize % typeSize == 0)
134 constexpr llvm::StringLiteral Msg =
135 "Cast a region whose size is not a multiple of the destination type "
137 auto R = std::make_unique<PathSensitiveBugReport>(BT, Msg, errorNode);
139 C.emitReport(std::move(R));
147 bool ento::shouldRegisterCastSizeChecker(
const CheckerManager &mgr) {
153 return !LO.CPlusPlus;
static bool evenFlexibleArraySize(ASTContext &Ctx, CharUnits RegionSize, CharUnits TypeSize, QualType ToPointeeTy)
Check if we are casting to a struct with a flexible array at the end.
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 ...
const ConstantArrayType * getAsConstantArrayType(QualType T) const
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
CharUnits - This is an opaque type for sizes expressed in character units.
bool isZero() const
isZero - Test whether the quantity equals zero.
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Represents the canonical version of C arrays with a specified constant size.
specific_decl_iterator - Iterates over a subrange of declarations stored in a DeclContext,...
This represents one expression.
Represents a member of a struct/union/class.
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
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.
Represents a struct/union/class.
bool hasFlexibleArrayMember() const
field_iterator field_end() const
field_iterator field_begin() const
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
RecordDecl * getDecl() const
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
The base class of the type hierarchy.
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
const T * getAs() const
Member-template getAs<specific type>'.
CHECKER * registerChecker(AT &&... Args)
Used to register checkers.
const LangOptions & getLangOpts() const
MemRegion - The root abstract class for all memory regions.
virtual const llvm::APSInt * getKnownValue(ProgramStateRef state, SVal val)=0
Evaluates a given SVal.
SymbolicRegion - A special, "non-concrete" region.
DefinedOrUnknownSVal getDynamicExtent(ProgramStateRef State, const MemRegion *MR, SValBuilder &SVB)
The JSON file list parser is used to communicate input to InstallAPI.