From 3576e609929a1f58271ee53f8db6fb005f772d81 Mon Sep 17 00:00:00 2001 From: Eskil Abrahamsen Blomfeldt Date: Mon, 29 Jan 2024 11:44:25 +0100 Subject: [PATCH] Fix infinite loop in QTextLayout with setNumColumns() 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: 991c056438b311566bc4ea543af0f33dfd5dffbb. 49a63d375972079ae3000c8b7d512d58d4de32bb 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 Reviewed-by: Lars Knoll (cherry picked from commit be6c651be4ccfa4f70009bcbb51bef39638e0fba) Reviewed-by: Qt Cherry-pick Bot --- src/gui/text/qtextlayout.cpp | 2 +- .../gui/text/qtextlayout/tst_qtextlayout.cpp | 26 ++++++++++++++----- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/gui/text/qtextlayout.cpp b/src/gui/text/qtextlayout.cpp index 7e9dfbe3000..87c524ffc16 100644 --- a/src/gui/text/qtextlayout.cpp +++ b/src/gui/text/qtextlayout.cpp @@ -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); diff --git a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp index bea2556e68e..a0baa43cbcb 100644 --- a/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp +++ b/tests/auto/gui/text/qtextlayout/tst_qtextlayout.cpp @@ -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)