Go to the documentation of this file.
22 using namespace clang;
26 class CastSizeChecker :
public Checker< check::PreStmt<CastExpr> > {
27 mutable std::unique_ptr<BuiltinBug> BT;
30 void checkPreStmt(
const CastExpr *CE, CheckerContext &C)
const;
60 for (; Iter !=
End; ++Iter)
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->getSize() != 0)
86 return Left % FlexSize == 0;
89 void CastSizeChecker::checkPreStmt(
const CastExpr *CE,CheckerContext &C)
const {
105 const MemRegion *R =
C.getSVal(E).getAsRegion();
109 const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
113 SValBuilder &svalBuilder =
C.getSValBuilder();
121 CharUnits typeSize =
C.getASTContext().getTypeSizeInChars(ToPointeeTy);
127 if (regionSize % typeSize == 0)
133 if (ExplodedNode *errorNode =
C.generateErrorNode()) {
135 BT.reset(
new BuiltinBug(
this,
"Cast region with wrong size.",
136 "Cast a region whose size is not a multiple"
137 " of the destination type size."));
138 auto R = std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(),
141 C.emitReport(std::move(R));
145 void ento::registerCastSizeChecker(CheckerManager &mgr) {
146 mgr.registerChecker<CastSizeChecker>();
149 bool ento::shouldRegisterCastSizeChecker(
const CheckerManager &mgr) {
155 return !LO.CPlusPlus;
bool hasFlexibleArrayMember() const
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
field_iterator field_begin() const
specific_decl_iterator - Iterates over a subrange of declarations stored in a DeclContext,...
Represents the canonical version of C arrays with a specified constant size.
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
A (possibly-)qualified type.
Represents a member of a struct/union/class.
The base class of the type hierarchy.
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
const ConstantArrayType * getAsConstantArrayType(QualType T) const
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
const T * getAs() const
Member-template getAs<specific type>'.
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
bool isNegative() const
isNegative - Test whether the quantity is less than zero.
and static some checkers Checker The latter are built on top of the former via the Checker and CheckerVisitor and attempts to isolate them from much of the gore of the internal analysis the analyzer is basically a source code simulator that traces out possible paths of execution The state of the and the combination of state and program point is a node in an exploded which has the entry program point and initial state
bool isZero() const
isZero - Test whether the quantity equals zero.
field_iterator field_end() const
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
DefinedOrUnknownSVal getDynamicExtent(ProgramStateRef State, const MemRegion *MR, SValBuilder &SVB)
PointerType - C99 6.7.5.1 - Pointer Declarators.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
RecordDecl * getDecl() const
CharUnits - This is an opaque type for sizes expressed in character units.
QualType getPointeeType() const
This represents one expression.
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types,...
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
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.
Represents a struct/union/class.