[patch, libstdc++] In debug mode, diagnose empty initializer_list in min/max/minmax

Jonathan Wakely jwakely@redhat.com
Tue Feb 23 22:39:00 GMT 2016


On 23/02/16 22:03 +0100, Eelis wrote:
>The std::min, std::max, and std::minmax overloads that take a std::initializer_list all require that the list is not empty. The attached patch adds debug mode checks for this.

Nice, thanks for the patch.

>Thanks,
>
>Eelis

>Index: libstdc++-v3/include/debug/formatter.h
>===================================================================
>--- libstdc++-v3/include/debug/formatter.h	(revision 233636)
>+++ libstdc++-v3/include/debug/formatter.h	(working copy)
>@@ -87,6 +87,8 @@
>     __msg_splice_bad,
>     __msg_splice_other,
>     __msg_splice_overlap,
>+    // std::initializer_list checks
>+    __msg_empty_init_list,
>     // iterator checks
>     __msg_init_singular,
>     __msg_init_copy_singular,
>Index: libstdc++-v3/src/c++11/debug.cc
>===================================================================
>--- libstdc++-v3/src/c++11/debug.cc	(revision 233636)
>+++ libstdc++-v3/src/c++11/debug.cc	(working copy)
>@@ -139,6 +139,8 @@
>     "attempt to splice an iterator from a different container",
>     "splice destination %1.name;"
>     " occurs within source range [%2.name;, %3.name;)",
>+    // std::initializer_list checks
>+    "%1;(): empty initializer_list",
>     // iterator checks
>     "attempt to initialize an iterator that will immediately become singular",
>     "attempt to copy-construct an iterator from a singular iterator",

New entries should go at the end, so you don't alter the positions of
existing entries.


>Index: libstdc++-v3/include/debug/macros.h
>===================================================================
>--- libstdc++-v3/include/debug/macros.h	(revision 233636)
>+++ libstdc++-v3/include/debug/macros.h	(working copy)
>@@ -69,6 +69,12 @@
> 		      ._M_iterator(_First, #_First)			\
> 		      ._M_iterator(_Last, #_Last))
> 
>+// Verify that the initializer_list is non-empty.
>+#define __glibcxx_check_non_empty_init_list(_List)			\
>+_GLIBCXX_DEBUG_VERIFY(_List.size() != 0,				\
>+		      _M_message(__gnu_debug::__msg_empty_init_list)	\
>+		      ._M_string(__func__))
>+
> /** Verify that we can insert into *this with the iterator _Position.
>  *  Insertion into a container at a specific position requires that
>  *  the iterator be nonsingular, either dereferenceable or past-the-end,
>Index: libstdc++-v3/include/debug/debug.h
>===================================================================
>--- libstdc++-v3/include/debug/debug.h	(revision 233636)
>+++ libstdc++-v3/include/debug/debug.h	(working copy)
>@@ -62,6 +62,7 @@
> 
> # define __glibcxx_requires_cond(_Cond,_Msg)
> # define __glibcxx_requires_valid_range(_First,_Last)
>+# define __glibcxx_requires_non_empty_init_list(_List)
> # define __glibcxx_requires_sorted(_First,_Last)
> # define __glibcxx_requires_sorted_pred(_First,_Last,_Pred)
> # define __glibcxx_requires_sorted_set(_First1,_Last1,_First2)

This should be enabled for _GLIBCXX_ASSERTIONS not only
_GLIBCXX_DEBUG.

Otherwise this looks good, but will have to wait until after the GCC 6
release now. If I forget about it please send a ping email to remind
us once GCC 6 has been released, thanks.





More information about the Libstdc++ mailing list