Skip to content

Commit 6705e01

Browse files
Adrian-StaneaRainDalena
authored andcommitted
logic analyzer: fix annotation empty string causing infinite loop
- 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]> Signed-off-by: JDalenaJ <[email protected]>
1 parent 35d2380 commit 6705e01

File tree

1 file changed

+15
-4
lines changed

1 file changed

+15
-4
lines changed

plugins/m2k/m2k-gui/sigrok-gui/src/annotationcurve.cpp

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -672,16 +672,27 @@ uint64_t AnnotationCurve::getMaxAnnotationCount(int index)
672672
// shorten text to fit maxWidth pixels in width
673673
QString AnnotationCurve::shortenAnnotationText(const QString text, const double maxWidth, QPainter *painter) const
674674
{
675-
const int padding = 12;
676-
const QString extension = "...";
675+
const int PADDING = 12;
676+
const QString EXTENSION = "...";
677677
int count = 0;
678678

679+
bool isWithinTextLength = true;
680+
bool isWithinMaxWidth = true;
681+
682+
// Empty text or maxWidth is too small to even fit the extension
683+
if(text.isEmpty() || maxWidth <= PADDING + painter->fontMetrics().horizontalAdvance(EXTENSION)) {
684+
return EXTENSION;
685+
}
686+
679687
// find maximum number of characters that fit
680-
while(QSizeF(QwtText(text.left(count) + extension).textSize(painter->font())).width() <= maxWidth - padding) {
688+
while(isWithinTextLength && isWithinMaxWidth) {
681689
count++;
690+
isWithinTextLength = count < text.length();
691+
isWithinMaxWidth = QSizeF(QwtText(text.left(count) + EXTENSION).textSize(painter->font())).width() <=
692+
maxWidth - PADDING;
682693
}
683694

684-
return count ? text.left(count) + extension : "";
695+
return isWithinTextLength ? text.left(count) + EXTENSION : "";
685696
}
686697

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

0 commit comments

Comments
 (0)