Skip to content

activitymanager: Avoid infinite loop in ActivityList

When updating the selection in the activity manager we search through activities using using a do while loop. Each iteration of the loop increments or decrements selectedIndex depending on the search direction, wrapping around if needed. The loop stops only once we have found a visible item to select or if we have looped back to the item we started with. The latter is done by comparing selectedIndex with the index we started at, saved earlier in startingWithSelected.

If the user triggers the activity manager and starts searching activities right away without first selecting one, startingWithSelected will remain at its initial value of -1. If there are no matches and the user then attempts to select the next or previous item, we will loop infinitely because there are no visible items and selectedIndex can never again equal -1. This infinite loop will hang the entirety of plasmashell.

To fix this, keep track of how many items we have visited and forcibly break out of the loop if we have seen all activities (meaning that we have wrapped around). This does not rely on the value of either selectedIndex or startingWithSelected, meaning it also works if no activities are visible. We can also keep the old selection behaviour by setting selectedIndex to startingWithSelected right before we break.

Merge request reports