[PATCH] PR libstdc++/71579 assert that type traits are not misused with an incomplete type

Antony Polukhin antoshkka@gmail.com
Thu Jun 20 17:49:00 GMT 2019


чт, 6 июн. 2019 г. в 15:19, Jonathan Wakely <jwakely@redhat.com>:
> I'm removing some of these assertions again, because they are either
> reundant or wrong.

Thanks for cleaning up!


In attachment there is an additional patch for type traits hardening.

Things that still remain unasserted are type traits  with variadic
template arguments. I have to came up with a proper solution for
providing a useful and lightweight diagnostics.

-- 
Best regards,
Antony Polukhin
-------------- next part --------------
diff --git a/libstdc++-v3/ChangeLog b/libstdc++-v3/ChangeLog
index ac98c0d..9063fe5 100644
--- a/libstdc++-v3/ChangeLog
+++ b/libstdc++-v3/ChangeLog
@@ -1,3 +1,28 @@
+2019-06-20  Antony Polukhin  <antoshkka@gmail.com>
+
+	PR libstdc++/71579
+	* include/std/type_traits (is_assignable, is_nothrow_assignable)
+	(is_trivially_assignable, is_nothrow_invocable_r): Add static_asserts
+	to make sure that the second argument of the type trait is not misused
+	with incomplete types.
+	(is_convertible, is_nothrow_convertible, is_swappable_with)
+	(is_nothrow_swappable_with): Add static_asserts to make sure that the
+	first and second arguments of the type trait are not misused with
+	incomplete types.
+	invoke_result: Add static_asserts to make sure that the first argument
+	of the type trait is not misused with incomplete types.
+	* testsuite/20_util/invoke_result/incomplete_neg.cc: New test.
+	* testsuite/20_util/is_assignable/incomplete_neg.cc: New test.
+	* testsuite/20_util/is_convertible/incomplete_neg.cc: New test.
+	* testsuite/20_util/is_nothrow_assignable/incomplete_neg.cc: New test.
+	* testsuite/20_util/is_nothrow_convertible/incomplete_neg.cc: New test.
+	* testsuite/20_util/is_nothrow_swappable/incomplete_neg.cc: New test.
+	* testsuite/20_util/is_nothrow_swappable_with/incomplete_neg.cc: New
+	test.
+	* testsuite/20_util/is_swappable/incomplete_neg.cc: New test.
+	* testsuite/20_util/is_swappable_with/incomplete_neg.cc: New test.
+	* testsuite/20_util/is_trivially_assignable/incomplete_neg.cc: New test.
+
 2019-06-20  Jonathan Wakely  <jwakely@redhat.com>
 
 	* acinclude.m4 (GLIBCXX_ENABLE_DEBUG): Only do debug build for final
diff --git a/libstdc++-v3/include/std/type_traits b/libstdc++-v3/include/std/type_traits
index 7d4deb1..77fc94e 100644
--- a/libstdc++-v3/include/std/type_traits
+++ b/libstdc++-v3/include/std/type_traits
@@ -1106,7 +1106,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     : public __bool_constant<__is_assignable(_Tp, _Up)>
     {
       static_assert(std::__is_complete_or_unbounded(__type_identity<_Tp>{}),
-	"template argument must be a complete class or an unbounded array");
+	"first template argument must be a complete class or an unbounded array");
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_Up>{}),
+	"second template argument must be a complete class or an unbounded array");
     };
 
   template<typename _Tp, bool = __is_referenceable<_Tp>::value>
@@ -1168,7 +1170,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     : public __is_nothrow_assignable_impl<_Tp, _Up>
     {
       static_assert(std::__is_complete_or_unbounded(__type_identity<_Tp>{}),
-	"template argument must be a complete class or an unbounded array");
+	"first template argument must be a complete class or an unbounded array");
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_Up>{}),
+	"second template argument must be a complete class or an unbounded array");
     };
 
   template<typename _Tp, bool = __is_referenceable<_Tp>::value>
@@ -1313,7 +1317,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     : public __bool_constant<__is_trivially_assignable(_Tp, _Up)>
     {
       static_assert(std::__is_complete_or_unbounded(__type_identity<_Tp>{}),
-	"template argument must be a complete class or an unbounded array");
+	"first template argument must be a complete class or an unbounded array");
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_Up>{}),
+	"second template argument must be a complete class or an unbounded array");
     };
 
   template<typename _Tp, bool = __is_referenceable<_Tp>::value>
@@ -1474,7 +1480,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _From, typename _To>
     struct is_convertible
     : public __is_convertible_helper<_From, _To>::type
-    { };
+    {
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_From>{}),
+	"first template argument must be a complete class or an unbounded array");
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_To>{}),
+	"second template argument must be a complete class or an unbounded array");
+    };
 
   template<typename _From, typename _To,
            bool = __or_<is_void<_From>, is_function<_To>,
@@ -1516,7 +1527,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _From, typename _To>
     struct is_nothrow_convertible
     : public __is_nt_convertible_helper<_From, _To>::type
-    { };
+    {
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_From>{}),
+	"_From must be a complete class or an unbounded array");
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_To>{}),
+	"_To must be a complete class or an unbounded array");
+};
 
   /// is_nothrow_convertible_v
   template<typename _From, typename _To>
@@ -2810,13 +2826,23 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Tp, typename _Up>
     struct is_swappable_with
     : public __is_swappable_with_impl<_Tp, _Up>::type
-    { };
+    {
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_Tp>{}),
+	"first template argument must be a complete class or an unbounded array");
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_Up>{}),
+	"second template argument must be a complete class or an unbounded array");
+    };
 
   /// is_nothrow_swappable_with
   template<typename _Tp, typename _Up>
     struct is_nothrow_swappable_with
     : public __is_nothrow_swappable_with_impl<_Tp, _Up>::type
-    { };
+    {
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_Tp>{}),
+	"first template argument must be a complete class or an unbounded array");
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_Up>{}),
+	"second template argument must be a complete class or an unbounded array");
+    };
 
 #if __cplusplus >= 201402L
   /// is_swappable_with_v
@@ -2839,7 +2865,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
 
   template<typename _Result, typename _Ret>
     struct __is_invocable_impl<_Result, _Ret, __void_t<typename _Result::type>>
-    : __or_<is_void<_Ret>, is_convertible<typename _Result::type, _Ret>>::type
+    : __or_<is_void<_Ret>,
+	    __is_convertible_helper<typename _Result::type, _Ret>>::type
     { };
 
   template<typename _Fn, typename... _ArgTypes>
@@ -2916,7 +2943,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
   template<typename _Functor, typename... _ArgTypes>
     struct invoke_result
     : public __invoke_result<_Functor, _ArgTypes...>
-    { };
+    {
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_Functor>{}),
+	"template argument must be a complete class or an unbounded array");
+    };
 
   /// std::invoke_result_t
   template<typename _Fn, typename... _Args>
@@ -2965,7 +2995,10 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
     struct is_nothrow_invocable_r
     : __and_<__is_nt_invocable_impl<__invoke_result<_Fn, _ArgTypes...>, _Ret>,
              __call_is_nothrow_<_Fn, _ArgTypes...>>::type
-    { };
+    {
+      static_assert(std::__is_complete_or_unbounded(__type_identity<_Fn>{}),
+	"_Fn must be a complete class or an unbounded array");
+    };
 
   /// std::is_invocable_v
   template<typename _Fn, typename... _Args>
diff --git a/libstdc++-v3/testsuite/20_util/invoke_result/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/invoke_result/incomplete_neg.cc
new file mode 100644
index 0000000..eb06479
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/invoke_result/incomplete_neg.cc
@@ -0,0 +1,30 @@
+// { dg-do compile { target c++17 } }
+//
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-error "must be a complete class" "" { target *-*-* } 0 }
+
+#include <type_traits>
+
+class X;
+
+void test01()
+{
+  std::invoke_result<X, int>();		// { dg-error "required from here" }
+  std::invoke_result<X>();		// { dg-error "required from here" }
+}
diff --git a/libstdc++-v3/testsuite/20_util/is_assignable/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_assignable/incomplete_neg.cc
new file mode 100644
index 0000000..f32a087
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_assignable/incomplete_neg.cc
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++11 } }
+//
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-error "must be a complete class" "" { target *-*-* } 0 }
+
+#include <type_traits>
+
+class X;
+
+void test01()
+{
+  std::is_assignable<X, int>();		// { dg-error "required from here" }
+  std::is_assignable<int, X>();		// { dg-error "required from here" }
+  std::is_assignable<X, X>();		// { dg-error "required from here" }
+}
diff --git a/libstdc++-v3/testsuite/20_util/is_convertible/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_convertible/incomplete_neg.cc
new file mode 100644
index 0000000..3ffb879
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_convertible/incomplete_neg.cc
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++11 } }
+//
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-error "must be a complete class" "" { target *-*-* } 0 }
+
+#include <type_traits>
+
+class X;
+
+void test01()
+{
+  std::is_convertible<X, int>();	// { dg-error "required from here" }
+  std::is_convertible<int, X>();	// { dg-error "required from here" }
+  std::is_convertible<X, X>();		// { dg-error "required from here" }
+}
diff --git a/libstdc++-v3/testsuite/20_util/is_nothrow_assignable/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_nothrow_assignable/incomplete_neg.cc
new file mode 100644
index 0000000..86bffbe
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_nothrow_assignable/incomplete_neg.cc
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++11 } }
+//
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-error "must be a complete class" "" { target *-*-* } 0 }
+
+#include <type_traits>
+
+class X;
+
+void test01()
+{
+  std::is_nothrow_assignable<X, int>();		// { dg-error "required from here" }
+  std::is_nothrow_assignable<int, X>();		// { dg-error "required from here" }
+  std::is_nothrow_assignable<X, X>();		// { dg-error "required from here" }
+}
diff --git a/libstdc++-v3/testsuite/20_util/is_nothrow_convertible/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_nothrow_convertible/incomplete_neg.cc
new file mode 100644
index 0000000..c944d30
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_nothrow_convertible/incomplete_neg.cc
@@ -0,0 +1,32 @@
+// { dg-options "-std=gnu++2a" }
+// { dg-do compile { target c++2a } }
+//
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-error "must be a complete class" "" { target *-*-* } 0 }
+
+#include <type_traits>
+
+class X;
+
+void test01()
+{
+  std::is_nothrow_convertible<X, int>();	// { dg-error "required from here" }
+  std::is_nothrow_convertible<int, X>();	// { dg-error "required from here" }
+  std::is_nothrow_convertible<X, X>();		// { dg-error "required from here" }
+}
diff --git a/libstdc++-v3/testsuite/20_util/is_nothrow_swappable/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_nothrow_swappable/incomplete_neg.cc
new file mode 100644
index 0000000..5ddb6b5
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_nothrow_swappable/incomplete_neg.cc
@@ -0,0 +1,29 @@
+// { dg-do compile { target c++17 } }
+//
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-error "must be a complete class" "" { target *-*-* } 0 }
+
+#include <type_traits>
+
+class X;
+
+void test01()
+{
+  std::is_nothrow_swappable<X>();	// { dg-error "required from here" }
+}
diff --git a/libstdc++-v3/testsuite/20_util/is_nothrow_swappable_with/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_nothrow_swappable_with/incomplete_neg.cc
new file mode 100644
index 0000000..5a0bcbb
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_nothrow_swappable_with/incomplete_neg.cc
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++17 } }
+//
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-error "must be a complete class" "" { target *-*-* } 0 }
+
+#include <type_traits>
+
+class X;
+
+void test01()
+{
+  std::is_nothrow_swappable_with<X, int>();	// { dg-error "required from here" }
+  std::is_nothrow_swappable_with<int, X>();	// { dg-error "required from here" }
+  std::is_nothrow_swappable_with<X, X>();	// { dg-error "required from here" }
+}
diff --git a/libstdc++-v3/testsuite/20_util/is_swappable/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_swappable/incomplete_neg.cc
new file mode 100644
index 0000000..c7ec399
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_swappable/incomplete_neg.cc
@@ -0,0 +1,29 @@
+// { dg-do compile { target c++17 } }
+//
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-error "must be a complete class" "" { target *-*-* } 0 }
+
+#include <type_traits>
+
+class X;
+
+void test01()
+{
+  std::is_swappable<X>();	// { dg-error "required from here" }
+}
diff --git a/libstdc++-v3/testsuite/20_util/is_swappable_with/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_swappable_with/incomplete_neg.cc
new file mode 100644
index 0000000..5fbd3e0
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_swappable_with/incomplete_neg.cc
@@ -0,0 +1,31 @@
+// { dg-do compile { target c++17 } }
+//
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-error "must be a complete class" "" { target *-*-* } 0 }
+
+#include <type_traits>
+
+class X;
+
+void test01()
+{
+  std::is_swappable_with<X, int>();	// { dg-error "required from here" }
+  std::is_swappable_with<int, X>();	// { dg-error "required from here" }
+  std::is_swappable_with<X, X>();	// { dg-error "required from here" }
+}
diff --git a/libstdc++-v3/testsuite/20_util/is_trivially_assignable/incomplete_neg.cc b/libstdc++-v3/testsuite/20_util/is_trivially_assignable/incomplete_neg.cc
new file mode 100644
index 0000000..a93285c
--- /dev/null
+++ b/libstdc++-v3/testsuite/20_util/is_trivially_assignable/incomplete_neg.cc
@@ -0,0 +1,33 @@
+// { dg-do compile { target c++11 } }
+// { dg-prune-output "invalid use of incomplete type" }
+// { dg-prune-output "must be a complete" }
+//
+// Copyright (C) 2019 Free Software Foundation, Inc.
+//
+// This file is part of the GNU ISO C++ Library.  This library is free
+// software; you can redistribute it and/or modify it under the
+// terms of the GNU General Public License as published by the
+// Free Software Foundation; either version 3, or (at your option)
+// any later version.
+//
+// This library is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+// GNU General Public License for more details.
+//
+// You should have received a copy of the GNU General Public License along
+// with this library; see the file COPYING3.  If not see
+// <http://www.gnu.org/licenses/>.
+
+// { dg-error "must be a complete class" "" { target *-*-* } 0 }
+
+#include <type_traits>
+
+class X;
+
+void test01()
+{
+  std::is_trivially_assignable<X, int>();	// { dg-error "required from here" }
+  std::is_trivially_assignable<int, X>();	// { dg-error "required from here" }
+  std::is_trivially_assignable<X, X>();		// { dg-error "required from here" }
+}


More information about the Libstdc++ mailing list