[libcxx] r348994 - [libcxx] Add assertion in deque::pop_back when popping from an empty deque

Louis Dionne ldionne at apple.com
Wed Dec 12 15:58:26 PST 2018


Author: ldionne
Date: Wed Dec 12 15:58:25 2018
New Revision: 348994

URL: http://llvm.org/viewvc/llvm-project?rev=348994&view=rev
Log:
[libcxx] Add assertion in deque::pop_back when popping from an empty deque

Also, add tests making sure that vector and deque both catch the problem
when assertions are enabled. Otherwise, deque would segfault and vector
would never terminate.

Added:
    libcxx/trunk/test/libcxx/containers/sequences/deque/pop_back_empty.pass.cpp
    libcxx/trunk/test/libcxx/containers/sequences/vector/pop_back_empty.pass.cpp
Modified:
    libcxx/trunk/include/deque

Modified: libcxx/trunk/include/deque
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/include/deque?rev=348994&r1=348993&r2=348994&view=diff
==============================================================================
--- libcxx/trunk/include/deque (original)
+++ libcxx/trunk/include/deque Wed Dec 12 15:58:25 2018
@@ -987,7 +987,7 @@ public:
 #if _LIBCPP_STD_VER >= 14
         _NOEXCEPT;
 #else
-        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || 
+        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
                     __is_nothrow_swappable<allocator_type>::value);
 #endif
 protected:
@@ -1156,7 +1156,7 @@ __deque_base<_Tp, _Allocator>::swap(__de
 #if _LIBCPP_STD_VER >= 14
         _NOEXCEPT
 #else
-        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || 
+        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
                     __is_nothrow_swappable<allocator_type>::value)
 #endif
 {
@@ -2342,7 +2342,7 @@ deque<_Tp, _Allocator>::__add_front_capa
                 _Dp(__a, __base::__block_size));
         __buf.push_back(__hold.get());
         __hold.release();
-    
+
         for (typename __base::__map_pointer __i = __base::__map_.begin();
                 __i != __base::__map_.end(); ++__i)
             __buf.push_back(*__i);
@@ -2604,6 +2604,7 @@ template <class _Tp, class _Allocator>
 void
 deque<_Tp, _Allocator>::pop_back()
 {
+    _LIBCPP_ASSERT(!empty(), "deque::pop_back called for empty deque");
     allocator_type& __a = __base::__alloc();
     size_type __p = __base::size() + __base::__start_ - 1;
     __alloc_traits::destroy(__a, __to_raw_pointer(*(__base::__map_.begin() +
@@ -2854,7 +2855,7 @@ deque<_Tp, _Allocator>::swap(deque& __c)
 #if _LIBCPP_STD_VER >= 14
         _NOEXCEPT
 #else
-        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value || 
+        _NOEXCEPT_(!__alloc_traits::propagate_on_container_swap::value ||
                     __is_nothrow_swappable<allocator_type>::value)
 #endif
 {

Added: libcxx/trunk/test/libcxx/containers/sequences/deque/pop_back_empty.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/containers/sequences/deque/pop_back_empty.pass.cpp?rev=348994&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/containers/sequences/deque/pop_back_empty.pass.cpp (added)
+++ libcxx/trunk/test/libcxx/containers/sequences/deque/pop_back_empty.pass.cpp Wed Dec 12 15:58:25 2018
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <deque>
+
+// pop_back() more than the number of elements in a deque
+
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
+
+#include <cstdlib>
+#include <deque>
+
+
+int main() {
+    std::deque<int> q;
+    q.push_back(0);
+    q.pop_back();
+    q.pop_back();
+}

Added: libcxx/trunk/test/libcxx/containers/sequences/vector/pop_back_empty.pass.cpp
URL: http://llvm.org/viewvc/llvm-project/libcxx/trunk/test/libcxx/containers/sequences/vector/pop_back_empty.pass.cpp?rev=348994&view=auto
==============================================================================
--- libcxx/trunk/test/libcxx/containers/sequences/vector/pop_back_empty.pass.cpp (added)
+++ libcxx/trunk/test/libcxx/containers/sequences/vector/pop_back_empty.pass.cpp Wed Dec 12 15:58:25 2018
@@ -0,0 +1,25 @@
+//===----------------------------------------------------------------------===//
+//
+//                     The LLVM Compiler Infrastructure
+//
+// This file is dual licensed under the MIT and the University of Illinois Open
+// Source Licenses. See LICENSE.TXT for details.
+//
+//===----------------------------------------------------------------------===//
+
+// <vector>
+
+// pop_back() more than the number of elements in a vector
+
+#define _LIBCPP_ASSERT(x, m) ((x) ? (void)0 : std::exit(0))
+
+#include <cstdlib>
+#include <vector>
+
+
+int main() {
+    std::vector<int> v;
+    v.push_back(0);
+    v.pop_back();
+    v.pop_back();
+}




More information about the libcxx-commits mailing list