Skip to content

Commit

Permalink
Fix infinite loop in QTextLayout with setNumColumns()
Browse files Browse the repository at this point in the history
If the line width is negative, then we might exit the layout
loop before consuming any text, and thus the loop will never
finish. This is a side effect of a change for maximumWidth:
991c056.

49a63d3 fixed this issue for
QTextLayout::setFixedSize(), but I forgot to do the same in
the overload of QTextLayout::setNumColumns() which includes
an alignment width and therefore sets the line width in addition
to the column count.

Basically, we just make sure the line width is never negative so
that the width > line.width condition also means the width > 0.

Pick-to: 6.6 6.5
Fixes: QTBUG-115459
Change-Id: If904bfc64cd74e819a0864db55fa9555073d0781
Reviewed-by: Vladimir Belyavsky <[email protected]>
Reviewed-by: Lars Knoll <[email protected]>
(cherry picked from commit be6c651)
Reviewed-by: Qt Cherry-pick Bot <[email protected]>
  • Loading branch information
eskilblomfeldt authored and Qt Cherry-pick Bot committed Jan 29, 2024
1 parent 9f6e20a commit 3576e60
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion src/gui/text/qtextlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1652,7 +1652,7 @@ void QTextLine::setNumColumns(int numColumns)
void QTextLine::setNumColumns(int numColumns, qreal alignmentWidth)
{
QScriptLine &line = eng->lines[index];
line.width = QFixed::fromReal(alignmentWidth);
line.width = QFixed::fromReal(qBound(0.0, alignmentWidth, qreal(QFIXED_MAX)));
line.length = 0;
line.textWidth = 0;
layout_helper(numColumns);
Expand Down
26 changes: 19 additions & 7 deletions tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2737,13 +2737,25 @@ void tst_QTextLayout::min_maximumWidth()

void tst_QTextLayout::negativeLineWidth()
{
QTextLayout layout;
layout.setText("Foo bar");
layout.beginLayout();
QTextLine line = layout.createLine();
line.setLineWidth(-1);
QVERIFY(line.textLength() > 0);
layout.endLayout();
{
QTextLayout layout;
layout.setText("Foo bar");
layout.beginLayout();
QTextLine line = layout.createLine();
line.setLineWidth(-1);
QVERIFY(line.textLength() > 0);
layout.endLayout();
}

{
QTextLayout layout;
layout.setText("Foo bar");
layout.beginLayout();
QTextLine line = layout.createLine();
line.setNumColumns(2, -1);
QVERIFY(line.textLength() > 0);
layout.endLayout();
}
}

QTEST_MAIN(tst_QTextLayout)
Expand Down

0 comments on commit 3576e60

Please sign in to comment.