https://gcc.gnu.org/g:1921b0c043014cdcc9981eefb674f2cab3a9d72e

commit r14-11354-g1921b0c043014cdcc9981eefb674f2cab3a9d72e
Author: Jonathan Wakely <jwak...@redhat.com>
Date:   Fri Nov 15 22:03:20 2024 +0000

    libstdc++: Add debug assertions to std::list and std::forward_list
    
    While working on fancy pointer support for the linked lists I noticed
    they didn't have any debug assertions. This adds the obvious non-empty
    assertions to front() and back().
    
    libstdc++-v3/ChangeLog:
    
            * include/bits/forward_list.h (forward_list::front): Add
            non-empty assertions.
            * include/bits/stl_list.h (list::front, list::back): Add
            non-empty assertions.
    
    (cherry picked from commit e7aa614d7372b5d3cbcd2400838c80ef905ba381)

Diff:
---
 libstdc++-v3/include/bits/forward_list.h |  3 +++
 libstdc++-v3/include/bits/stl_list.h     | 13 +++++++++++--
 2 files changed, 14 insertions(+), 2 deletions(-)

diff --git a/libstdc++-v3/include/bits/forward_list.h 
b/libstdc++-v3/include/bits/forward_list.h
index 5ab2253f6093..1dae56c4734e 100644
--- a/libstdc++-v3/include/bits/forward_list.h
+++ b/libstdc++-v3/include/bits/forward_list.h
@@ -40,6 +40,7 @@
 #include <bits/allocator.h>
 #include <ext/alloc_traits.h>
 #include <ext/aligned_buffer.h>
+#include <debug/assertions.h>
 
 namespace std _GLIBCXX_VISIBILITY(default)
 {
@@ -821,6 +822,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       reference
       front()
       {
+       __glibcxx_requires_nonempty();
        _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
        return *__front->_M_valptr();
       }
@@ -833,6 +835,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       const_reference
       front() const
       {
+       __glibcxx_requires_nonempty();
        _Node* __front = static_cast<_Node*>(this->_M_impl._M_head._M_next);
        return *__front->_M_valptr();
       }
diff --git a/libstdc++-v3/include/bits/stl_list.h 
b/libstdc++-v3/include/bits/stl_list.h
index 8b2521960a8f..089484844684 100644
--- a/libstdc++-v3/include/bits/stl_list.h
+++ b/libstdc++-v3/include/bits/stl_list.h
@@ -59,6 +59,7 @@
 
 #include <bits/concept_check.h>
 #include <ext/alloc_traits.h>
+#include <debug/assertions.h>
 #if __cplusplus >= 201103L
 #include <initializer_list>
 #include <bits/allocated_ptr.h>
@@ -1203,7 +1204,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       _GLIBCXX_NODISCARD
       reference
       front() _GLIBCXX_NOEXCEPT
-      { return *begin(); }
+      {
+       __glibcxx_requires_nonempty();
+       return *begin();
+      }
 
       /**
        *  Returns a read-only (constant) reference to the data at the first
@@ -1212,7 +1216,10 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       _GLIBCXX_NODISCARD
       const_reference
       front() const _GLIBCXX_NOEXCEPT
-      { return *begin(); }
+      {
+       __glibcxx_requires_nonempty();
+       return *begin();
+      }
 
       /**
        *  Returns a read/write reference to the data at the last element
@@ -1222,6 +1229,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       reference
       back() _GLIBCXX_NOEXCEPT
       {
+       __glibcxx_requires_nonempty();
        iterator __tmp = end();
        --__tmp;
        return *__tmp;
@@ -1235,6 +1243,7 @@ _GLIBCXX_BEGIN_NAMESPACE_CXX11
       const_reference
       back() const _GLIBCXX_NOEXCEPT
       {
+       __glibcxx_requires_nonempty();
        const_iterator __tmp = end();
        --__tmp;
        return *__tmp;

Reply via email to