qt6-bb10/src
Marc Mutz 73c44c8381 QLocale: fix UB (signed overflow) in formattedDataSize()
Coverity complains that qCountLeadingZeroBits() may return 64 and then
the RHS will be out-of-range for int. Of course, 64 leading zero bits
means that the argument is 0, which we excluded in the first `if` of
the if-else chain.

I can only guess (because it doesn't tell me which value of `bytes` it
is using for the analysis) that Coverity assumes bytes ==
numeric_limits<qint64>::min() and considers the overflow in qAbs() to
yield a 0, because, it being UB, it may yield anything.

Be that as it may, the fact remains that formattedDataSize() invokes
UB when passed numeric_limits<qint64>::min(), as confirmed by ubsan:

   global/qnumeric.h:479:26: runtime error: negation of -9223372036854775808 cannot be represented in type 'long long int'; cast to an unsigned type to negate this value to itself
   text/qlocale.cpp:5062:82: runtime error: signed integer overflow: -2147483648 * 3 cannot be represented in type 'int'
   text/qlocale.cpp:5062:26: runtime error: division by zero
   FAIL!  : tst_QLocale::formattedDataSize(English-Decimal-min) Compared values are not the same
      Actual   (QLocale(language).formattedDataSize(bytes, decimalPlaces, units)): "-inf bytes"
      Expected ("output")                                                        : "-9.22 EB"

So fix the overflow by using the new QtPrivate::qUnsignedAbs(). Then
sit back and await Coverity's verdict on the next run.

[ChangeLog][QtCore][QLocale] Fix issue when calling
formattedDataSize() with numeric_limits<qint64>::min().

Amends 9d23aebb27.

Pick-to: 6.5 6.2 5.15
Coverity-Id: 474294
Change-Id: I9a5a5acbdcf881a624bb827ca1dd970771f1bb6e
Reviewed-by: Shawn Rutledge <shawn.rutledge@qt.io>
Reviewed-by: Thiago Macieira <thiago.macieira@intel.com>
Reviewed-by: Edward Welbourne <edward.welbourne@qt.io>
(cherry picked from commit b93575de01760ff2ab0d817557a642c71cdb4414)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
(cherry picked from commit 6cefb8be965759cc022ce3ef1ae7a722f0566ce7)
2025-02-21 21:24:30 +00:00
..
3rdparty Update PCRE2 to 10.45 2025-02-18 11:30:02 +00:00
android Android: Fix cursorHandle and EditPopup positions 2025-02-10 21:26:32 +00:00
assets Tasktree: fix FTBFS due to unique_ptrs on forward-declared classes 2025-01-15 16:24:43 +00:00
concurrent Qt Concurrent documentation: fix Forward Iterator example container 2025-02-13 20:33:23 +00:00
corelib QLocale: fix UB (signed overflow) in formattedDataSize() 2025-02-21 21:24:30 +00:00
dbus CMake: Use the $<VERSION> placeholder in qt_attribution.json 2025-01-13 14:34:59 +01:00
entrypoint CMake: Correctly annotate the Entrypoint (winmain) SBOM license ID 2024-06-19 08:15:40 +00:00
gui Accept multiple fonts with the same family and style name 2025-02-21 00:03:46 +00:00
network Do not keep the headers and message body in case of temporary redirect 2025-02-21 00:03:44 +00:00
opengl Doc: Fix method docs for QOpenGLTexture::setWrapMode 2025-01-25 14:21:29 +00:00
openglwidgets Remove GENERATE_CPP_EXPORTS argument 2024-06-13 14:39:17 +00:00
platformsupport libinput: Fix developer build without xkbcommon 2025-02-17 20:27:51 +00:00
plugins wasm: call focus on window if not editable 2025-02-21 18:55:17 +00:00
printsupport Docs: Add that Qt Print Support is not available on Android 2025-01-22 02:25:28 +00:00
sql QtSql: fix GCC -Wextra-semi warnings after member function definitions 2024-12-20 10:40:03 +00:00
testlib QBenchmarkPerfEvents: help Coverity understand the code 2025-02-20 09:22:06 +00:00
tools Update PCRE2 to 10.45 2025-02-18 11:30:02 +00:00
widgets QScroller: Remove workaround for timer start 2025-02-19 04:10:22 +00:00
xml [QDomNodePrivate] delete unnecessary pointer check from the statement 2024-12-12 17:36:21 +00:00
CMakeLists.txt Add Qt icon library for examples 2023-04-22 18:14:38 +03:00