20 using namespace clang;
24 class CastSizeChecker :
public Checker< check::PreStmt<CastExpr> > {
25 mutable std::unique_ptr<BuiltinBug> BT;
28 void checkPreStmt(
const CastExpr *CE, CheckerContext &C)
const;
58 for (; Iter !=
End; ++Iter)
60 assert(Last &&
"empty structs should already be handled");
67 if (ArrayTy->getSize() == 1 && TypeSize > FlexSize)
69 else if (ArrayTy->getSize() != 0)
84 return Left % FlexSize == 0;
87 void CastSizeChecker::checkPreStmt(
const CastExpr *CE,CheckerContext &C)
const {
103 const MemRegion *R = C.getSVal(E).getAsRegion();
107 const SymbolicRegion *SR = dyn_cast<SymbolicRegion>(R);
111 SValBuilder &svalBuilder = C.getSValBuilder();
112 SVal extent = SR->getExtent(svalBuilder);
113 const llvm::APSInt *extentInt = svalBuilder.getKnownValue(state, extent);
118 CharUnits typeSize = C.getASTContext().getTypeSizeInChars(ToPointeeTy);
121 if (typeSize.isZero())
124 if (regionSize % typeSize == 0)
130 if (ExplodedNode *errorNode = C.generateErrorNode()) {
132 BT.reset(
new BuiltinBug(
this,
"Cast region with wrong size.",
133 "Cast a region whose size is not a multiple" 134 " of the destination type size."));
135 auto R = std::make_unique<PathSensitiveBugReport>(*BT, BT->getDescription(),
138 C.emitReport(std::move(R));
142 void ento::registerCastSizeChecker(CheckerManager &mgr) {
143 mgr.registerChecker<CastSizeChecker>();
146 bool ento::shouldRegisterCastSizeChecker(
const LangOptions &LO) {
151 return !LO.CPlusPlus;
PointerType - C99 6.7.5.1 - Pointer Declarators.
QualType getPointeeType() const
A (possibly-)qualified type.
IntrusiveRefCntPtr< const ProgramState > ProgramStateRef
The base class of the type hierarchy.
bool isZero() const
isZero - Test whether the quantity equals zero.
const T * getAs() const
Member-template getAs<specific type>'.
Represents a struct/union/class.
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
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...
i32 captured_struct **param SharedsTy A type which contains references the shared variables *param Shareds Context with the list of shared variables from the p *TaskFunction *param Data Additional data for task generation like final * state
CharUnits - This is an opaque type for sizes expressed in character units.
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
field_iterator field_begin() const
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
bool isNegative() const
isNegative - Test whether the quantity is less than zero.
This represents one expression.
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.
field_iterator field_end() const
static CharUnits fromQuantity(QuantityType Quantity)
fromQuantity - Construct a CharUnits quantity from a raw integer type.
RecordDecl * getDecl() const
const ConstantArrayType * getAsConstantArrayType(QualType T) const
bool hasFlexibleArrayMember() const
Dataflow Directional Tag Classes.
const Type * getArrayElementTypeNoTypeQual() const
If this is an array type, return the element type of the array, potentially with type qualifiers miss...
specific_decl_iterator - Iterates over a subrange of declarations stored in a DeclContext, providing only those that are of type SpecificDecl (or a class derived from it).
A helper class that allows the use of isa/cast/dyncast to detect TagType objects of structs/unions/cl...
bool isIncompleteType(NamedDecl **Def=nullptr) const
Types are partitioned into 3 broad categories (C99 6.2.5p1): object types, function types...
CanQualType getCanonicalType(QualType T) const
Return the canonical (structural) type corresponding to the specified potentially non-canonical type ...
SourceRange getSourceRange() const LLVM_READONLY
SourceLocation tokens are not useful in isolation - they are low level value objects created/interpre...
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
Represents the canonical version of C arrays with a specified constant size.