Author: Nico Weber
Date: 2025-01-16T09:33:01-05:00
New Revision: 7dd34baf5505d689161c3a8678322a394d7a2929

URL: 
https://github.com/llvm/llvm-project/commit/7dd34baf5505d689161c3a8678322a394d7a2929
DIFF: 
https://github.com/llvm/llvm-project/commit/7dd34baf5505d689161c3a8678322a394d7a2929.diff

LOG: Revert "[Wunsafe-buffer-usage] Fix false positive when const sized array 
is indexed by const evaluatable expressions (#119340)"

This reverts commit 64c2156d8802b0d7724f65ce854844670e4ec457.
Causes asserts, see
https://github.com/llvm/llvm-project/pull/119340#issuecomment-2595858729

Added: 
    

Modified: 
    clang/lib/Analysis/UnsafeBufferUsage.cpp
    clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp 
b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index bef5fa8624ce48..a9aff39df64746 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -453,11 +453,8 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
     return false;
   }
 
-  Expr::EvalResult EVResult;
-  if (Node.getIdx()->EvaluateAsInt(EVResult, Finder->getASTContext())) {
-    llvm::APSInt ArrIdx = EVResult.Val.getInt();
-    // FIXME: ArrIdx.isNegative() we could immediately emit an error as that's 
a
-    // bug
+  if (const auto *IdxLit = dyn_cast<IntegerLiteral>(Node.getIdx())) {
+    const APInt ArrIdx = IdxLit->getValue();
     if (ArrIdx.isNonNegative() && ArrIdx.getLimitedValue() < limit)
       return true;
   }

diff  --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp 
b/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
index e80b54b7c69677..7dd6c83dbba2a8 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
@@ -92,35 +92,3 @@ char access_strings() {
   c = array_string[5];
   return c;
 }
-
-struct T {
-  int array[10];
-};
-
-const int index = 1;
-
-constexpr int get_const(int x) {
-  if(x < 3)
-    return ++x;
-  else
-    return x + 5;
-};
-
-void array_indexed_const_expr(unsigned idx) {
-  // expected-note@+2 {{change type of 'arr' to 'std::array' to label it for 
hardening}}
-  // expected-warning@+1{{'arr' is an unsafe buffer that does not perform 
bounds checks}}
-  int arr[10];
-  arr[sizeof(int)] = 5;
-
-  int array[sizeof(T)];
-  array[sizeof(int)] = 5;
-  array[sizeof(T) -1 ] = 3;
-
-  int k = arr[6 & 5];
-  k = arr[2 << index];
-  k = arr[8 << index]; // expected-note {{used in buffer access here}}
-  k = arr[16 >> 1];
-  k = arr[get_const(index)];
-  k = arr[get_const(5)]; // expected-note {{used in buffer access here}}
-  k = arr[get_const(4)];
-}


        
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to