@samitouri / QOSamiQemu / commits / 632d7bc0ec

tests/qtest/ast2700-sgpio-test: 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/ast2700-sgpio-test.c: In function ‘test_output_pins’: ../tests/qtest/ast2700-sgpio-test.c:27:33: error: ‘sprintf’ may write a terminating nul past the end of the destination [-Werror=format-overflow=] ../tests/qtest/ast2700-sgpio-test.c: In function ‘test_irq_level_high’: ../tests/qtest/ast2700-sgpio-test.c:85:33: error: ‘sprintf’ may write a terminating nul past the end of the destination [-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> Message-id: 20260310151507.2973843-2-peter.maydell@linaro.org

Peter Maydell committed Mar 10, 2026 at 15:15 UTC 632d7bc0ec4f94936c9e6a1bd46cacd3c044d70d
1 file changed +12 -12
tests/qtest/ast2700-sgpio-test.c
+12 -12
@@ -18,14 +18,14 @@
18 static void test_output_pins(const char *machine, const uint32_t base, int idx)
19 {
20 QTestState *s = qtest_init(machine);
21 - char name[16];
22 - char qom_path[64];
21 uint32_t offset = 0;
22 uint32_t value = 0;
23 for (int i = 0; i < ASPEED_SGPIO_MAX_PIN_PAIR; i++) {
24 /* Odd index is output port */
27 - sprintf(name, "sgpio%03d", i * 2 + 1);
28 - sprintf(qom_path, "/machine/soc/sgpio[%d]", idx);
25 + g_autofree const char *name = g_strdup_printf("sgpio%03d", i * 2 + 1);
26 + g_autofree const char *qom_path
27 + = g_strdup_printf("/machine/soc/sgpio[%d]", idx);
28 +
29 offset = base + (R_SGPIO_0_CONTROL + i) * 4;
30 /* set serial output */
31 qtest_writel(s, offset, 0x00000001);
@@ -45,14 +45,14 @@ static void test_output_pins(const char *machine, const uint32_t base, int idx)
45 static void test_input_pins(const char *machine, const uint32_t base, int idx)
46 {
47 QTestState *s = qtest_init(machine);
48 - char name[16];
49 - char qom_path[64];
48 uint32_t offset = 0;
49 uint32_t value = 0;
50 for (int i = 0; i < ASPEED_SGPIO_MAX_PIN_PAIR; i++) {
51 /* Even index is input port */
54 - sprintf(name, "sgpio%03d", i * 2);
55 - sprintf(qom_path, "/machine/soc/sgpio[%d]", idx);
52 + g_autofree const char *name = g_strdup_printf("sgpio%03d", i * 2);
53 + g_autofree const char *qom_path
54 + = g_strdup_printf("/machine/soc/sgpio[%d]", idx);
55 +
56 offset = base + (R_SGPIO_0_CONTROL + i) * 4;
57 /* set serial input */
58 qtest_qom_set_bool(s, qom_path, name, true);
@@ -73,8 +73,6 @@ static void test_irq_level_high(const char *machine,
73 const uint32_t base, int idx)
74 {
75 QTestState *s = qtest_init(machine);
76 - char name[16];
77 - char qom_path[64];
76 uint32_t ctrl_offset = 0;
77 uint32_t int_offset = 0;
78 uint32_t int_reg_idx = 0;
@@ -82,8 +80,10 @@ static void test_irq_level_high(const char *machine,
80 uint32_t value = 0;
81 for (int i = 0; i < ASPEED_SGPIO_MAX_PIN_PAIR; i++) {
82 /* Even index is input port */
85 - sprintf(name, "sgpio%03d", i * 2);
86 - sprintf(qom_path, "/machine/soc/sgpio[%d]", idx);
83 + g_autofree const char *name = g_strdup_printf("sgpio%03d", i * 2);
84 + g_autofree const char *qom_path =
85 + g_strdup_printf("/machine/soc/sgpio[%d]", idx);
86 +
87 int_reg_idx = i / 32;
88 int_bit_idx = i % 32;
89 int_offset = base + (R_SGPIO_INT_STATUS_0 + int_reg_idx) * 4;