Skip to content

Commit dfc9d95

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. Currently the matcher also uses hasPointerType() which checks that the canonical type of an argument is pointer and won't match on arrays decayed to pointer. Replacing hasPointerType() with isAnyPointerType() which allows implicit casts allows for the arrays to be matched as well and this way we get fixits for array arguments to function pointer calls too.
1 parent e06f352 commit dfc9d95

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
@@ -282,8 +282,8 @@ isInUnspecifiedPointerContext(internal::Matcher<Stmt> InnerMatcher) {
282282
// (i.e., computing the distance between two pointers); or ...
283283

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

289289
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)