-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[clang] fix crash when template with constructor attribute is instantiated without a priority #169282
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
Conversation
…iated without a priority The current implementation expects the priority argument to be provided to `[[gnu::constructor(<priority>)]]`, but the argument is really optional. This was causing a segfault when instantiating the function-template because we were trying to fold an `Expr*` that was a nullptr. This change skips the evaluation of the priority argument when it is missing; this will instantiate a function declaration with the default priority (65535).
|
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
|
@llvm/pr-subscribers-clang Author: Carson Radtke (carsonRadtke) Changesfixes: #169072 The current implementation expects the priority argument to be provided to This change skips the evaluation of the priority argument when it is missing; this will instantiate a function declaration with the default priority (65535). Full diff: https://github.com/llvm/llvm-project/pull/169282.diff 3 Files Affected:
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 439e47b209b2f..20e5a33ff7711 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -513,6 +513,8 @@ Bug Fixes to Attribute Support
- Fix handling of parameter indexes when an attribute is applied to a C++23 explicit object member function.
- Fixed several false positives and false negatives in function effect (`nonblocking`) analysis. (#GH166078) (#GH166101) (#GH166110)
- Fix ``cleanup`` attribute by delaying type checks until after the type is deduced. (#GH129631)
+- Fix a crash when instantiating a function template with ``constructor`` or ``destructor``
+ attributes without a priority argument. (#GH169072)
Bug Fixes to C++ Support
^^^^^^^^^^^^^^^^^^^^^^^^
diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
index 26693514bb278..e74c41517ecbf 100644
--- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
+++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp
@@ -245,15 +245,17 @@ static void sharedInstantiateConstructorDestructorAttr(
ExprResult Result = S.SubstExpr(A->getPriority(), TemplateArgs);
if (Result.isInvalid())
return;
- tempInstPriority = Result.get();
- if (std::optional<llvm::APSInt> CE =
- tempInstPriority->getIntegerConstantExpr(C)) {
- // Consistent with non-templated priority arguments, which must fit in a
- // 32-bit unsigned integer.
- if (!CE->isIntN(32)) {
- S.Diag(tempInstPriority->getExprLoc(), diag::err_ice_too_large)
- << toString(*CE, 10, false) << /*Size=*/32 << /*Unsigned=*/1;
- return;
+ if (Result.isUsable()) {
+ tempInstPriority = Result.get();
+ if (std::optional<llvm::APSInt> CE =
+ tempInstPriority->getIntegerConstantExpr(C)) {
+ // Consistent with non-templated priority arguments, which must fit in a
+ // 32-bit unsigned integer.
+ if (!CE->isIntN(32)) {
+ S.Diag(tempInstPriority->getExprLoc(), diag::err_ice_too_large)
+ << toString(*CE, 10, false) << /*Size=*/32 << /*Unsigned=*/1;
+ return;
+ }
}
}
}
diff --git a/clang/test/SemaTemplate/attributes.cpp b/clang/test/SemaTemplate/attributes.cpp
index 20fe983af28f7..d0ab0a68dec2a 100644
--- a/clang/test/SemaTemplate/attributes.cpp
+++ b/clang/test/SemaTemplate/attributes.cpp
@@ -640,3 +640,23 @@ namespace preferred_name {
Foo<1, 2, int, float>::nosuch x; // expected-error {{no type named 'nosuch' in 'preferred_name::Bar<int, float>'}}
}
::preferred_name::Foo<1, 2, int, float>::nosuch x; // expected-error {{no type named 'nosuch' in 'preferred_name::Bar<int, float>'}}
+
+// GH169072: templated attribute((constructor)) function crashes clang
+// constructor/destructor attribute without priority argument should not crash.
+namespace gh169072 {
+ template <typename T>
+ [[gnu::constructor]] void foo() {}
+
+ template void foo<int>();
+
+ template <typename T>
+ [[gnu::destructor]] void bar() {}
+
+ template void bar<int>();
+
+ // Also test with explicit priority argument
+ template <typename T>
+ [[gnu::constructor(101)]] void baz() {}
+
+ template void baz<int>();
+}
|
|
@carsonRadtke Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
|
LLVM Buildbot has detected a new failure on builder Full details are available at: https://lab.llvm.org/buildbot/#/builders/169/builds/17540 Here is the relevant piece of the build log for the reference |
fixes: #169072
The current implementation expects the priority argument to be provided to
[[gnu::constructor(<priority>)]], but the argument is really optional. This was causing a segfault when instantiating the function-template because we were trying to fold anExpr*that was a nullptr.This change skips the evaluation of the priority argument when it is missing; this will instantiate a function declaration with the default priority (65535).