@samitouri / QOSamiQemu / commits / 62272f9f88

tests/qtest/arm-cpu-features: Use g_strdup_printf() instead of char arrays

Older versions of gcc with -Wformat-overflow=2 don't like the usage of fixed size char arrays in this test; gcc 7.5.0 (SUSE Linux) says: ../tests/qtest/arm-cpu-features.c: In function ‘test_query_cpu_model_expansion_kvm’: ../tests/qtest/arm-cpu-features.c:578:35: error: ‘%u’ directive writing between 1 and 10 bytes into a region of size 5 [-Werror=format-overflow=] These can't actually happen because of the limited size of the values being substituted in. However rather than require readers to check whether the arrays really have been declared large enough, we prefer to use g_strdup_printf() for this kind of string work. Reported-by: Fabiano Rosas <farosas@suse.de> Signed-off-by: Peter Maydell <peter.maydell@linaro.org> Tested-by: Fabiano Rosas <farosas@suse.de> Reviewed-by: Cédric Le Goater <clg@redhat.com> Reviewed-by: Philippe Mathieu-Daudé <philmd@linaro.org> Message-id: 20260310151507.2973843-3-peter.maydell@linaro.org

Peter Maydell committed Mar 10, 2026 at 15:15 UTC 62272f9f8891d2f6f9a210cdc607a263aa40531b
1 file changed +15 -9
tests/qtest/arm-cpu-features.c
+15 -9
@@ -519,7 +519,6 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
519 bool kvm_supports_pmu;
520 bool kvm_supports_steal_time;
521 bool kvm_supports_sve;
522 - char max_name[8], name[8];
522 uint32_t max_vq, vq;
523 uint64_t vls;
524 QDict *resp;
@@ -573,9 +572,12 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
572 }
573
574 if (kvm_supports_sve) {
575 + g_autofree const char *max_name = NULL;
576 + g_autofree const char *name = NULL;
577 +
578 g_assert(vls != 0);
579 max_vq = 64 - __builtin_clzll(vls);
578 - sprintf(max_name, "sve%u", max_vq * 128);
580 + max_name = g_strdup_printf("sve%u", max_vq * 128);
581
582 /* Enabling a supported length is of course fine. */
583 assert_sve_vls(qts, "host", vls, "{ %s: true }", max_name);
@@ -583,6 +585,9 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
585 /* Get the next supported length smaller than max-vq. */
586 vq = 64 - __builtin_clzll(vls & ~BIT_ULL(max_vq - 1));
587 if (vq) {
588 + g_autofree const char *name2 =
589 + g_strdup_printf("sve%u", vq * 128);
590 +
591 /*
592 * We have at least one length smaller than max-vq,
593 * so we can disable max-vq.
@@ -595,11 +600,10 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
600 * unless all larger, supported vector lengths are also
601 * disabled.
602 */
598 - sprintf(name, "sve%u", vq * 128);
599 - error = g_strdup_printf("cannot disable %s", name);
603 + error = g_strdup_printf("cannot disable %s", name2);
604 assert_error(qts, "host", error,
605 "{ %s: true, %s: false }",
602 - max_name, name);
606 + max_name, name2);
607 g_free(error);
608 }
609
@@ -608,7 +612,7 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
612 * we need at least one vector length enabled.
613 */
614 vq = __builtin_ffsll(vls);
611 - sprintf(name, "sve%u", vq * 128);
615 + name = g_strdup_printf(name, "sve%u", vq * 128);
616 error = g_strdup_printf("cannot disable %s", name);
617 assert_error(qts, "host", error, "{ %s: false }", name);
618 g_free(error);
@@ -620,9 +624,11 @@ static void test_query_cpu_model_expansion_kvm(const void *data)
624 }
625 }
626 if (vq <= SVE_MAX_VQ) {
623 - sprintf(name, "sve%u", vq * 128);
624 - error = g_strdup_printf("cannot enable %s", name);
625 - assert_error(qts, "host", error, "{ %s: true }", name);
627 + g_autofree const char *name2 =
628 + g_strdup_printf("sve%u", vq * 128);
629 +
630 + error = g_strdup_printf("cannot enable %s", name2);
631 + assert_error(qts, "host", error, "{ %s: true }", name2);
632 g_free(error);
633 }
634 } else {