Skip to content

Commit b45f752

Browse files
PiotrZSLtru
authored andcommitted
[clang-tidy] Fix crash in C language in readability-non-const-parameter (llvm#100461)
Fix crash that happen when redeclaration got different number of parameters than definition. Fixes llvm#100340 (cherry picked from commit a27f816)
1 parent 90f2d48 commit b45f752

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-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;

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -496,6 +496,10 @@ Changes in existing checks
496496
``static_cast``. Fixed false positives in C++20 spaceship operator by ignoring
497497
casts in implicit and defaulted functions.
498498

499+
- Improved :doc:`readability-non-const-parameter
500+
<clang-tidy/checks/readability/non-const-parameter>` check to not crash when
501+
redeclaration have fewer parameters than expected.
502+
499503
- Improved :doc:`readability-redundant-inline-specifier
500504
<clang-tidy/checks/readability/redundant-inline-specifier>` check to properly
501505
emit warnings for static data member with an in-class initializer.
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)