[clang] [Clang] Fix missing -Warray-bounds warning on member function calls. (PR #179647)
Prajwal P J via cfe-commits
cfe-commits at lists.llvm.org
Thu Feb 5 01:48:36 PST 2026
https://github.com/pjalwadi updated https://github.com/llvm/llvm-project/pull/179647
>From d3e9b62998d92b3ea3948d506300673af1240d17 Mon Sep 17 00:00:00 2001
From: prajwal jalwadi <prajwaljalwadi at gmail.com>
Date: Wed, 4 Feb 2026 15:27:57 +0530
Subject: [PATCH] [Clang] Fix missing -Warray-bounds warning on member function
calls
Fixes #179128.
This patch fixes a false negative where Clang failed to detect out-of-bounds access when calling a member function on an invalid array index. It adds handling for CXXMemberCallExpr in CheckArrayAccess.
Signed-off-by: prajwal jalwadi<prajwaljalwadi at gmail.com>
---
clang/docs/ReleaseNotes.rst | 4 ++++
clang/lib/Sema/SemaChecking.cpp | 4 ++++
clang/test/SemaCXX/constant-expression-cxx2a.cpp | 5 +++--
3 files changed, 11 insertions(+), 2 deletions(-)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 7dc6881ed43e6..03590678b31cb 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -204,6 +204,10 @@ Improvements to Clang's diagnostics
- Improved ``-Wassign-enum`` performance by caching enum enumerator values. (#GH176454)
+- Fixed a false negative in ``-Warray-bounds`` where the warning was suppressed
+ when accessing a member function on a past-the-end array element.
+ (#GH179128)
+
Improvements to Clang's time-trace
----------------------------------
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 3f19b6ad16c13..cec6060cc9dab 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -15449,6 +15449,10 @@ void Sema::CheckArrayAccess(const Expr *expr) {
expr = cast<MemberExpr>(expr)->getBase();
break;
}
+ case Stmt::CXXMemberCallExprClass: {
+ expr = cast<CXXMemberCallExpr>(expr)->getImplicitObjectArgument();
+ break;
+ }
case Stmt::ArraySectionExprClass: {
const ArraySectionExpr *ASE = cast<ArraySectionExpr>(expr);
// FIXME: We should probably be checking all of the elements to the
diff --git a/clang/test/SemaCXX/constant-expression-cxx2a.cpp b/clang/test/SemaCXX/constant-expression-cxx2a.cpp
index 4fcd243b4442c..fb60b5300c362 100644
--- a/clang/test/SemaCXX/constant-expression-cxx2a.cpp
+++ b/clang/test/SemaCXX/constant-expression-cxx2a.cpp
@@ -1241,8 +1241,9 @@ namespace dtor_call {
}
constexpr void destroy_past_end_array() { // expected-error {{never produces a constant expression}}
- A a[2];
- a[2].~A(); // expected-note {{destruction of dereferenced one-past-the-end pointer}}
+ A a[2]; // expected-note {{array 'a' declared here}}
+ a[2].~A(); // expected-note {{destruction of dereferenced one-past-the-end pointer}} \
+ // expected-warning {{array index 2 is past the end of the array}}
}
union As {
More information about the cfe-commits
mailing list