Patchwork [google,integration,gcc-4_8] Additional lightweight debug checks for std::deque

login
register
mail settings
Submitter Paul Pluzhnikov
Date Sept. 24, 2013, 1:29 a.m.
Message ID <ye6qfvsvhus8.fsf@elbrus2.mtv.corp.google.com>
Download mbox | patch
Permalink /patch/277333/
State New
Headers show

Comments

Paul Pluzhnikov - Sept. 24, 2013, 1:29 a.m.
Greetings,

I've committed the patch below to google/integration (r202856) and
gcc-4_8 (r202857) branches. Google ref: b/10872448.

This cought approximately 10 bugs in our code.

See also r195357 (similar checks for std::vector) and PR 56109.

Thanks,


2013-09-23  Paul Pluzhnikov  <ppluzhnikov@google.com>

	* libstdc++-v3/include/bits/stl_deque.h (operator[], front,
	  back, pop_front, pop_back): Add conditional range checks.

Patch

Index: libstdc++-v3/include/bits/stl_deque.h
===================================================================
--- libstdc++-v3/include/bits/stl_deque.h	(revision 202851)
+++ libstdc++-v3/include/bits/stl_deque.h	(working copy)
@@ -1242,7 +1242,12 @@ 
        */
       reference
       operator[](size_type __n)
-      { return this->_M_impl._M_start[difference_type(__n)]; }
+      {
+#if __google_stl_debug_deque
+	_M_range_check(__n);
+#endif
+	return this->_M_impl._M_start[difference_type(__n)];
+      }
 
       /**
        *  @brief Subscript access to the data contained in the %deque.
@@ -1257,7 +1262,12 @@ 
        */
       const_reference
       operator[](size_type __n) const
-      { return this->_M_impl._M_start[difference_type(__n)]; }
+      {
+#if __google_stl_debug_deque
+	_M_range_check(__n);
+#endif
+	return this->_M_impl._M_start[difference_type(__n)];
+      }
 
     protected:
       /// Safety check used only from at().
@@ -1311,7 +1321,12 @@ 
        */
       reference
       front()
-      { return *begin(); }
+      {
+#if __google_stl_debug_deque
+	if (empty()) __throw_logic_error("front() on empty deque");
+#endif
+	return *begin();
+      }
 
       /**
        *  Returns a read-only (constant) reference to the data at the first
@@ -1319,7 +1334,12 @@ 
        */
       const_reference
       front() const
-      { return *begin(); }
+      {
+#if __google_stl_debug_deque
+	if (empty()) __throw_logic_error("front() on empty deque");
+#endif
+	return *begin();
+      }
 
       /**
        *  Returns a read/write reference to the data at the last element of the
@@ -1328,6 +1348,9 @@ 
       reference
       back()
       {
+#if __google_stl_debug_deque
+	if (empty()) __throw_logic_error("back() on empty deque");
+#endif
 	iterator __tmp = end();
 	--__tmp;
 	return *__tmp;
@@ -1340,6 +1363,9 @@ 
       const_reference
       back() const
       {
+#if __google_stl_debug_deque
+	if (empty()) __throw_logic_error("back() on empty deque");
+#endif
 	const_iterator __tmp = end();
 	--__tmp;
 	return *__tmp;
@@ -1420,6 +1446,9 @@ 
       void
       pop_front()
       {
+#if __google_stl_debug_deque
+	if (empty()) __throw_logic_error("pop_front() on empty deque");
+#endif
 	if (this->_M_impl._M_start._M_cur
 	    != this->_M_impl._M_start._M_last - 1)
 	  {
@@ -1441,6 +1470,9 @@ 
       void
       pop_back()
       {
+#if __google_stl_debug_deque
+	if (empty()) __throw_logic_error("pop_back() on empty deque");
+#endif
 	if (this->_M_impl._M_finish._M_cur
 	    != this->_M_impl._M_finish._M_first)
 	  {