Bug 50529 - [C++0x] std::vector::erase invokes undefined behavior with empty range
Summary: [C++0x] std::vector::erase invokes undefined behavior with empty range
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: libstdc++ (show other bugs)
Version: 4.6.0
: P3 normal
Target Milestone: 4.6.2
Assignee: Paolo Carlini
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-09-27 00:23 UTC by Jordan DeLong
Modified: 2011-09-30 20:48 UTC (History)
0 users

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2011-09-27 00:00:00


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Jordan DeLong 2011-09-27 00:23:02 UTC
If you call vector erase() with an empty range, it invokes std::move() with an output iterator inside of the source range.  (I.e. it tries to move all the elements onto themselves.)  This causes problems if some of the elements don't support self move-assignment (e.g. if they are other std::vectors).
Comment 1 Paolo Carlini 2011-09-27 00:38:24 UTC
Ok, thanks, it's also an optimization.
Comment 2 Paolo Carlini 2011-09-27 00:45:12 UTC
deque too
Comment 3 paolo@gcc.gnu.org 2011-09-27 02:39:42 UTC
Author: paolo
Date: Tue Sep 27 02:39:34 2011
New Revision: 179234

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=179234
Log:
2011-09-26  Paolo Carlini  <paolo.carlini@oracle.com>

	PR libstdc++/50529
	* include/bits/vector.tcc (vector<>::erase(iterator, iterator)):
	Fix to do nothing if the range is empty.
	* include/bits/stl_bvector.h: Likewise.
	* include/bits/deque.tcc: Likewise.
	* include/debug/vector: Adjust.
	* include/debug/deque: Likewise.
	* testsuite/23_containers/vector/modifiers/erase/50529.cc: New.
	* testsuite/23_containers/deque/modifiers/erase/50529.cc: Likewise.
	* testsuite/23_containers/deque/modifiers/erase/3.cc: Adjust.

Added:
    trunk/libstdc++-v3/testsuite/23_containers/deque/modifiers/erase/50529.cc
    trunk/libstdc++-v3/testsuite/23_containers/vector/modifiers/erase/50529.cc
Modified:
    trunk/libstdc++-v3/ChangeLog
    trunk/libstdc++-v3/include/bits/deque.tcc
    trunk/libstdc++-v3/include/bits/stl_bvector.h
    trunk/libstdc++-v3/include/bits/vector.tcc
    trunk/libstdc++-v3/include/debug/deque
    trunk/libstdc++-v3/include/debug/vector
    trunk/libstdc++-v3/testsuite/23_containers/deque/modifiers/erase/3.cc
Comment 4 Paolo Carlini 2011-09-27 02:40:53 UTC
Done.
Comment 5 Jordan DeLong 2011-09-27 02:56:14 UTC
Nice.  Thanks!
Comment 6 paolo@gcc.gnu.org 2011-09-30 20:47:17 UTC
Author: paolo
Date: Fri Sep 30 20:47:12 2011
New Revision: 179403

URL: http://gcc.gnu.org/viewcvs?root=gcc&view=rev&rev=179403
Log:
2011-09-30  François Dumont  <fdumont@gcc.gnu.org>

	* include/debug/vector (vector<>::erase(iterator, iterator)): Check
	iterators equality using normal iterators.
	* include/debug/deque (deque<>::erase(iterator, iterator)): Likewise.

2011-09-30  Paolo Carlini  <paolo.carlini@oracle.com>

	PR libstdc++/50529
	* include/bits/vector.tcc (vector<>::erase(iterator, iterator)):
	Fix to do nothing if the range is empty.
	* include/bits/stl_bvector.h: Likewise.
	* include/bits/deque.tcc: Likewise.
	* include/debug/vector: Adjust.
	* include/debug/deque: Likewise.
	* testsuite/23_containers/vector/modifiers/erase/50529.cc: New.
	* testsuite/23_containers/deque/modifiers/erase/50529.cc: Likewise.
	* testsuite/23_containers/deque/modifiers/erase/3.cc: Adjust.

Added:
    branches/gcc-4_6-branch/libstdc++-v3/testsuite/23_containers/deque/modifiers/erase/50529.cc
    branches/gcc-4_6-branch/libstdc++-v3/testsuite/23_containers/vector/modifiers/erase/50529.cc
Modified:
    branches/gcc-4_6-branch/libstdc++-v3/ChangeLog
    branches/gcc-4_6-branch/libstdc++-v3/include/bits/deque.tcc
    branches/gcc-4_6-branch/libstdc++-v3/include/bits/stl_bvector.h
    branches/gcc-4_6-branch/libstdc++-v3/include/bits/vector.tcc
    branches/gcc-4_6-branch/libstdc++-v3/include/debug/deque
    branches/gcc-4_6-branch/libstdc++-v3/include/debug/vector
    branches/gcc-4_6-branch/libstdc++-v3/testsuite/23_containers/deque/modifiers/erase/3.cc
Comment 7 Paolo Carlini 2011-09-30 20:48:32 UTC
Fixed in 4.6.2 too.