@samitouri / QOSamiQemu / commits / 2a58162a1e

tests: Clean up double comparisons to avoid compiler warning

To enable -Wformat-overflow=2, we need to clean up a couple of false positives: [2/5] Compiling C object tests/unit/test-qobject-output-visitor.p/test-qobject-output-visitor.c.o ../tests/unit/test-qobject-output-visitor.c: In function ‘test_visitor_out_list_struct’: ../tests/unit/test-qobject-output-visitor.c:577:28: error: ‘%.6f’ directive writing between 3 and 317 bytes into a region of size 32 [-Werror=format-overflow=] 577 | sprintf(expected, "%.6f", (double)i / 3); | ^~~~ ../tests/unit/test-qobject-output-visitor.c:577:27: note: assuming directive output of 8 bytes 577 | sprintf(expected, "%.6f", (double)i / 3); | ^~~~~~ In file included from /usr/include/stdio.h:970, from /home/me/q/var/qemu/include/qemu/osdep.h:114, from ../tests/unit/test-qobject-output-visitor.c:13: In function ‘sprintf’, inlined from ‘test_visitor_out_list_struct’ at ../tests/unit/test-qobject-output-visitor.c:577:9: /usr/include/bits/stdio2.h:30:10: note: ‘__builtin___sprintf_chk’ output between 4 and 318 bytes into a destination of size 32 30 | return __builtin___sprintf_chk (__s, __USE_FORTIFY_LEVEL - 1, | ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 31 | __glibc_objsize (__s), __fmt, | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 32 | __va_arg_pack ()); | ~~~~~~~~~~~~~~~~~ cc1: all warnings being treated as errors FAILED: tests/unit/test-qobject-output-visitor.p/test-qobject-output-visitor.c.o These buffers cannot actually overflow because the doubles are between 0 and 31.0/3 inclusive. However, formatting doubles just to compare them is silly. Compare them directly instead. To avoid potential rounding trouble, change the numbers tested to be representable exactly in double. Signed-off-by: Akihiko Odaki <odaki@rsg.ci.i.u-tokyo.ac.jp> Reviewed-by: Markus Armbruster <armbru@redhat.com> Message-ID: <20260305-nvme-v4-3-b65b9de1839f@rsg.ci.i.u-tokyo.ac.jp> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>

Akihiko Odaki committed Mar 5, 2026 at 15:16 UTC 2a58162a1e4545cf26e77c139c46d7a644b2318b
2 files changed +4 -11
tests/unit/test-qobject-input-visitor.c
+2 -6
@@ -500,7 +500,7 @@ static void test_visitor_in_list_struct(TestInputVisitorData *data,
500 g_string_append_printf(json, "'number': [");
501 sep = "";
502 for (i = 0; i < 32; i++) {
503 - g_string_append_printf(json, "%s%f", sep, (double)i / 3);
503 + g_string_append_printf(json, "%s%f", sep, (double)i / FLT_RADIX);
504 sep = ", ";
505 }
506 g_string_append_printf(json, "], ");
@@ -583,11 +583,7 @@ static void test_visitor_in_list_struct(TestInputVisitorData *data,
583
584 i = 0;
585 for (num_list = arrs->number; num_list; num_list = num_list->next) {
586 - char expected[32], actual[32];
587 -
588 - sprintf(expected, "%.6f", (double)i / 3);
589 - sprintf(actual, "%.6f", num_list->value);
590 - g_assert_cmpstr(expected, ==, actual);
586 + g_assert_cmpfloat(num_list->value, ==, (double)i / FLT_RADIX);
587 i++;
588 }
589
tests/unit/test-qobject-output-visitor.c
+2 -5
@@ -538,7 +538,7 @@ static void test_visitor_out_list_struct(TestOutputVisitorData *data,
538 }
539
540 for (i = 31; i >= 0; i--) {
541 - QAPI_LIST_PREPEND(arrs->number, (double)i / 3);
541 + QAPI_LIST_PREPEND(arrs->number, (double)i / FLT_RADIX);
542 }
543
544 for (i = 31; i >= 0; i--) {
@@ -571,12 +571,9 @@ static void test_visitor_out_list_struct(TestOutputVisitorData *data,
571 i = 0;
572 QLIST_FOREACH_ENTRY(qlist, e) {
573 QNum *qvalue = qobject_to(QNum, qlist_entry_obj(e));
574 - char expected[32], actual[32];
574
575 g_assert(qvalue);
577 - sprintf(expected, "%.6f", (double)i / 3);
578 - sprintf(actual, "%.6f", qnum_get_double(qvalue));
579 - g_assert_cmpstr(actual, ==, expected);
576 + g_assert_cmpfloat(qnum_get_double(qvalue), ==, (double)i / FLT_RADIX);
577 i++;
578 }
579