[clang] "Reland "[Wunsafe-buffer-usage] Fix false positive when const sized array is indexed by const evaluatable expressions (#119340)"" (PR #123713)
Malavika Samak via cfe-commits
cfe-commits at lists.llvm.org
Tue Jan 21 00:38:40 PST 2025
https://github.com/malavikasamak created https://github.com/llvm/llvm-project/pull/123713
This reverts commit 7dd34baf5505d689161c3a8678322a394d7a2929.
Fixed the assertion violation reported by 7dd34baf5505d689161c3a8678322a394d7a2929
>From b3b7a079e861432554eb3c85a2cc235980f53f65 Mon Sep 17 00:00:00 2001
From: MalavikaSamak <malavika2 at apple.com>
Date: Tue, 21 Jan 2025 14:01:25 +0530
Subject: [PATCH] "Reland "[Wunsafe-buffer-usage] Fix false positive when const
sized array is indexed by const evaluatable expressions (#119340)""
This reverts commit 7dd34baf5505d689161c3a8678322a394d7a2929.
Fixed the assertion violation reported by 7dd34baf5505d689161c3a8678322a394d7a2929
---
clang/lib/Analysis/UnsafeBufferUsage.cpp | 9 ++++--
.../warn-unsafe-buffer-usage-array.cpp | 32 +++++++++++++++++++
2 files changed, 39 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Analysis/UnsafeBufferUsage.cpp b/clang/lib/Analysis/UnsafeBufferUsage.cpp
index a9aff39df64746..c064aa30e8aedc 100644
--- a/clang/lib/Analysis/UnsafeBufferUsage.cpp
+++ b/clang/lib/Analysis/UnsafeBufferUsage.cpp
@@ -453,8 +453,13 @@ AST_MATCHER(ArraySubscriptExpr, isSafeArraySubscript) {
return false;
}
- if (const auto *IdxLit = dyn_cast<IntegerLiteral>(Node.getIdx())) {
- const APInt ArrIdx = IdxLit->getValue();
+ Expr::EvalResult EVResult;
+ const Expr *IndexExpr = Node.getIdx();
+ if (!IndexExpr->isValueDependent() &&
+ IndexExpr->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 (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 7dd6c83dbba2a8..e80b54b7c69677 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
@@ -92,3 +92,35 @@ 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 at +2 {{change type of 'arr' to 'std::array' to label it for hardening}}
+ // expected-warning at +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)];
+}
More information about the cfe-commits
mailing list