Commit 991ed989 authored by Romain Vimont's avatar Romain Vimont Committed by Hugo Beauzée-Luyssen

qt: playlist: fix stack overflow

The playlist tree may be arbitrary deep, so traversing it recursively
may lead to stack overflow.

Traverse it iteratively instead.

Fixes #18376
Signed-off-by: 's avatarHugo Beauzée-Luyssen <hugo@beauzee.fr>
parent b838ea1c
......@@ -41,6 +41,7 @@
#include <assert.h>
#include <QFont>
#include <QAction>
#include <QStack>
/*************************************************************************
* Playlist model implementation
......@@ -518,20 +519,31 @@ PLItem *PLModel::findByPLId( PLItem *root, int i_id ) const
if( root->id() == i_id )
return root;
QList<AbstractPLItem *>::iterator it = root->children.begin();
while ( it != root->children.end() )
/* traverse the tree (in depth first) iteratively to avoid stack overflow */
struct RemainingChildren {
QList<AbstractPLItem *>::const_iterator next;
QList<AbstractPLItem *>::const_iterator end;
};
QStack<RemainingChildren> stack;
if( root->childCount() )
stack.push( {root->children.cbegin(), root->children.cend()} );
while ( !stack.isEmpty() )
{
PLItem *item = static_cast<PLItem *>(*it);
RemainingChildren &remainingChildren = stack.top();
PLItem *item = static_cast<PLItem *>( *remainingChildren.next );
if( item->id() == i_id )
return item;
if( ++remainingChildren.next == remainingChildren.end )
/* there are no more children at this depth level */
stack.pop();
if( item->childCount() )
{
PLItem *childFound = findByPLId( item, i_id );
if( childFound )
return childFound;
}
++it;
stack.push( {item->children.cbegin(), item->children.cend()} );
}
return NULL;
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment