[llvm-branch-commits] [clang-tools-extra] release/23.x: [clang-tidy] Fix crashes when analyzing unknown exceptions in bugprone-exception-escape (#218067) (PR #218128)
Douglas Yung via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Sun Aug 23 12:13:32 PDT 2026
https://github.com/dyung updated https://github.com/llvm/llvm-project/pull/218128
>From 9a30d9b392edd515458a6d2bab5a07462d6ae68d Mon Sep 17 00:00:00 2001
From: Zeyi Xu <mitchell.xu2 at gmail.com>
Date: Sat, 22 Aug 2026 21:09:21 +0800
Subject: [PATCH] [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
(cherry picked from commit c0feba8bedbfda4f1a2f3727f2ef270fcc6ca138)
---
.../clang-tidy/utils/ExceptionAnalyzer.cpp | 4 ++
.../exception-escape-coro-unknown.cpp | 42 +++++++++++++++++++
...ions-without-specification-as-throwing.cpp | 18 +++++++-
3 files changed, 63 insertions(+), 1 deletion(-)
create mode 100644 clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape-coro-unknown.cpp
diff --git a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
index af18fda7a3a54..3224aa0a857be 100644
--- a/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
+++ b/clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp
@@ -365,6 +365,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();
@@ -612,6 +614,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 llvm-branch-commits
mailing list