Author: Younan Zhang Date: 2025-10-28T15:15:35+08:00 New Revision: 2984a8db804e29f31006a24f3ee033bd905894ee
URL: https://github.com/llvm/llvm-project/commit/2984a8db804e29f31006a24f3ee033bd905894ee DIFF: https://github.com/llvm/llvm-project/commit/2984a8db804e29f31006a24f3ee033bd905894ee.diff LOG: [Clang] Fix an iterator invalidation bug in concept normalization cache (#165352) The NormalizationCache may be inserted recursively when normalizing template arguments with non-dependent default arguments. Since the ADT doesn't preserve iterator validity, this caused undefined behavior. This is a regression on trunk so there is no release note. Fixes https://github.com/llvm/llvm-project/issues/165238 Added: Modified: clang/lib/Sema/SemaConcept.cpp clang/test/SemaTemplate/concepts.cpp Removed: ################################################################################ diff --git a/clang/lib/Sema/SemaConcept.cpp b/clang/lib/Sema/SemaConcept.cpp index f04cc454cdb7c..fb4d0b4582684 100644 --- a/clang/lib/Sema/SemaConcept.cpp +++ b/clang/lib/Sema/SemaConcept.cpp @@ -2408,11 +2408,16 @@ const NormalizedConstraint *Sema::getNormalizedAssociatedConstraints( if (CacheEntry == NormalizationCache.end()) { auto *Normalized = NormalizedConstraint::fromAssociatedConstraints( *this, ND, AssociatedConstraints); + if (!Normalized) { + NormalizationCache.try_emplace(ConstrainedDeclOrNestedReq, nullptr); + return nullptr; + } + // substitute() can invalidate iterators of NormalizationCache. + bool Failed = SubstituteParameterMappings(*this).substitute(*Normalized); CacheEntry = NormalizationCache.try_emplace(ConstrainedDeclOrNestedReq, Normalized) .first; - if (!Normalized || - SubstituteParameterMappings(*this).substitute(*Normalized)) + if (Failed) return nullptr; } return CacheEntry->second; diff --git a/clang/test/SemaTemplate/concepts.cpp b/clang/test/SemaTemplate/concepts.cpp index becf5467a1b61..c90af41a09468 100644 --- a/clang/test/SemaTemplate/concepts.cpp +++ b/clang/test/SemaTemplate/concepts.cpp @@ -1632,3 +1632,29 @@ void fn3() { } } + +namespace GH165238 { + +namespace std { +template <typename, typename _Tp> +concept output_iterator = requires(_Tp __t) { __t; }; +template <typename _Out> struct basic_format_context { + static_assert(output_iterator<_Out, int>); + using char_type = _Out; +}; +template <typename> class basic_format_parse_context; +template <typename, typename _Context, typename _Formatter, + typename = basic_format_parse_context<typename _Context::char_type>> +concept __parsable_with = requires(_Formatter __f) { __f; }; +template <typename _Tp, typename _CharT, + typename _Context = basic_format_context<_CharT>> +concept __formattable_impl = __parsable_with<_Tp, _Context, _Context>; +template <typename _Tp, typename _CharT> +concept formattable = __formattable_impl<_Tp, _CharT>; +} // namespace std +struct { + void operator()(std::formattable<char> auto); +} call; +void foo() { call(""); } + +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
