-
Notifications
You must be signed in to change notification settings - Fork 10.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[-Wunsafe-buffer-usage] Add fixits for array to pointer assignment (#…
…81343) 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.
- Loading branch information
1 parent
a7982d5
commit 6fce42f
Showing
4 changed files
with
163 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
45 changes: 45 additions & 0 deletions
45
clang/test/SemaCXX/warn-unsafe-buffer-usage-fixits-array-assign-to-ptr.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \ | ||
// RUN: -fsafe-buffer-usage-suggestions \ | ||
// RUN: -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s | ||
|
||
void safe_array_assigned_to_safe_ptr(unsigned idx) { | ||
int buffer[10]; | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]: | ||
int* ptr; | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]: | ||
ptr = buffer; | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]: | ||
} | ||
|
||
void safe_array_assigned_to_unsafe_ptr(unsigned idx) { | ||
int buffer[10]; | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]: | ||
int* ptr; | ||
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:11}:"std::span<int> ptr" | ||
ptr = buffer; | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]: | ||
ptr[idx] = 0; | ||
} | ||
|
||
void unsafe_array_assigned_to_safe_ptr(unsigned idx) { | ||
int buffer[10]; | ||
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:17}:"std::array<int, 10> buffer" | ||
int* ptr; | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]: | ||
ptr = buffer; | ||
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:15-[[@LINE-1]]:15}:".data()" | ||
buffer[idx] = 0; | ||
} | ||
|
||
// FIXME: Implement fixit/s for this case. | ||
// See comment in CArrayToPtrAssignmentGadget::getFixits to learn why this hasn't been implemented. | ||
void unsafe_array_assigned_to_unsafe_ptr(unsigned idx) { | ||
int buffer[10]; | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:{{.*}} | ||
int* ptr; | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:{{.*}} | ||
ptr = buffer; | ||
// CHECK-NOT: fix-it:"{{.*}}":{[[@LINE-1]]:{{.*}} | ||
buffer[idx] = 0; | ||
ptr[idx] = 0; | ||
} |