SI-8239 don't loop forever in ContextTrees.locateContextTree

Made loop invariant / recursion metric explicit.
Merge pull request #3469 from adriaanm/t8239

 don't loop forever in ContextTrees.locateContextTree