From: Morgan Deters Date: Fri, 25 Mar 2011 05:20:56 +0000 (+0000) Subject: Fix for a bug Andrew Reynolds found for iterators that affects empty CDList<> objects... X-Git-Tag: cvc5-1.0.0~8635 X-Git-Url: https://git.libre-soc.org/?a=commitdiff_plain;h=ee36b95b8f722fe6501cc6ac635efd49ca673791;p=cvc5.git Fix for a bug Andrew Reynolds found for iterators that affects empty CDList<> objects that allocate from ContextMemoryAllocator<>. Iterators were broken in that begin() != end() for empty lists (again---only those that allocated space from ContextMemoryAllocator<>). Added a unit test for this, too. Thanks Andy! --- diff --git a/src/context/cdlist_context_memory.h b/src/context/cdlist_context_memory.h index 20d672d31..2fd1ceb8c 100644 --- a/src/context/cdlist_context_memory.h +++ b/src/context/cdlist_context_memory.h @@ -476,7 +476,11 @@ public: * Returns an iterator pointing to the first item in the list. */ const_iterator begin() const { - return const_iterator(&d_headSegment, 0); + // This looks curious, but we have to make sure that begin() == end() + // for an empty list, and begin() == (head,0) for a nonempty one. + // Since the segment spill-over is implemented in + // iterator::operator++(), let's reuse it. */ + return ++const_iterator(&d_headSegment, -1); } /** diff --git a/test/unit/context/cdlist_black.h b/test/unit/context/cdlist_black.h index aa25ba9eb..6541973bf 100644 --- a/test/unit/context/cdlist_black.h +++ b/test/unit/context/cdlist_black.h @@ -25,12 +25,15 @@ #include "memory.h" +#include "util/exception.h" #include "context/context.h" #include "context/cdlist.h" +#include "context/cdlist_context_memory.h" using namespace std; using namespace CVC4::context; using namespace CVC4::test; +using namespace CVC4; struct DtorSensitiveObject { bool& d_dtorCalled; @@ -133,6 +136,17 @@ public: TS_ASSERT_EQUALS(aThirdFalse, false); } + void testEmptyIterators() { + CDList* list1 = new(true) CDList(d_context); + CDList< int, ContextMemoryAllocator >* list2 = + new(d_context->getCMM()) + CDList< int, ContextMemoryAllocator >(true, d_context, false, + ContextMemoryAllocator(d_context->getCMM())); + TS_ASSERT_EQUALS(list1->begin(), list1->end()); + TS_ASSERT_EQUALS(list2->begin(), list2->end()); + list1->deleteSelf(); + } + /* setrlimit() totally broken on Mac OS X */ void testOutOfMemory() { #ifdef __APPLE__