From e64fd05fecae291c9d7358d2e47d7170995af256 Mon Sep 17 00:00:00 2001 From: Marc Mutz Date: Thu, 5 Dec 2024 11:39:53 +0100 Subject: [PATCH] tst_QString: extend arg() tests with enums w/o explicit underlying_type MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The QtDeclarative code causing QTBUG-131906 hits UB, because it tries to store -666 in an enum {A, B}, which has a valid range of [0,1], therefore its underlying_type is uint, yet, as per [conv.prom]/3¹, integer-promotes to _int_ instead, so in Qt 6.8 would cause the arg(int) overload to be called, outputting -666, while in Qt 6.9, it's treated (correctly) as an unsigned value, outputting -666's two's complement, a positive value. Add a version of the scenario that does not cause UB. ¹ Thanks to Ahmad Samir for digging up the pertinent legalese. Task-number: QTBUG-131906 Pick-to: 6.9 6.8 6.5 Change-Id: Iba1a04de523a0b4cd1c87deea40c643cf16df14f Reviewed-by: Thiago Macieira Reviewed-by: Fabian Kosmale --- tests/auto/corelib/text/qstring/tst_qstring.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/auto/corelib/text/qstring/tst_qstring.cpp b/tests/auto/corelib/text/qstring/tst_qstring.cpp index d4a041a6c15..051161d1f92 100644 --- a/tests/auto/corelib/text/qstring/tst_qstring.cpp +++ b/tests/auto/corelib/text/qstring/tst_qstring.cpp @@ -6695,6 +6695,11 @@ void tst_QString::arg() QLatin1String("[9223372036854775808]") ); // (unscoped) enums + enum Enum { + Foo1, Foo2, // reproducer for QTBUG-131906, + RangeExtended = -667, // but w/o the UB of out-of-range values + }; + QCOMPARE(s4.arg(Enum(-666)), QLatin1String("[-666]")); enum : int { FooS = -1 }; enum : uint { FooU = 1 }; QCOMPARE(s4.arg(FooS), QLatin1String("[-1]"));