Skip to content

Conversation

@zahiraam
Copy link
Contributor

@zahiraam zahiraam commented Dec 3, 2024

If the range for llvm::any_of is empty, Idx will be 0 and an underflow might occur when computing Idx-1.

@zahiraam zahiraam marked this pull request as ready for review December 3, 2024 20:05
@zahiraam zahiraam requested a review from AaronBallman December 3, 2024 20:05
@llvmbot
Copy link
Member

llvmbot commented Dec 3, 2024

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

@llvm/pr-subscribers-clang-tidy

Author: Zahira Ammarguellat (zahiraam)

Changes

If the range for llvm::any_of is empty, Idx will be 0 and an underflow might occur when computing Idx-1.


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

1 Files Affected:

  • (modified) clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp (+4-4)
diff --git a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
index 8eaf54fe0088a4..ce307a2384aef7 100644
--- a/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
+++ b/clang-tools-extra/clang-tidy/bugprone/CrtpConstructorAccessibilityCheck.cpp
@@ -58,10 +58,10 @@ getDerivedParameter(const ClassTemplateSpecializationDecl *CRTP,
                Arg.getAsType()->getAsCXXRecordDecl() == Derived;
       });
 
-  return AnyOf ? CRTP->getSpecializedTemplate()
-                     ->getTemplateParameters()
-                     ->getParam(Idx - 1)
-               : nullptr;
+  return AnyOf && Idx > 0 ? CRTP->getSpecializedTemplate()
+                                ->getTemplateParameters()
+                                ->getParam(Idx - 1)
+                          : nullptr;
 }
 
 static std::vector<FixItHint>

->getTemplateParameters()
->getParam(Idx - 1)
: nullptr;
return AnyOf && Idx > 0 ? CRTP->getSpecializedTemplate()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it's possible for AnyOf to be true and Idx to be 0 at the same time. Idx starts as zero, but the call to any_of on line 54 has a lambda which explicitly does ++Idx, so any_of cannot return true without incrementing Idx.

If this came from a static analysis tool, I would claim it's a false positive that doesn't require changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Even if the range is empty Idx will be incremented?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the range is empty, then Idx is not incremented, but AnyOf will be false.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Closing the PR then. Thanks.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the noise.

@zahiraam
Copy link
Contributor Author

zahiraam commented Dec 4, 2024

_ No description provided. _

@zahiraam zahiraam closed this Dec 4, 2024
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.

3 participants