Skip to content

Commit 3ea4f5d

Browse files
committed
[clang-tidy] bugprone-exception-escape didn't detech catching of an exception with pointer type by void * exception handler
1 parent f9dd885 commit 3ea4f5d

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

clang-tools-extra/clang-tidy/utils/ExceptionAnalyzer.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,10 @@ bool isStandardPointerConvertible(QualType From, QualType To) {
141141
if (RD->isCompleteDefinition() &&
142142
isBaseOf(From->getPointeeType().getTypePtr(),
143143
To->getPointeeType().getTypePtr())) {
144-
return true;
144+
// If B is an inaccessible or ambiguous base class of D, a program
145+
// that necessitates this conversion is ill-formed
146+
return isUnambiguousPublicBaseClass(From->getPointeeType().getTypePtr(),
147+
To->getPointeeType().getTypePtr());
145148
}
146149
}
147150

@@ -375,10 +378,7 @@ bool ExceptionAnalyzer::ExceptionInfo::filterByCatch(
375378
isPointerOrPointerToMember(ExceptionCanTy->getTypePtr())) {
376379
// A standard pointer conversion not involving conversions to pointers to
377380
// private or protected or ambiguous classes ...
378-
if (isStandardPointerConvertible(ExceptionCanTy, HandlerCanTy) &&
379-
isUnambiguousPublicBaseClass(
380-
ExceptionCanTy->getTypePtr()->getPointeeType().getTypePtr(),
381-
HandlerCanTy->getTypePtr()->getPointeeType().getTypePtr())) {
381+
if (isStandardPointerConvertible(ExceptionCanTy, HandlerCanTy)) {
382382
TypesToDelete.push_back(ExceptionTy);
383383
}
384384
// A function pointer conversion ...

clang-tools-extra/docs/ReleaseNotes.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -518,6 +518,10 @@ Changes in existing checks
518518
usages of ``std::string_view::compare``. Added a `StringLikeClasses` option
519519
to detect usages of ``compare`` method in custom string-like classes.
520520

521+
- Improved :doc:`exception-escape <clang-tidy/checks/bugprone/exception-escape>`
522+
check to correctly detect exception handler of type `CV void *` as catching all
523+
`CV` compatible pointer types.
524+
521525
Removed checks
522526
^^^^^^^^^^^^^^
523527

clang-tools-extra/test/clang-tidy/checkers/bugprone/exception-escape.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -756,3 +756,21 @@ struct test_implicit_throw {
756756
};
757757

758758
}}
759+
760+
void pointer_exception_can_not_escape_with_const_void_handler() noexcept {
761+
// CHECK-MESSAGES-NOT: :[[@LINE-1]]:6: warning: an exception may be thrown in function 'pointer_exception_can_not_escape_with_const_void_handler' which should not throw exceptions
762+
const int value = 42;
763+
try {
764+
throw &value;
765+
} catch (const void *) {
766+
}
767+
}
768+
769+
void pointer_exception_can_not_escape_with_void_handler() noexcept {
770+
// CHECK-MESSAGES-NOT: :[[@LINE-1]]:6: warning: an exception may be thrown in function 'pointer_exception_can_not_escape_with_void_handler' which should not throw exceptions
771+
int value = 42;
772+
try {
773+
throw &value;
774+
} catch (void *) {
775+
}
776+
}

0 commit comments

Comments
 (0)