From d0f2635e1000368898832fa19a8d4be1203f6d7d Mon Sep 17 00:00:00 2001 From: Albert Astals Cid Date: Mon, 3 Mar 2025 21:51:03 +0100 Subject: [PATCH] sct: qRound with param bigger than max int is undefined oss-fuzz/399667098 --- src/imageformats/sct.cpp | 8 ++++---- src/imageformats/util_p.h | 9 +++++++++ 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/src/imageformats/sct.cpp b/src/imageformats/sct.cpp index 51b1aab..5b7f923 100644 --- a/src/imageformats/sct.cpp +++ b/src/imageformats/sct.cpp @@ -207,10 +207,10 @@ public: auto v = QString::fromLatin1(pchar_t(res.data()), res.size()).toDouble(&ok); if (ok && v > 0) { if (m_pb._unitsOfMeasurement) { // Inches - return qRound(width() / v / 25.4 * 1000); + return qRoundOrZero(width() / v / 25.4 * 1000); } // Millimeters - return qRound(width() / v * 1000); + return qRoundOrZero(width() / v * 1000); } return 0; } @@ -221,10 +221,10 @@ public: auto v = QString::fromLatin1(pchar_t(res.data()), res.size()).toDouble(&ok); if (ok && v > 0) { if (m_pb._unitsOfMeasurement) { // Inches - return qRound(height() / v / 25.4 * 1000); + return qRoundOrZero(height() / v / 25.4 * 1000); } // Millimeters - return qRound(height() / v * 1000); + return qRoundOrZero(height() / v * 1000); } return 0; } diff --git a/src/imageformats/util_p.h b/src/imageformats/util_p.h index 225c0b5..d88dec3 100644 --- a/src/imageformats/util_p.h +++ b/src/imageformats/util_p.h @@ -60,4 +60,13 @@ inline QImage imageAlloc(qint32 width, qint32 height, const QImage::Format &form return imageAlloc(QSize(width, height), format); } +inline double qRoundOrZero(double d) +{ + // If the value d is outside the range of int, the behavior is undefined. + if (d > std::numeric_limits::max()) { + return 0; + } + return qRound(d); +} + #endif // UTIL_P_H