Skip to content

Fix hang in line-break algorithm

Nyan Pasu requested to merge nyanpasu/kcachegrind:fix-line-break-hang into master

findBreak() previously mixed QFontMetrics::boundingRect().width() (the width a string takes up, including left/right overhangs) and QFontMetrics::horizontalAdvance() (which excludes overhangs). This resulted in inconsistent behaviors within the function.

For borderline strings where fm->boundingRect(text).width() >= maxWidth but fm->horizontalAdvance(text) < maxWidth, the code would not return early, and would never exit the binary search loop because halfWidth < maxWidth even when halfPos == text.length(). In fact, bottomPos gets set to text.length() + 1 and the loop continues forever.

By consistently using QFontMetrics::boundingRect().width(), we make sure the binary search loop always breaks out. It would be nice to add a (halfWidth < maxWidth && halfPos < breakPos) check, but it's not strictly necessary.

BUG: 428917

Merge request reports