Skip to content

Commit 8ae0d74

Browse files
PiotrZSLyuxuanchen1997
authored andcommitted
[clang-tidy] Fix crash in C language in readability-non-const-parameter (#100461)
Summary: Fix crash that happen when redeclaration got different number of parameters than definition. Fixes #100340 Test Plan: Reviewers: Subscribers: Tasks: Tags: Differential Revision: https://phabricator.intern.facebook.com/D60250525
1 parent 6902e52 commit 8ae0d74

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,9 +157,12 @@ void NonConstParameterCheck::diagnoseNonConstParameters() {
157157
if (!Function)
158158
continue;
159159
unsigned Index = Par->getFunctionScopeIndex();
160-
for (FunctionDecl *FnDecl : Function->redecls())
160+
for (FunctionDecl *FnDecl : Function->redecls()) {
161+
if (FnDecl->getNumParams() <= Index)
162+
continue;
161163
Fixes.push_back(FixItHint::CreateInsertion(
162164
FnDecl->getParamDecl(Index)->getBeginLoc(), "const "));
165+
}
163166

164167
diag(Par->getLocation(), "pointer parameter '%0' can be pointer to const")
165168
<< Par->getName() << Fixes;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// RUN: %check_clang_tidy %s readability-non-const-parameter %t
2+
3+
static int f();
4+
5+
int f(p)
6+
int *p;
7+
// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: pointer parameter 'p' can be pointer to const [readability-non-const-parameter]
8+
// CHECK-FIXES: {{^}} const int *p;{{$}}
9+
{
10+
return *p;
11+
}

0 commit comments

Comments
 (0)