Skip to content

Commit

Permalink
logic analyzer: fix annotation empty string causing infinite loop
Browse files Browse the repository at this point in the history
- Handle the edge case where an empty string passed to the shortenAnnotationText
function causes an infinite loop.
This fix ensures that the function returns the extension ("...") when
the text is empty or the maxWidth is too small to fit the extension.

Signed-off-by: Adrian Stanea <[email protected]>
  • Loading branch information
Adrian-Stanea committed Nov 25, 2024
1 parent 618d5e8 commit b986365
Showing 1 changed file with 19 additions and 8 deletions.
27 changes: 19 additions & 8 deletions src/logicanalyzer/annotationcurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -700,16 +700,27 @@ uint64_t AnnotationCurve::getMaxAnnotationCount(int index)

QString AnnotationCurve::shortenAnnotationText(const QString text, const double maxWidth, QPainter *painter) const
{
const int padding = 12;
const QString extension = "...";
int count = 0;
const int PADDING = 12;
const QString EXTENSION = "...";
int count = 0;

// find maximum number of characters that fit
while(QSizeF(QwtText(text.left(count) + extension).textSize(painter->font())).width() <= maxWidth - padding) {
count++;
}
bool isWithinTextLength = true;
bool isWithinMaxWidth = true;

// Empty text or maxWidth is too small to even fit the extension
if(text.isEmpty() || maxWidth <= PADDING + painter->fontMetrics().horizontalAdvance(EXTENSION)) {
return EXTENSION;
}

// find maximum number of characters that fit
while(isWithinTextLength && isWithinMaxWidth) {
count++;
isWithinTextLength = count < text.length();
isWithinMaxWidth = QSizeF(QwtText(text.left(count) + EXTENSION).textSize(painter->font())).width() <=
maxWidth - PADDING;
}

return count ? text.left(count) + extension : "";
return isWithinTextLength ? text.left(count) + EXTENSION : "";
}


Expand Down

0 comments on commit b986365

Please sign in to comment.