[clang] [clang][StaticAnalyzer] Reduce MallocSizeofChecker false positives for layout-compatible types (PR #200253)
Balázs Benics via cfe-commits
cfe-commits at lists.llvm.org
Tue Jun 16 08:21:57 PDT 2026
================
@@ -149,24 +149,40 @@ static bool typesCompatible(ASTContext &C, QualType A, QualType B) {
if (A.getTypePtr() == B.getTypePtr())
return true;
+ const PointerType *ptrA = A->getAs<PointerType>();
+ const PointerType *ptrB = B->getAs<PointerType>();
+
// When neither type is a pointer and exactly one is a record type, check
// target-specific size and alignment. This avoids false positives for
// types that wrap another type with the same layout (e.g.
// std::atomic<int32_t> vs int32_t, or struct{int32_t x;} vs int32_t),
// while preserving warnings for unrelated types that happen to share a
// size (e.g. long vs double, struct A vs struct B).
- if (!A->getAs<PointerType>() && !B->getAs<PointerType>() &&
- (A->isRecordType() != B->isRecordType()) && !A->isIncompleteType() &&
- !B->isIncompleteType() && C.getTypeSize(A) == C.getTypeSize(B) &&
- C.getTypeAlign(A) <= C.getTypeAlign(B))
- return true;
-
- if (const PointerType *ptrA = A->getAs<PointerType>())
- if (const PointerType *ptrB = B->getAs<PointerType>()) {
- A = ptrA->getPointeeType();
- B = ptrB->getPointeeType();
- continue;
+ if (!ptrA && !ptrB && (A->isRecordType() != B->isRecordType()) &&
+ !A->isIncompleteType() && !B->isIncompleteType()) {
+ const RecordType *RecTy = A->getAs<RecordType>();
+ QualType Scalar = B;
+ if (!RecTy) {
+ RecTy = B->getAs<RecordType>();
+ Scalar = A;
}
+ if (const CXXRecordDecl *RD =
----------------
steakhal wrote:
According to the LLVM style, one does not need to name the type in the declaration if it was already spelled in the initializer expression. You can use `auto` here.
https://github.com/llvm/llvm-project/pull/200253
More information about the cfe-commits
mailing list