]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
libstdc++: Prevent unwanted ADL in std::to_array [PR111512]
authorJonathan Wakely <jwakely@redhat.com>
Thu, 21 Sep 2023 08:14:57 +0000 (09:14 +0100)
committerJonathan Wakely <jwakely@redhat.com>
Mon, 25 Sep 2023 08:48:44 +0000 (09:48 +0100)
As noted in PR c++/111512, GCC does ADL for __builtin_memcpy if it is
unqualified, which can cause errors for template argument types which
cannot be completed.

Casting the memcpy arguments to void* prevents ADL from considering the
problem type.

libstdc++-v3/ChangeLog:

PR libstdc++/111511
PR c++/111512
* include/std/array (to_array): Cast memcpy arguments to void*.
* testsuite/23_containers/array/creation/111512.cc: New test.

libstdc++-v3/include/std/array
libstdc++-v3/testsuite/23_containers/array/creation/111512.cc [new file with mode: 0644]

index 0e32d7b52d01fab77339bbea94de9eca691a5682..c4d534c3a3496a8bec7522afb55cda3500c22654 100644 (file)
@@ -432,7 +432,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
            {
              array<remove_cv_t<_Tp>, _Nm> __arr;
              if (!__is_constant_evaluated() && _Nm != 0)
-               __builtin_memcpy(__arr.data(), __a, sizeof(__a));
+               __builtin_memcpy((void*)__arr.data(), (void*)__a, sizeof(__a));
              else
                for (size_t __i = 0; __i < _Nm; ++__i)
                  __arr._M_elems[__i] = __a[__i];
@@ -461,7 +461,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
            {
              array<remove_cv_t<_Tp>, _Nm> __arr;
              if (!__is_constant_evaluated() && _Nm != 0)
-               __builtin_memcpy(__arr.data(), __a, sizeof(__a));
+               __builtin_memcpy((void*)__arr.data(), (void*)__a, sizeof(__a));
              else
                for (size_t __i = 0; __i < _Nm; ++__i)
                  __arr._M_elems[__i] = __a[__i];
diff --git a/libstdc++-v3/testsuite/23_containers/array/creation/111512.cc b/libstdc++-v3/testsuite/23_containers/array/creation/111512.cc
new file mode 100644 (file)
index 0000000..b0f25a6
--- /dev/null
@@ -0,0 +1,24 @@
+// { dg-do compile { target c++20 } }
+
+// Bug libstdc++/111511 - Incorrect ADL in std::to_array in GCC 11/12/13
+// Bug c++/111512 - GCC's __builtin_memcpy can trigger ADL
+
+#include <array>
+#include <utility>
+
+struct incomplete;
+
+template<class T>
+struct holder {
+    T t; // { dg-bogus "'holder<T>::t' has incomplete type" }
+};
+
+// A complete type that cannot be used as an associated type for ADL.
+using adl_bomb = holder<incomplete>*;
+
+int main()
+{
+    adl_bomb a[1]{};
+    (void) std::to_array(a);
+    (void) std::to_array(std::move(a));
+}