//=== CastSizeChecker.cpp ---------------------------------------*- C++ -*-===// // // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. // See https://llvm.org/LICENSE.txt for license information. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception // //===----------------------------------------------------------------------===// // // CastSizeChecker checks when casting a malloc'ed symbolic region to type T, // whether the size of the symbolic region is a multiple of the size of T. // //===----------------------------------------------------------------------===// #include "clang/AST/CharUnits.h" #include "clang/StaticAnalyzer/Checkers/BuiltinCheckerRegistration.h" #include "clang/StaticAnalyzer/Core/BugReporter/BugType.h" #include "clang/StaticAnalyzer/Core/Checker.h" #include "clang/StaticAnalyzer/Core/CheckerManager.h" #include "clang/StaticAnalyzer/Core/PathSensitive/CheckerContext.h" #include "clang/StaticAnalyzer/Core/PathSensitive/DynamicExtent.h" usingnamespaceclang; usingnamespaceento; namespace { class CastSizeChecker : public Checker< check::PreStmt<CastExpr> > { … }; } /// Check if we are casting to a struct with a flexible array at the end. /// \code /// struct foo { /// size_t len; /// struct bar data[]; /// }; /// \endcode /// or /// \code /// struct foo { /// size_t len; /// struct bar data[0]; /// } /// \endcode /// In these cases it is also valid to allocate size of struct foo + a multiple /// of struct bar. static bool evenFlexibleArraySize(ASTContext &Ctx, CharUnits RegionSize, CharUnits TypeSize, QualType ToPointeeTy) { … } void CastSizeChecker::checkPreStmt(const CastExpr *CE,CheckerContext &C) const { … } void ento::registerCastSizeChecker(CheckerManager &mgr) { … } bool ento::shouldRegisterCastSizeChecker(const CheckerManager &mgr) { … }