[git commit] Fix erase() on derived __base_associative

Bernhard Reutner-Fischer rep.dot.nop at gmail.com
Fri Oct 5 14:03:24 UTC 2018


commit: https://git.uclibc.org/uClibc++/commit/?id=da23783634c57174bd3a19a3c0c0cc4626bdd219
branch: https://git.uclibc.org/uClibc++/commit/?id=refs/heads/master

When calling erase() on a containers derived from __base_associative
(e.g. multimap) and providing a pair of iterators a segfault will
occur.

Example code to reproduce:

	typedef std::multimap<int, int> testmap;
	testmap t;
	t.insert(std::pair<int, int>(1, 1));
	t.insert(std::pair<int, int>(2, 1));
	t.insert(std::pair<int, int>(3, 1));
	t.erase(t.begin(), t.end());

Signed-off-by: Ben Kelly <ben at benjii.net>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop at gmail.com>
---
 ChangeLog                      |  1 +
 include/associative_base       |  3 +--
 tests/mmaptest.cpp             | 17 +++++++++++++++++
 tests/testoutput/mmaptest.good |  4 ++--
 4 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 5fcc770..7160582 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,4 +1,5 @@
 0.2.5
+-   __base_associative: fix erase(iter,iter), e.g. multimap
 -   list: fix splice to empty list from other.begin()
 -   add refcounted exceptions
 -   tests: Allow to run tests via a simulator
diff --git a/include/associative_base b/include/associative_base
index 27ae0ef..be8b27f 100644
--- a/include/associative_base
+++ b/include/associative_base
@@ -200,8 +200,7 @@ public:
 	}
 	void erase(iterator first, iterator last){
 		while(first != last){
-			backing.erase(first.base_iterator());
-			++first;
+			first = backing.erase(first.base_iterator());
 		}
 	}
 
diff --git a/tests/mmaptest.cpp b/tests/mmaptest.cpp
index fa5af6d..ee621c3 100644
--- a/tests/mmaptest.cpp
+++ b/tests/mmaptest.cpp
@@ -130,6 +130,22 @@ bool test_positioned_insert(){
 	return true;
 }
 
+static bool erase_both_iters() {
+	typedef std::multimap<int, int> testmap;
+        testmap tst;
+        tst.insert(std::pair<int, int>(1, 1));
+        tst.insert(std::pair<int, int>(2, 1));
+        tst.insert(std::pair<int, int>(3, 1));
+        tst.erase(tst.begin(), tst.end());
+	if (tst.empty() == true
+	    && tst.size() == 0
+	    && tst.rbegin() == tst.rend()
+	    && tst.begin() == tst.end()
+	    && tst.find(42) == tst.end())
+		return true;
+	return false;
+}
+
 
 int main(){
 
@@ -137,6 +153,7 @@ int main(){
 
         TestFramework::AssertReturns<bool>(test_added_elements, true);
         TestFramework::AssertReturns<bool>(test_positioned_insert, true);
+        TestFramework::AssertReturns<bool>(erase_both_iters, true);
 
         TestFramework::results();
 
diff --git a/tests/testoutput/mmaptest.good b/tests/testoutput/mmaptest.good
index 3c74927..a08c83e 100644
--- a/tests/testoutput/mmaptest.good
+++ b/tests/testoutput/mmaptest.good
@@ -1,6 +1,6 @@
-..
+...
 ------------------------------
-Ran 2 tests
+Ran 3 tests
 
 OK
 Start of multimap test


More information about the uClibc-cvs mailing list