Skip to content

Commit 12dc35b

Browse files
author
MalavikaSamak
committed
[Wunsafe-buffer-usage] Add additional tests to esnure safe accesses to const sized array are not warned
Add more tests, where the index of the const array access evaluates to a constant and depends on a template argument. rdar://143759014
1 parent 76e73ae commit 12dc35b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

clang/test/SemaCXX/warn-unsafe-buffer-usage-array.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,3 +124,36 @@ void array_indexed_const_expr(unsigned idx) {
124124
k = arr[get_const(5)]; // expected-note {{used in buffer access here}}
125125
k = arr[get_const(4)];
126126
}
127+
128+
template<unsigned length>
129+
consteval bool isNullTerminated(const char (&literal)[length])
130+
{
131+
return literal[length - 1] == '\0';
132+
}
133+
134+
template <typename T, unsigned M, unsigned N>
135+
T access2DArray(const T (&arr)[M][N]) {
136+
return arr[M-1][N-1];
137+
}
138+
139+
template<unsigned idx>
140+
constexpr int access_elements() {
141+
int arr[idx + 20];
142+
return arr[idx + 1];
143+
}
144+
145+
void test_template_methods()
146+
{
147+
constexpr char arr[] = "Good Morning!"; // = {'a', 'b', 'c', 'd', 'e'};
148+
isNullTerminated(arr);
149+
isNullTerminated("");
150+
auto _ = isNullTerminated("hello world\n");
151+
access_elements<5>();
152+
153+
int arr1[3][4] = {
154+
{1, 2, 3, 4},
155+
{5, 6, 7, 8},
156+
{9, 10, 11, 12}
157+
};
158+
access2DArray(arr1);
159+
}

0 commit comments

Comments
 (0)