https://github.com/Hao-Chen2337 updated https://github.com/llvm/llvm-project/pull/215724
>From 92d5c5a0032614a66965f73dd4f2816f3198a652 Mon Sep 17 00:00:00 2001 From: Hao-Chen2337 <[email protected]> Date: Wed, 12 Aug 2026 11:13:14 +0800 Subject: [PATCH] [clang-tidy] Fix crash on pointer arithmetic with an incomplete type in bugprone-pointer-arithmetic-on-polymorphic-object --- .../PointerArithmeticOnPolymorphicObjectCheck.cpp | 8 ++++++-- .../pointer-arithmetic-on-polymorphic-object-all.cpp | 10 ++++++++++ ...nter-arithmetic-on-polymorphic-object-decl-only.cpp | 10 ++++++++++ 3 files changed, 26 insertions(+), 2 deletions(-) diff --git a/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp index ef5fee9f3d2c3..e54a77953283a 100644 --- a/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp +++ b/clang-tools-extra/clang-tidy/bugprone/PointerArithmeticOnPolymorphicObjectCheck.cpp @@ -15,8 +15,12 @@ using namespace clang::ast_matchers; namespace clang::tidy::bugprone { namespace { -AST_MATCHER(CXXRecordDecl, isAbstract) { return Node.isAbstract(); } -AST_MATCHER(CXXRecordDecl, isPolymorphic) { return Node.isPolymorphic(); } +AST_MATCHER(CXXRecordDecl, isAbstract) { + return Node.hasDefinition() && Node.isAbstract(); +} +AST_MATCHER(CXXRecordDecl, isPolymorphic) { + return Node.hasDefinition() && Node.isPolymorphic(); +} } // namespace PointerArithmeticOnPolymorphicObjectCheck:: diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp index f8163a2fd3fb8..80be193a41535 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-all.cpp @@ -150,3 +150,13 @@ struct TemplateHolder : Base { (void)x; } }; + +// pointer arithmetic on an incomplete type must not crash the matcher. +template <typename T> +struct IncompletePointeeCrash { + struct Incomplete; + void f(Incomplete *p) { + p[0]; + // no-warning + } +}; diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-decl-only.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-decl-only.cpp index 48757bbc9b10e..f8a741cb5b174 100644 --- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-decl-only.cpp +++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/pointer-arithmetic-on-polymorphic-object-decl-only.cpp @@ -139,3 +139,13 @@ void typeAliases(BaseAlias *b, DerivedAlias *d, FinalDerivedAlias *fd, fdp += 1; // no-warning } + +// pointer arithmetic on an incomplete type must not crash the matcher. +template <typename T> +struct IncompletePointeeCrash { + struct Incomplete; + void f(Incomplete *p) { + p[0]; + // no-warning + } +}; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
