Skip to content

Commit 6357781

Browse files
authored
[clang-tidy] fix nullptr dereference in bugprone-forwarding-reference (#106856)
Previously, when checking if a `TemplateSpecializationType` is either `enable_if` or `enable_if_t`, the AST matcher would call `getTemplateName`, `getASTemplateDecl` and `getTemplatedDecl` in succession to check the `NamedDecl` returned from `getTemplatedDecl` is an `std::enable_if[_t]`. In the linked issue, the pointer returned by `getTemplatedDecl` is a `nullptr` that is unconditionally accessed, resulting in a crash. Instead, the checking is done on the `TemplateDecl` returned by `getASTemplateDecl`. Fixes #106333
1 parent 3e32e45 commit 6357781

File tree

3 files changed

+17
-8
lines changed

3 files changed

+17
-8
lines changed

clang-tools-extra/clang-tidy/bugprone/ForwardingReferenceOverloadCheck.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
#include "ForwardingReferenceOverloadCheck.h"
1010
#include "clang/AST/ASTContext.h"
1111
#include "clang/ASTMatchers/ASTMatchFinder.h"
12-
#include <algorithm>
1312

1413
using namespace clang::ast_matchers;
1514

@@ -19,14 +18,14 @@ namespace {
1918
// Check if the given type is related to std::enable_if.
2019
AST_MATCHER(QualType, isEnableIf) {
2120
auto CheckTemplate = [](const TemplateSpecializationType *Spec) {
22-
if (!Spec || !Spec->getTemplateName().getAsTemplateDecl()) {
21+
if (!Spec)
2322
return false;
24-
}
25-
const NamedDecl *TypeDecl =
26-
Spec->getTemplateName().getAsTemplateDecl()->getTemplatedDecl();
27-
return TypeDecl->isInStdNamespace() &&
28-
(TypeDecl->getName() == "enable_if" ||
29-
TypeDecl->getName() == "enable_if_t");
23+
24+
const TemplateDecl *TDecl = Spec->getTemplateName().getAsTemplateDecl();
25+
26+
return TDecl && TDecl->isInStdNamespace() &&
27+
(TDecl->getName() == "enable_if" ||
28+
TDecl->getName() == "enable_if_t");
3029
};
3130
const Type *BaseType = Node.getTypePtr();
3231
// Case: pointer or reference to enable_if.

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,10 @@ Changes in existing checks
111111
<clang-tidy/checks/bugprone/casting-through-void>` check to suggest replacing
112112
the offending code with ``reinterpret_cast``, to more clearly express intent.
113113

114+
- Improved :doc:`bugprone-forwarding-reference-overload
115+
<clang-tidy/checks/bugprone/forwarding-reference-overload>` check by fixing
116+
a crash when determining if an ``enable_if[_t]`` was found.
117+
114118
- Improved :doc:`cert-flp30-c<clang-tidy/checks/cert/flp30-c>` check to
115119
fix false positive that floating point variable is only used in increment
116120
expression.

clang-tools-extra/test/clang-tidy/checkers/bugprone/forwarding-reference-overload.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,3 +261,9 @@ class Test11 {
261261
Test11(const Test11 &) = default;
262262
Test11(Test11 &&) = default;
263263
};
264+
265+
template <template <class> typename T, typename U>
266+
struct gh106333
267+
{
268+
gh106333(U && arg1, T<int> arg2) {}
269+
};

0 commit comments

Comments
 (0)