Crash on empty first page (+fix)

Demon Lord

06-02-2008 17:20:38

Aloha!

Problem:
When creating a TreeIterator3D instance, the following code gets called:

TreeIterator3D::TreeIterator3D(TreeLoader3D *trees)
{
TreeIterator3D::trees = trees;

//Setup iterators
currentGrid = trees->pageGridList.begin();
currentX = 0; currentZ = 0;
currentTreeList = &trees->_getGridPage(currentGrid->second, currentX, currentZ);
currentTree = currentTreeList->begin();
hasMore = true;

//If there's not tree in the first page, keep looking
if (currentTree == currentTreeList->end())
moveNext();

(...)


The moveNext() method begins like this:

void TreeIterator3D::moveNext()
{
//Out of bounds check
if (!hasMore)
OGRE_EXCEPT(1, "Cannot read past end of TreeIterator list", "TreeIterator::moveNext()");

//Preserve the last tree
prevTreeDat = currentTreeDat;

//Incriment iterators to the next tree
++currentTree;
while (currentTree == currentTreeList->end()){
(...)


Therefore, if the first page is empty, currentTree will equal currentTreeList->end(), and the moveNext() method will try to go beyond the list with ++currentTree, leading to a crash.

Adding a check to moveNext() solves this problem:

void TreeIterator3D::moveNext()
{
//Out of bounds check
if (!hasMore)
OGRE_EXCEPT(1, "Cannot read past end of TreeIterator list", "TreeIterator::moveNext()");

//Preserve the last tree
prevTreeDat = currentTreeDat;

//Incriment iterators to the next tree
if (currentTree != currentTreeList->end())
++currentTree;
while (currentTree == currentTreeList->end()){
(...)

JohnJ

06-02-2008 21:43:02

Thanks for posting, I'm glad you found the problem. I've added the fix to CVS.