1 // RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
2 // RUN: -std=c++20 -verify=expected %s
3 // RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
4 // RUN: -mllvm -debug-only=SafeBuffers \
5 // RUN: -std=c++20 -verify=expected,debug %s
7 // A generic -debug would also enable our notes. This is probably fine.
9 // RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
10 // RUN: -std=c++20 -mllvm -debug \
11 // RUN: -verify=expected,debug %s
13 // This test file checks the behavior under the assumption that no fixits
14 // were emitted for the test cases. If -Wunsafe-buffer-usage is improved
15 // to support these cases (thus failing the test), the test should be changed
16 // to showcase a different unsupported example.
18 // RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
19 // RUN: -mllvm -debug-only=SafeBuffers \
20 // RUN: -std=c++20 -fdiagnostics-parseable-fixits %s \
21 // RUN: 2>&1 | FileCheck %s
24 // This debugging facility is only available in debug builds.
29 int *x
= new int[10]; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
30 x
[5] = 10; // expected-note{{used in buffer access here}}
31 int z
= x
[-1]; // expected-note{{used in buffer access here}} \
32 // debug-note{{safe buffers debug: gadget 'ULCArraySubscript' refused to produce a fix}}
36 int a
[10]; // expected-warning{{'a' is an unsafe buffer that does not perform bounds checks}} \
37 // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : not a pointer}}
39 for (int i
= 0; i
< 10; i
++) {
40 a
[i
] = i
; // expected-note{{used in buffer access here}}
44 void failed_multiple_decl() {
45 int *a
= new int[4], b
; // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
46 // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : multiple VarDecls}}
47 a
[4] = 3; // expected-note{{used in buffer access here}}
50 void failed_param_var_decl(int *a
=new int[3]) { // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
51 // debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : has default arg}}
52 a
[4] = 6; // expected-note{{used in buffer access here}}
55 void unclaimed_use() {
56 int *a
= new int[3]; // expected-warning{{'a' is an unsafe pointer used for buffer access}}
57 a
[2] = 9; // expected-note{{used in buffer access here}}
58 int *b
= a
++; // expected-note{{used in pointer arithmetic here}} \
59 // debug-note{{safe buffers debug: failed to produce fixit for 'a' : has an unclaimed use}}
62 void implied_unclaimed_var(int *b
) { // expected-warning{{'b' is an unsafe pointer used for buffer access}}
63 int *a
= new int[3]; // expected-warning{{'a' is an unsafe pointer used for buffer access}}
64 a
[4] = 7; // expected-note{{used in buffer access here}}
65 a
= b
; // debug-note{{safe buffers debug: gadget 'PointerAssignment' refused to produce a fix}}
66 b
++; // expected-note{{used in pointer arithmetic here}} \
67 // debug-note{{safe buffers debug: failed to produce fixit for 'b' : has an unclaimed use}}
70 int *a
= new int[3]; // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
71 // debug-note{{safe buffers debug: failed to produce fixit for 'a' : neither local nor a parameter}}
73 a
[7] = 4; // expected-note{{used in buffer access here}}
76 void test_decomp_decl() {
82 void test_claim_use_multiple() {
83 int *a
= new int[8]; // expected-warning{{'a' is an unsafe pointer used for buffer access}}
84 a
[6] = 9; // expected-note{{used in buffer access here}}
85 a
++; // expected-note{{used in pointer arithmetic here}} \
86 // debug-note{{safe buffers debug: failed to produce fixit for 'a' : has an unclaimed use}}
94 S
f() { return S
{new int[4]}; }
96 void test_struct_claim_use() {
98 x
[6] = 8; // expected-warning{{unsafe buffer access}}
99 x
++; // expected-warning{{unsafe pointer arithmetic}}