public inbox for libstdc++@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Avoid bogus overflow warnings in std::vector<bool> [PR110807]
@ 2023-07-26 16:03 Jonathan Wakely
  2023-07-26 18:11 ` [committed] libstdc++: Require C++11 for 23_containers/vector/bool/110807.cc [PR110807] Jonathan Wakely
  0 siblings, 1 reply; 2+ messages in thread
From: Jonathan Wakely @ 2023-07-26 16:03 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Tested x86_64-linux. Pushed to trunk. Backport to gcc-13 will come after
the 13.2 release.

-- >8 --

GCC thinks the allocation can alter the object being copied if it's
globally reachable, so doesn't realize that [x.begin(), x.end()) after
the allocation is the same as x.size() before it.

This causes a testsuite failure when testing with -D_GLIBCXX_DEBUG:
FAIL: 23_containers/vector/bool/swap.cc (test for excess errors)
A fix is to move the calls to x.begin() and x.end() to before the
allocation.

A similar problem exists in vector<bool>::_M_insert_range where *this is
globally reachable, as reported in PR libstdc++/110807. That can also be
fixed by moving calls to begin() and end() before the allocation.

libstdc++-v3/ChangeLog:

	PR libstdc++/110807
	* include/bits/stl_bvector.h (vector(const vector&)): Access
	iterators before allocating.
	* include/bits/vector.tcc (vector<bool>::_M_insert_range):
	Likewise.
	* testsuite/23_containers/vector/bool/110807.cc: New test.
---
 libstdc++-v3/include/bits/stl_bvector.h            |  3 ++-
 libstdc++-v3/include/bits/vector.tcc               |  5 +++--
 .../testsuite/23_containers/vector/bool/110807.cc  | 14 ++++++++++++++
 3 files changed, 19 insertions(+), 3 deletions(-)
 create mode 100644 libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc

diff --git a/libstdc++-v3/include/bits/stl_bvector.h b/libstdc++-v3/include/bits/stl_bvector.h
index ad462c5933c..8d18bcaffd4 100644
--- a/libstdc++-v3/include/bits/stl_bvector.h
+++ b/libstdc++-v3/include/bits/stl_bvector.h
@@ -773,8 +773,9 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
       vector(const vector& __x)
       : _Base(_Bit_alloc_traits::_S_select_on_copy(__x._M_get_Bit_allocator()))
       {
+	const_iterator __xbegin = __x.begin(), __xend = __x.end();
 	_M_initialize(__x.size());
-	_M_copy_aligned(__x.begin(), __x.end(), begin());
+	_M_copy_aligned(__xbegin, __xend, begin());
       }
 
 #if __cplusplus >= 201103L
diff --git a/libstdc++-v3/include/bits/vector.tcc b/libstdc++-v3/include/bits/vector.tcc
index f592c72dec2..ada396c9b30 100644
--- a/libstdc++-v3/include/bits/vector.tcc
+++ b/libstdc++-v3/include/bits/vector.tcc
@@ -980,11 +980,12 @@ _GLIBCXX_BEGIN_NAMESPACE_CONTAINER
 	      {
 		const size_type __len =
 		  _M_check_len(__n, "vector<bool>::_M_insert_range");
+		const iterator __begin = begin(), __end = end();
 		_Bit_pointer __q = this->_M_allocate(__len);
 		iterator __start(std::__addressof(*__q), 0);
-		iterator __i = _M_copy_aligned(begin(), __position, __start);
+		iterator __i = _M_copy_aligned(__begin, __position, __start);
 		__i = std::copy(__first, __last, __i);
-		iterator __finish = std::copy(__position, end(), __i);
+		iterator __finish = std::copy(__position, __end, __i);
 		this->_M_deallocate();
 		this->_M_impl._M_end_of_storage = __q + _S_nword(__len);
 		this->_M_impl._M_start = __start;
diff --git a/libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc b/libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc
new file mode 100644
index 00000000000..5c019bd9524
--- /dev/null
+++ b/libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc
@@ -0,0 +1,14 @@
+// { dg-options "-O2" }
+// { dg-do compile }
+
+// Bug 110807
+// Copy list initialisation of a vector<bool> raises a warning with -O2
+
+#include <vector>
+
+std::vector<bool> byCallSpread;
+
+void f()
+{
+  byCallSpread = {true};
+}
-- 
2.41.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

* [committed] libstdc++: Require C++11 for 23_containers/vector/bool/110807.cc [PR110807]
  2023-07-26 16:03 [committed] libstdc++: Avoid bogus overflow warnings in std::vector<bool> [PR110807] Jonathan Wakely
@ 2023-07-26 18:11 ` Jonathan Wakely
  0 siblings, 0 replies; 2+ messages in thread
From: Jonathan Wakely @ 2023-07-26 18:11 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Pushed to trunk.

-- >8 --

This new test uses uniform initialization syntax, so requires C++11 or
later.

libstdc++-v3/ChangeLog:

	PR libstdc++/110807
	* testsuite/23_containers/vector/bool/110807.cc: Require c++11.
---
 libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc b/libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc
index 5c019bd9524..2e9d4019edb 100644
--- a/libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc
+++ b/libstdc++-v3/testsuite/23_containers/vector/bool/110807.cc
@@ -1,5 +1,5 @@
 // { dg-options "-O2" }
-// { dg-do compile }
+// { dg-do compile { target c++11 } }
 
 // Bug 110807
 // Copy list initialisation of a vector<bool> raises a warning with -O2
-- 
2.41.0


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2023-07-26 18:12 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-07-26 16:03 [committed] libstdc++: Avoid bogus overflow warnings in std::vector<bool> [PR110807] Jonathan Wakely
2023-07-26 18:11 ` [committed] libstdc++: Require C++11 for 23_containers/vector/bool/110807.cc [PR110807] Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).