https://github.com/bviyer created 
https://github.com/llvm/llvm-project/pull/172399

Fixes https://github.com/llvm/llvm-project/issues/154713

>From 18a648b9b1a5afb20ac964f581f80281377360e1 Mon Sep 17 00:00:00 2001
From: "Balaji V. Iyer" <[email protected]>
Date: Mon, 15 Dec 2025 20:36:00 -0600
Subject: [PATCH] Check for a valid Index in array before getting it

Fixes https://github.com/llvm/llvm-project/issues/154713
---
 clang/lib/AST/ExprConstant.cpp        | 4 ++++
 clang/test/AST/array-overflow-index.c | 9 +++++++++
 2 files changed, 13 insertions(+)
 create mode 100644 clang/test/AST/array-overflow-index.c

diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp
index 11c5e1c6e90f4..fb85119b137ab 100644
--- a/clang/lib/AST/ExprConstant.cpp
+++ b/clang/lib/AST/ExprConstant.cpp
@@ -9749,6 +9749,10 @@ bool LValueExprEvaluator::VisitArraySubscriptExpr(const 
ArraySubscriptExpr *E) {
 
     if (Success) {
       Result.setFrom(Info.Ctx, Val);
+      // If Index cannot be represented as a 64 bit integer, return 
unsuccessful.
+      if (!Index.tryExtValue().has_value())
+        return Error(E);
+
       HandleLValueVectorElement(Info, E, Result, VT->getElementType(),
                                 VT->getNumElements(), Index.getExtValue());
     }
diff --git a/clang/test/AST/array-overflow-index.c 
b/clang/test/AST/array-overflow-index.c
new file mode 100644
index 0000000000000..8a43cfbb01f91
--- /dev/null
+++ b/clang/test/AST/array-overflow-index.c
@@ -0,0 +1,9 @@
+// RUN: %clang_cc1 -verify %s
+
+// ref-no-diagnostics
+// expected-no-diagnostics
+
+int __attribute__((vector_size(4))) test_vector = {1};
+int get_last_element(void) {
+    return test_vector[~0UL];
+}

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to