mirror of
https://github.com/llvm/llvm-project.git
synced 2025-04-18 09:26:41 +00:00

Introducing CArrayToPtrAssignment gadget and implementing fixits for some cases of array being assigned to pointer. Key observations: - const size array can be assigned to std::span and bounds are propagated - const size array can't be on LHS of assignment This means array to pointer assignment has no strategy implications. Fixits are implemented for cases where one of the variables in the assignment is safe. For assignment of a safe array to unsafe pointer we know that the RHS will never be transformed since it's safe and can immediately emit the optimal fixit. Similarly for assignment of unsafe array to safe pointer. (Obviously this is not and can't be future-proof in regards to what variables we consider unsafe and that is fine.) Fixits for assignment from unsafe array to unsafe pointer (from Array to Span strategy) are not implemented in this patch as that needs to be properly designed first - we might possibly implement optimal fixits for partially transformed cases, put both variables in a single fixit group or do something else.
99 lines
4.2 KiB
C++
99 lines
4.2 KiB
C++
// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
|
|
// RUN: -std=c++20 -verify=expected %s
|
|
// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
|
|
// RUN: -mllvm -debug-only=SafeBuffers \
|
|
// RUN: -std=c++20 -verify=expected,debug %s
|
|
|
|
// A generic -debug would also enable our notes. This is probably fine.
|
|
//
|
|
// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
|
|
// RUN: -std=c++20 -mllvm -debug \
|
|
// RUN: -verify=expected,debug %s
|
|
|
|
// This test file checks the behavior under the assumption that no fixits
|
|
// were emitted for the test cases. If -Wunsafe-buffer-usage is improved
|
|
// to support these cases (thus failing the test), the test should be changed
|
|
// to showcase a different unsupported example.
|
|
//
|
|
// RUN: %clang_cc1 -Wunsafe-buffer-usage -fsafe-buffer-usage-suggestions \
|
|
// RUN: -mllvm -debug-only=SafeBuffers \
|
|
// RUN: -std=c++20 -fdiagnostics-parseable-fixits %s \
|
|
// RUN: 2>&1 | FileCheck %s
|
|
// CHECK-NOT: fix-it:
|
|
|
|
// This debugging facility is only available in debug builds.
|
|
//
|
|
// REQUIRES: asserts
|
|
|
|
void foo() {
|
|
int *x = new int[10]; // expected-warning{{'x' is an unsafe pointer used for buffer access}}
|
|
x[5] = 10; // expected-note{{used in buffer access here}}
|
|
int z = x[-1]; // expected-note{{used in buffer access here}} \
|
|
// debug-note{{safe buffers debug: gadget 'ULCArraySubscript' refused to produce a fix}}
|
|
}
|
|
|
|
void failed_multiple_decl() {
|
|
int *a = new int[4], b; // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
|
|
// debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : multiple VarDecls}}
|
|
a[4] = 3; // expected-note{{used in buffer access here}}
|
|
}
|
|
|
|
void failed_param_var_decl(int *a =new int[3]) { // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
|
|
// debug-note{{safe buffers debug: failed to produce fixit for declaration 'a' : has default arg}}
|
|
a[4] = 6; // expected-note{{used in buffer access here}}
|
|
}
|
|
|
|
void unclaimed_use() {
|
|
int *a = new int[3]; // expected-warning{{'a' is an unsafe pointer used for buffer access}}
|
|
a[2] = 9; // expected-note{{used in buffer access here}}
|
|
int *b = a++; // expected-note{{used in pointer arithmetic here}} \
|
|
// debug-note{{safe buffers debug: failed to produce fixit for 'a' : has an unclaimed use}}
|
|
}
|
|
|
|
void implied_unclaimed_var(int *b) { // expected-warning{{'b' is an unsafe pointer used for buffer access}}
|
|
int *a = new int[3]; // expected-warning{{'a' is an unsafe pointer used for buffer access}}
|
|
a[4] = 7; // expected-note{{used in buffer access here}}
|
|
a = b; // debug-note{{safe buffers debug: gadget 'PtrToPtrAssignment' refused to produce a fix}}
|
|
b++; // expected-note{{used in pointer arithmetic here}} \
|
|
// debug-note{{safe buffers debug: failed to produce fixit for 'b' : has an unclaimed use}}
|
|
}
|
|
|
|
int *a = new int[3]; // expected-warning{{'a' is an unsafe pointer used for buffer access}} \
|
|
// debug-note{{safe buffers debug: failed to produce fixit for 'a' : neither local nor a parameter}}
|
|
void test_globals() {
|
|
a[7] = 4; // expected-note{{used in buffer access here}}
|
|
}
|
|
|
|
void test_decomp_decl() {
|
|
int a[2] = {1, 2};
|
|
auto [x, y] = a;
|
|
x = 9;
|
|
}
|
|
|
|
void test_claim_use_multiple() {
|
|
int *a = new int[8]; // expected-warning{{'a' is an unsafe pointer used for buffer access}}
|
|
a[6] = 9; // expected-note{{used in buffer access here}}
|
|
a++; // expected-note{{used in pointer arithmetic here}} \
|
|
// debug-note{{safe buffers debug: failed to produce fixit for 'a' : has an unclaimed use}}
|
|
}
|
|
|
|
struct S
|
|
{
|
|
int *x;
|
|
};
|
|
|
|
S f() { return S{new int[4]}; }
|
|
|
|
void test_struct_claim_use() {
|
|
auto [x] = f();
|
|
x[6] = 8; // expected-warning{{unsafe buffer access}}
|
|
x++; // expected-warning{{unsafe pointer arithmetic}}
|
|
}
|
|
|
|
void use(int*);
|
|
void array2d(int idx) {
|
|
int buffer[10][5]; // expected-warning{{'buffer' is an unsafe buffer that does not perform bounds checks}}
|
|
use(buffer[idx]); // expected-note{{used in buffer access here}} \
|
|
// debug-note{{safe buffers debug: failed to produce fixit for 'buffer' : has an unclaimed use}}
|
|
}
|