From ff0f038ea68182074b2aed281ca8f5890356c4fb Mon Sep 17 00:00:00 2001 From: Giuseppe D'Angelo Date: Thu, 30 Jan 2025 16:07:23 +0100 Subject: [PATCH] QRect: warn about int overflows in debug message We now print the correct width/height of a rectangle, but we should warn users that the rectangle so built is overflowing the `int` representation. Amends 0f336500a0add3e3a8bb31c5cc605e5e13e23833. Change-Id: Iaed0f3c7457e37e80a9d5efad940d498aa059bd4 Reviewed-by: Marc Mutz Reviewed-by: Shawn Rutledge --- src/corelib/io/qdebug_p.h | 4 ++++ tests/auto/corelib/tools/qrect/tst_qrect.cpp | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/src/corelib/io/qdebug_p.h b/src/corelib/io/qdebug_p.h index 9b048ae9ab9..732929e7215 100644 --- a/src/corelib/io/qdebug_p.h +++ b/src/corelib/io/qdebug_p.h @@ -52,6 +52,10 @@ static inline void formatQRect(QDebug &debug, const Rect &rect) const qint64 w = qint64(rect.right()) - rect.left() + 1; const qint64 h = qint64(rect.bottom()) - rect.top() + 1; debug << w << 'x' << h; + + constexpr qint64 M = (std::numeric_limits::max)(); + if (w > M || h > M) + debug << " (oversized)"; } else { debug << rect.width() << 'x' << rect.height(); } diff --git a/tests/auto/corelib/tools/qrect/tst_qrect.cpp b/tests/auto/corelib/tools/qrect/tst_qrect.cpp index ebf1d1b38bd..d7312ffc322 100644 --- a/tests/auto/corelib/tools/qrect/tst_qrect.cpp +++ b/tests/auto/corelib/tools/qrect/tst_qrect.cpp @@ -4526,7 +4526,7 @@ void tst_QRect::debug() str.clear(); debug << QRect(QPoint(INT_MIN, INT_MIN), QPoint(INT_MAX, INT_MAX)); - QCOMPARE(str, "QRect(-2147483648,-2147483648 4294967296x4294967296)"); + QCOMPARE(str, "QRect(-2147483648,-2147483648 4294967296x4294967296 (oversized))"); } QTEST_MAIN(tst_QRect)