[clang-tools-extra] c0feba8 - [clang-tidy] Fix crashes when analyzing unknown exceptions in bugprone-exception-escape (#218067)
via cfe-commits
cfe-commits at lists.llvm.org
Sat Aug 22 06:09:27 PDT 2026
Author: Zeyi Xu
Date: 2026-08-22T13:09:21Z
New Revision: c0feba8bedbfda4f1a2f3727f2ef270fcc6ca138
URL: https://github.com/llvm/llvm-project/commit/c0feba8bedbfda4f1a2f3727f2ef270fcc6ca138
DIFF: https://github.com/llvm/llvm-project/commit/c0feba8bedbfda4f1a2f3727f2ef270fcc6ca138.diff
LOG: [clang-tidy] Fix crashes when analyzing unknown exceptions in bugprone-exception-escape (#218067)
`ExceptionAnalyzer` represents exceptions of unknown type with a null
`Type`, but some consumers dereferenced it unconditionally, resulting in
a crash when `TreatFunctionsWithoutSpecificationAsThrowing` is enabled.
This commit fixes the problem by skipping type-dependent processing for
unknown exceptions.
Fixes #217649
Added:
clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-coro-unknown.cpp
Modified:
clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp
Removed:
################################################################################
diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
index 7decd18dd25b9..f414272ba15bd 100644
--- a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
+++ b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
@@ -362,6 +362,8 @@ ExceptionAnalyzer::ExceptionInfo::filterByCatch(const Type *HandlerTy,
SmallVector<const Type *, 8> TypesToDelete;
for (const auto &ThrownException : ThrownExceptions) {
const Type *ExceptionTy = ThrownException.getFirst();
+ if (!ExceptionTy)
+ continue;
const CanQualType ExceptionCanTy =
ExceptionTy->getCanonicalTypeUnqualified();
const CanQualType HandlerCanTy = HandlerTy->getCanonicalTypeUnqualified();
@@ -607,6 +609,8 @@ ExceptionAnalyzer::throwsException(const Stmt *St,
Excs.getExceptions(), CallStack));
for (const auto &Exception : Excs.getExceptions()) {
const Type *ExcType = Exception.getFirst();
+ if (!ExcType)
+ continue;
if (const CXXRecordDecl *ThrowableRec = ExcType->getAsCXXRecordDecl()) {
const ExceptionInfo DestructorExcs = throwsException(
ThrowableRec->getDestructor(), Caught, CallStack, SourceLocation{});
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-coro-unknown.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-coro-unknown.cpp
new file mode 100644
index 0000000000000..a2325c401c404
--- /dev/null
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-coro-unknown.cpp
@@ -0,0 +1,42 @@
+// RUN: %check_clang_tidy -std=c++20-or-later %s bugprone-exception-escape %t -- \
+// RUN: -config='{"CheckOptions": { \
+// RUN: "bugprone-exception-escape.TreatFunctionsWithoutSpecificationAsThrowing": "OnlyUndefined" \
+// RUN: }}' -- -fexceptions
+
+namespace std {
+
+template <class Ret, typename... T> struct coroutine_traits {
+ using promise_type = typename Ret::promise_type;
+};
+
+template <class Promise = void> struct coroutine_handle {
+ template <class OtherPromise>
+ coroutine_handle(coroutine_handle<OtherPromise>) noexcept;
+ static coroutine_handle from_address(void *) noexcept;
+};
+
+struct suspend_never {
+ bool await_ready() noexcept { return true; }
+ void await_suspend(coroutine_handle<>) noexcept {}
+ void await_resume() noexcept {}
+};
+
+} // namespace std
+
+struct Task {
+ struct promise_type {
+ Task get_return_object() noexcept { return {}; }
+ std::suspend_never initial_suspend() noexcept { return {}; }
+ std::suspend_never final_suspend() noexcept { return {}; }
+ void return_void() noexcept {}
+ void unhandled_exception() noexcept {}
+ };
+};
+
+void undefined();
+
+Task calls_undefined() noexcept {
+ // CHECK-MESSAGES: :[[@LINE-1]]:6: warning: an exception may be thrown in function 'calls_undefined' which should not throw exceptions
+ undefined();
+ co_return;
+}
diff --git a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp
index ee9f19b5a2896..14c280a5901d6 100644
--- a/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp
+++ b/clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-treat-functions-without-specification-as-throwing.cpp
@@ -37,13 +37,29 @@ void calls_unknown() noexcept {
extern_declared();
}
+struct Error {};
+
void calls_unknown_caught() noexcept {
// CHECK-MESSAGES-ALL-NOT: warning:
// CHECK-MESSAGES-UNDEFINED-NOT: warning:
// CHECK-MESSAGES-NONE-NOT: warning:
try {
extern_declared();
- } catch(...) {
+ } catch (const Error &) {
+ } catch (...) {
+ }
+}
+
+void calls_unknown_typed_catch() noexcept {
+ // CHECK-MESSAGES-ALL: :[[@LINE-1]]:6: warning: an exception may be thrown in function 'calls_unknown_typed_catch' which should not throw exceptions
+ // CHECK-MESSAGES-ALL: :[[@LINE-28]]:6: note: frame #0: an exception of unknown type may be thrown in function 'extern_declared' here
+ // CHECK-MESSAGES-ALL: :[[@LINE+5]]:5: note: frame #1: function 'calls_unknown_typed_catch' calls function 'extern_declared' here
+ // CHECK-MESSAGES-UNDEFINED: :[[@LINE-4]]:6: warning: an exception may be thrown in function 'calls_unknown_typed_catch' which should not throw exceptions
+ // CHECK-MESSAGES-UNDEFINED: :[[@LINE-31]]:6: note: frame #0: an exception of unknown type may be thrown in function 'extern_declared' here
+ // CHECK-MESSAGES-UNDEFINED: :[[@LINE+2]]:5: note: frame #1: function 'calls_unknown_typed_catch' calls function 'extern_declared' here
+ try {
+ extern_declared();
+ } catch (const Error &) {
}
}
More information about the cfe-commits
mailing list