Skip to content

Commit e2344f6

Browse files
committed
[-Wunsafe-buffer-usage] Emit fixits for arguments of function pointers calls
Currently we ignore calls on function pointers (unlike direct calls of functions and class methods). This patch adds support for function pointers as well. The change is to simply replace use of forEachArgumentWithParam matcher in UPC gadget with forEachArgumentWithParamType. from the documentation of forEachArgumentWithParamType: /// Matches all arguments and their respective types for a \c CallExpr or /// \c CXXConstructExpr. It is very similar to \c forEachArgumentWithParam but /// it works on calls through function pointers as well.
1 parent ff51c1a commit e2344f6

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

clang/lib/Analysis/UnsafeBufferUsage.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,8 @@ isInUnspecifiedPointerContext(internal::Matcher<Stmt> InnerMatcher) {
281281
// (i.e., computing the distance between two pointers); or ...
282282

283283
auto CallArgMatcher =
284-
callExpr(forEachArgumentWithParam(InnerMatcher,
285-
hasPointerType() /* array also decays to pointer type*/),
284+
callExpr(forEachArgumentWithParamType(InnerMatcher,
285+
isAnyPointer() /* array also decays to pointer type*/),
286286
unless(callee(functionDecl(hasAttr(attr::UnsafeBufferUsage)))));
287287

288288
auto CastOperandMatcher =
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang_cc1 -std=c++20 -Wunsafe-buffer-usage \
2+
// RUN: -fsafe-buffer-usage-suggestions \
3+
// RUN: -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
4+
5+
void unsafe_array_func_ptr_call(void (*fn_ptr)(int *param)) {
6+
int p[32];
7+
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:3-[[@LINE-1]]:12}:"std::array<int, 32> p"
8+
9+
int tmp = p[5];
10+
fn_ptr(p);
11+
// CHECK-DAG: fix-it:"{{.*}}":{[[@LINE-1]]:11-[[@LINE-1]]:11}:".data()"
12+
}

0 commit comments

Comments
 (0)