[clang] [clang][Sema] Fix assertion in `tryDiagnoseOverloadedCast` (PR #108021)
Alejandro Álvarez Ayllón via cfe-commits
cfe-commits at lists.llvm.org
Wed Sep 18 00:25:41 PDT 2024
https://github.com/alejandro-alvarez-sonarsource updated https://github.com/llvm/llvm-project/pull/108021
>From b414b6e184fc566efd6a2dbe631ee68a25b461a1 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?=
<alejandro.alvarez at sonarsource.com>
Date: Fri, 16 Jun 2023 18:01:33 +0200
Subject: [PATCH 1/3] [clang][Sema] Fix wrong assumption in
`tryDiagnoseOverloadedCast`
A constructor may have failed to be used due to a broken templated
dependent parameter. The `InitializationSequence` itself can
succeed.
Due to the assumption that it is in a failed state, it can
either cause an assertion failure, or undefined behavior in release
mode, since `sequence.Failure` is not initialized.
---
clang/lib/Sema/SemaCast.cpp | 7 ++++-
.../cxx-bad-cast-diagnose-broken-template.cpp | 31 +++++++++++++++++++
2 files changed, 37 insertions(+), 1 deletion(-)
create mode 100644 clang/test/Parser/cxx-bad-cast-diagnose-broken-template.cpp
diff --git a/clang/lib/Sema/SemaCast.cpp b/clang/lib/Sema/SemaCast.cpp
index f01b22a72915c8..6ac6201843476b 100644
--- a/clang/lib/Sema/SemaCast.cpp
+++ b/clang/lib/Sema/SemaCast.cpp
@@ -446,7 +446,12 @@ static bool tryDiagnoseOverloadedCast(Sema &S, CastType CT,
: InitializationKind::CreateCast(/*type range?*/ range);
InitializationSequence sequence(S, entity, initKind, src);
- assert(sequence.Failed() && "initialization succeeded on second try?");
+ // It could happen that a constructor failed to be used because
+ // it requires a temporary of a broken type. Still, it will be found when
+ // looking for a match.
+ if (!sequence.Failed())
+ return false;
+
switch (sequence.getFailureKind()) {
default: return false;
diff --git a/clang/test/Parser/cxx-bad-cast-diagnose-broken-template.cpp b/clang/test/Parser/cxx-bad-cast-diagnose-broken-template.cpp
new file mode 100644
index 00000000000000..f9fc3e6082da10
--- /dev/null
+++ b/clang/test/Parser/cxx-bad-cast-diagnose-broken-template.cpp
@@ -0,0 +1,31 @@
+// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
+
+template< typename>
+struct IsConstCharArray
+{
+ static const bool value = false;
+};
+
+template< int N >
+struct IsConstCharArray< const char[ N ] >
+{
+ typedef char CharType;
+ static const bool value = true;
+ static const missing_int_t length = N - 1; // expected-error {{unknown type name 'missing_int_t'}}
+};
+
+class String {
+public:
+ template <typename T>
+ String(T& str, typename IsConstCharArray<T>::CharType = 0);
+};
+
+
+class Exception {
+public:
+ Exception(String const&);
+};
+
+void foo() {
+ throw Exception("some error"); // expected-error {{functional-style cast from 'const char[11]' to 'Exception' is not allowed}}
+}
>From 8658c333ff6700b9f2f5facbdeb26fc2931c2aae Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?=
<alejandro.alvarez at sonarsource.com>
Date: Fri, 13 Sep 2024 11:28:42 +0200
Subject: [PATCH 2/3] Add release note
---
clang/docs/ReleaseNotes.rst | 2 ++
1 file changed, 2 insertions(+)
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index c4fa017b982bbd..7b6ba3444288aa 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -384,6 +384,8 @@ Bug Fixes to C++ Support
- Fixed a crash in the typo correction of an invalid CTAD guide. (#GH107887)
- Fixed a crash when clang tries to subtitute parameter pack while retaining the parameter
pack. #GH63819, #GH107560
+- Fixed an assertion failure in debug mode, and potential crashes in release mode, when
+ diagnosing a failed cast caused indirectly by a failed implicit conversion to the type of the constructor parameter.
Bug Fixes to AST Handling
>From d5f47ff46b9af1483e2984a25ce4f65015a5c84e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Alejandro=20=C3=81lvarez=20Ayll=C3=B3n?=
<alejandro.alvarez at sonarsource.com>
Date: Fri, 13 Sep 2024 11:41:58 +0200
Subject: [PATCH 3/3] Sync test with PR
---
.../cxx-bad-cast-diagnose-broken-template.cpp | 15 +++++----------
1 file changed, 5 insertions(+), 10 deletions(-)
diff --git a/clang/test/Parser/cxx-bad-cast-diagnose-broken-template.cpp b/clang/test/Parser/cxx-bad-cast-diagnose-broken-template.cpp
index f9fc3e6082da10..3500975d936953 100644
--- a/clang/test/Parser/cxx-bad-cast-diagnose-broken-template.cpp
+++ b/clang/test/Parser/cxx-bad-cast-diagnose-broken-template.cpp
@@ -1,23 +1,18 @@
// RUN: %clang_cc1 -fcxx-exceptions -fexceptions -fsyntax-only -verify %s
-template< typename>
-struct IsConstCharArray
-{
- static const bool value = false;
-};
+template<typename>
+struct StringTrait {};
template< int N >
-struct IsConstCharArray< const char[ N ] >
-{
+struct StringTrait< const char[ N ] > {
typedef char CharType;
- static const bool value = true;
- static const missing_int_t length = N - 1; // expected-error {{unknown type name 'missing_int_t'}}
+ static const MissingIntT length = N - 1; // expected-error {{unknown type name 'MissingIntT'}}
};
class String {
public:
template <typename T>
- String(T& str, typename IsConstCharArray<T>::CharType = 0);
+ String(T& str, typename StringTrait<T>::CharType = 0);
};
More information about the cfe-commits
mailing list