Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-tidy] Fix crash in C language in readability-non-const-parameter #100461

Conversation

PiotrZSL
Copy link
Member

Fix crash that happen when redeclaration got
different number of parameters than definition.

No release notes, as this will be backported.

Fixes #100340

Fix crash that happen when redeclaration got
diffrent number of parameters than definition.

Fixes llvm#100340
@llvmbot
Copy link
Member

llvmbot commented Jul 24, 2024

@llvm/pr-subscribers-clang-tools-extra

@llvm/pr-subscribers-clang-tidy

Author: Piotr Zegar (PiotrZSL)

Changes

Fix crash that happen when redeclaration got
different number of parameters than definition.

No release notes, as this will be backported.

Fixes #100340


Full diff: https://github.com/llvm/llvm-project/pull/100461.diff

2 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp (+4-1)
  • (added) clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.c (+11)
diff --git a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
index 95a3a5165e2e8..43b69a24bdb16 100644
--- a/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
+++ b/clang-tools-extra/clang-tidy/readability/NonConstParameterCheck.cpp
@@ -157,9 +157,12 @@ void NonConstParameterCheck::diagnoseNonConstParameters() {
     if (!Function)
       continue;
     unsigned Index = Par->getFunctionScopeIndex();
-    for (FunctionDecl *FnDecl : Function->redecls())
+    for (FunctionDecl *FnDecl : Function->redecls()) {
+      if (FnDecl->getNumParams() <= Index)
+        continue;
       Fixes.push_back(FixItHint::CreateInsertion(
           FnDecl->getParamDecl(Index)->getBeginLoc(), "const "));
+    }
 
     diag(Par->getLocation(), "pointer parameter '%0' can be pointer to const")
         << Par->getName() << Fixes;
diff --git a/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.c b/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.c
new file mode 100644
index 0000000000000..db50467f3dd94
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/readability/non-const-parameter.c
@@ -0,0 +1,11 @@
+// RUN: %check_clang_tidy %s readability-non-const-parameter %t
+
+static int f();
+
+int f(p)
+  int *p;
+// CHECK-MESSAGES: :[[@LINE-1]]:8: warning: pointer parameter 'p' can be pointer to const [readability-non-const-parameter]
+// CHECK-FIXES: {{^}}  const int *p;{{$}}
+{
+    return *p;
+}

@PiotrZSL PiotrZSL merged commit a27f816 into llvm:main Jul 25, 2024
10 checks passed
yuxuanchen1997 pushed a commit that referenced this pull request Jul 25, 2024
…er (#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
@PiotrZSL PiotrZSL deleted the 100340-clang-tidy-crash-with-readability-non-const-parameter-and-kr-declarations branch August 1, 2024 13:30
llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 4, 2024
…er (llvm#100461)

Fix crash that happen when redeclaration got
different number of parameters than definition.

Fixes llvm#100340

(cherry picked from commit a27f816)
tru pushed a commit to llvmbot/llvm-project that referenced this pull request Aug 19, 2024
…er (llvm#100461)

Fix crash that happen when redeclaration got
different number of parameters than definition.

Fixes llvm#100340

(cherry picked from commit a27f816)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[clang-tidy] Crash with readability-non-const-parameter and K&R declarations
3 participants