mirror of
https://github.com/pocoproject/poco.git
synced 2025-10-27 19:10:20 +01:00
#2945: Iterating over Var containing empty container throws "Out of range" exception
This commit is contained in:
@@ -737,7 +737,7 @@ inline const std::type_info& Var::type() const
|
|||||||
|
|
||||||
inline Var::ConstIterator Var::begin() const
|
inline Var::ConstIterator Var::begin() const
|
||||||
{
|
{
|
||||||
if (isEmpty()) return ConstIterator(const_cast<Var*>(this), true);
|
if (size() == 0) return ConstIterator(const_cast<Var*>(this), true);
|
||||||
|
|
||||||
return ConstIterator(const_cast<Var*>(this), false);
|
return ConstIterator(const_cast<Var*>(this), false);
|
||||||
}
|
}
|
||||||
@@ -749,7 +749,7 @@ inline Var::ConstIterator Var::end() const
|
|||||||
|
|
||||||
inline Var::Iterator Var::begin()
|
inline Var::Iterator Var::begin()
|
||||||
{
|
{
|
||||||
if (isEmpty()) return Iterator(const_cast<Var*>(this), true);
|
if (size() == 0) return Iterator(const_cast<Var*>(this), true);
|
||||||
|
|
||||||
return Iterator(const_cast<Var*>(this), false);
|
return Iterator(const_cast<Var*>(this), false);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -31,6 +31,7 @@ VarIterator::VarIterator(Var* pVar, bool positionEnd):
|
|||||||
_pVar(pVar),
|
_pVar(pVar),
|
||||||
_position(positionEnd ? POSITION_END : 0)
|
_position(positionEnd ? POSITION_END : 0)
|
||||||
{
|
{
|
||||||
|
if (!_pVar || _pVar->isEmpty()) throw InvalidAccessException("Cannot create iterator on empty Var");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -2973,8 +2973,15 @@ void VarTest::testEmpty()
|
|||||||
void VarTest::testIterator()
|
void VarTest::testIterator()
|
||||||
{
|
{
|
||||||
Var da;
|
Var da;
|
||||||
assertTrue (da.isEmpty());
|
try
|
||||||
assertTrue (da.begin() == da.end());
|
{
|
||||||
|
auto it = da.begin();
|
||||||
|
fail("calling begin() on empty Var must throw");
|
||||||
|
}
|
||||||
|
catch (const InvalidAccessException&) { }
|
||||||
|
|
||||||
|
da = Poco::Dynamic::Array();
|
||||||
|
assertTrue(da.begin() == da.end());
|
||||||
|
|
||||||
da = 1;
|
da = 1;
|
||||||
assertTrue (!da.isEmpty());
|
assertTrue (!da.isEmpty());
|
||||||
|
|||||||
@@ -184,14 +184,29 @@ private:
|
|||||||
void testContainerIterator()
|
void testContainerIterator()
|
||||||
{
|
{
|
||||||
C cont;
|
C cont;
|
||||||
|
Poco::Dynamic::Var arr(cont);
|
||||||
|
int counter = 0;
|
||||||
|
|
||||||
|
// test empty
|
||||||
|
assertTrue (arr.size() == 0);
|
||||||
|
Poco::Dynamic::Var::Iterator it = arr.begin();
|
||||||
|
Poco::Dynamic::Var::Iterator end = arr.end();
|
||||||
|
|
||||||
|
for (; it != end; ++it)
|
||||||
|
{
|
||||||
|
*it = ++counter;
|
||||||
|
}
|
||||||
|
assertTrue(counter == 0);
|
||||||
|
|
||||||
|
// test non-empty
|
||||||
cont.push_back(1);
|
cont.push_back(1);
|
||||||
cont.push_back("2");
|
cont.push_back("2");
|
||||||
cont.push_back(3.5);
|
cont.push_back(3.5);
|
||||||
Poco::Dynamic::Var arr(cont);
|
arr = cont;
|
||||||
assertTrue (arr.size() == 3);
|
assertTrue (arr.size() == 3);
|
||||||
Poco::Dynamic::Var::Iterator it = arr.begin();
|
it = arr.begin();
|
||||||
Poco::Dynamic::Var::Iterator end = arr.end();
|
end = arr.end();
|
||||||
int counter = 0;
|
counter = 0;
|
||||||
for (; it != end; ++it)
|
for (; it != end; ++it)
|
||||||
{
|
{
|
||||||
switch (++counter)
|
switch (++counter)
|
||||||
|
|||||||
Reference in New Issue
Block a user