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 authored and AlexandraTrifan committed Nov 25, 2024
1 parent aecb9a7 commit ad538e5
Showing 1 changed file with 15 additions and 4 deletions.
19 changes: 15 additions & 4 deletions plugins/m2k/m2k-gui/sigrok-gui/src/annotationcurve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -672,16 +672,27 @@ uint64_t AnnotationCurve::getMaxAnnotationCount(int index)
// shorten text to fit maxWidth pixels in width
QString AnnotationCurve::shortenAnnotationText(const QString text, const double maxWidth, QPainter *painter) const
{
const int padding = 12;
const QString extension = "...";
const int PADDING = 12;
const QString EXTENSION = "...";
int count = 0;

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(QSizeF(QwtText(text.left(count) + extension).textSize(painter->font())).width() <= maxWidth - padding) {
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 : "";
}

void AnnotationCurve::drawTwoSampleAnnotation(int row, const Annotation &ann, QPainter *painter,
Expand Down

0 comments on commit ad538e5

Please sign in to comment.