Patchwork [google,gcc-4_7,integration] Add lightweight checks for front()/back() on empty vector

login
register
mail settings
Submitter Paul Pluzhnikov
Date Jan. 20, 2013, 6:15 p.m.
Message ID <CALoOobMWuh87UFsQPCyo-=oucL67jX2T8JKTxpr6_i9rvC4o4A@mail.gmail.com>
Download mbox | patch
Permalink /patch/213972/
State New
Headers show

Comments

Paul Pluzhnikov - Jan. 20, 2013, 6:15 p.m.
On Sun, Jan 20, 2013 at 3:16 AM, Gerald Pfeifer <gerald@pfeifer.com> wrote:
> Isn't the error message wrong, then?

Thanks for catching that! Updated patch attached.
Diego Novillo - Jan. 21, 2013, 5:20 p.m.
On Sun, Jan 20, 2013 at 1:15 PM, Paul Pluzhnikov <ppluzhnikov@google.com> wrote:
> On Sun, Jan 20, 2013 at 3:16 AM, Gerald Pfeifer <gerald@pfeifer.com> wrote:
>> Isn't the error message wrong, then?
>
> Thanks for catching that! Updated patch attached.

OK for google branches.


Diego.

Patch

Index: libstdc++-v3/include/bits/stl_vector.h
===================================================================
--- libstdc++-v3/include/bits/stl_vector.h	(revision 195313)
+++ libstdc++-v3/include/bits/stl_vector.h	(working copy)
@@ -878,7 +878,12 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        */
       reference
       front()
-      { return *begin(); }
+      {
+#if __google_stl_debug_vector
+        if (empty()) __throw_logic_error("front() on empty vector");
+#endif
+        return *begin();
+      }
 
       /**
        *  Returns a read-only (constant) reference to the data at the first
@@ -886,7 +891,12 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        */
       const_reference
       front() const
-      { return *begin(); }
+      {
+#if __google_stl_debug_vector
+        if (empty()) __throw_logic_error("front() on empty vector");
+#endif
+        return *begin();
+      }
 
       /**
        *  Returns a read/write reference to the data at the last
@@ -894,7 +904,12 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        */
       reference
       back()
-      { return *(end() - 1); }
+      {
+#if __google_stl_debug_vector
+        if (empty()) __throw_logic_error("back() on empty vector");
+#endif
+        return *(end() - 1);
+      }
       
       /**
        *  Returns a read-only (constant) reference to the data at the
@@ -902,7 +917,12 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
        */
       const_reference
       back() const
-      { return *(end() - 1); }
+      {
+#if __google_stl_debug_vector
+        if (empty()) __throw_logic_error("back() on empty vector");
+#endif
+        return *(end() - 1);
+      }
 
       // _GLIBCXX_RESOLVE_LIB_DEFECTS
       // DR 464. Suggestion for new member functions in standard containers.
@@ -917,7 +937,12 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       pointer
 #endif
       data() _GLIBCXX_NOEXCEPT
-      { return std::__addressof(front()); }
+      {
+#if __google_stl_debug_vector
+        if (empty()) return 0;
+#endif
+        return std::__addressof(front());
+      }
 
 #ifdef __GXX_EXPERIMENTAL_CXX0X__
       const _Tp*
@@ -925,7 +950,12 @@  _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       const_pointer
 #endif
       data() const _GLIBCXX_NOEXCEPT
-      { return std::__addressof(front()); }
+      {
+#if __google_stl_debug_vector
+        if (empty()) return 0;
+#endif
+        return std::__addressof(front());
+      }
 
       // [23.2.4.3] modifiers
       /**