[clang] [Wunsafe-buffer-usage] Add additional tests to esnure safe accesses to const sized array are not warned (PR #125483)
via cfe-commits
cfe-commits at lists.llvm.org
Mon Feb 3 03:36:46 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang
Author: Malavika Samak (malavikasamak)
<details>
<summary>Changes</summary>
Add more tests, where the index of the const array access evaluates to a constant and depends on a template argument.
rdar://143759014
---
Full diff: https://github.com/llvm/llvm-project/pull/125483.diff
1 Files Affected:
- (modified) clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp (+33)
``````````diff
diff --git a/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp b/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
index e80b54b7c69677..03a7ae5c7a8fb5 100644
--- a/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
+++ b/clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp
@@ -124,3 +124,36 @@ void array_indexed_const_expr(unsigned idx) {
k = arr[get_const(5)]; // expected-note {{used in buffer access here}}
k = arr[get_const(4)];
}
+
+template<unsigned length>
+consteval bool isNullTerminated(const char (&literal)[length])
+{
+ return literal[length - 1] == '\0';
+}
+
+template <typename T, unsigned M, unsigned N>
+T access2DArray(const T (&arr)[M][N]) {
+ return arr[M-1][N-1];
+}
+
+template<unsigned idx>
+constexpr int access_elements() {
+ int arr[idx + 20];
+ return arr[idx + 1];
+}
+
+void test_template_methods()
+{
+ constexpr char arr[] = "Good Morning!"; // = {'a', 'b', 'c', 'd', 'e'};
+ isNullTerminated(arr);
+ isNullTerminated("");
+ auto _ = isNullTerminated("hello world\n");
+ access_elements<5>();
+
+ int arr1[3][4] = {
+ {1, 2, 3, 4},
+ {5, 6, 7, 8},
+ {9, 10, 11, 12}
+ };
+ access2DArray(arr1);
+}
``````````
</details>
https://github.com/llvm/llvm-project/pull/125483
More information about the cfe-commits
mailing list