Skip to contentSkip to conversation

buildsys: Replace some deprecated sprintf()

PATCH Superseded Philippe Mathieu-Daudé 2 participants 7 messages Nov 4

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

cover buildsys: Replace some deprecated sprintf()

Since some time now we get the build output cluttered with many deprecation warnings, it is becoming hard to notice the real ones. This series fixes few recurrent use by the --disable-tcg --enable-hvf build.

(Yes, VOF is not used on HVF x86/ARM64, I haven't
investigated why it is selected and built).

Philippe Mathieu-Daudé (5):
tests/qtest: Use qemu_hexdump_to_buffer() in qtest_memwrite() tests/qtest/npcm7xx: Replace deprecated sprintf() with snprintf() tests/unit: Replace deprecated sprintf() with snprintf() hw/ppc/vof: Replace deprecated sprintf() with snprintf() hw/usb/hcd-ohci: Replace deprecated sprintf() with snprintf()

 hw/ppc/vof.c                             |  2 +-
 hw/usb/hcd-ohci.c                        |  2 +-
 tests/qtest/libqtest.c                   |  6 +-----
 tests/qtest/npcm7xx_adc-test.c           |  5 +++--
 tests/qtest/npcm7xx_pwm-test.c           |  8 ++++----
 tests/unit/test-qobject-input-visitor.c  |  2 +-
 tests/unit/test-qobject-output-visitor.c |  2 +-
 tests/unit/test-visitor-serialization.c  | 19 +++++++++++--------
 8 files changed, 23 insertions(+), 23 deletions(-)
Signature · 1 line
2.55.0
#1 lore raw

PATCH 1/5 tests/qtest: Use qemu_hexdump_to_buffer() in qtest_memwrite()

Replace sprintf("%02x) by equivalent qemu_hexdump_to_buffer() helper and silent when using Apple clang 17.0.0:

../tests/qtest/libqtest.c:1522:9: warning: 'sprintf' is deprecated:
This function is provided for compatibility reasons only. Due to security concerns inherent in the design of sprintf(3), it is highly recommended that you use snprintf(3) instead. [-Wdeprecated-declarations]

---
 tests/qtest/libqtest.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
tests/qtest/libqtest.c +1 -5
index 7f66c1bbb3b6..b6b9f8fe161d 100644
@@ -1509,7 +1509,6 @@ void qtest_bufread(QTestState *s, uint64_t addr, void *data, size_t size)
 void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
 {
     const uint8_t *ptr = data;
-    size_t i;
     char *enc;
 
     if (!size) {
@@ -1517,10 +1516,7 @@ void qtest_memwrite(QTestState *s, uint64_t addr, const void *data, size_t size)
     }
 
     enc = g_malloc(2 * size + 1);
-
-    for (i = 0; i < size; i++) {
-        sprintf(&enc[i * 2], "%02x", ptr[i]);
-    }
+    qemu_hexdump_to_buffer(enc, 2 * size + 1, ptr, size);
 
     qtest_sendf(s, "write 0x%" PRIx64 " 0x%zx 0x%s\n", addr, size, enc);
     qtest_rsp(s);
Signature · 1 line
2.55.0
#2 lore raw

PATCH 2/5 tests/qtest/npcm7xx: Replace deprecated sprintf() with snprintf()

In reply to Philippe Mathieu-Daudé

Replace sprintf() by safer snprintf() to fill the QOM path and property names. This silences when using Apple clang 17.0.0:

../tests/qtest/npcm7xx_adc-test.c:140:5: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/qtest/npcm7xx_adc-test.c:322:9: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/qtest/npcm7xx_pwm-test.c:244:5: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/qtest/npcm7xx_pwm-test.c:245:5: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/qtest/npcm7xx_pwm-test.c:255:5: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/qtest/npcm7xx_pwm-test.c:256:5: warning: 'sprintf' is deprecated [-Wdeprecated-declarations]

---
 tests/qtest/npcm7xx_adc-test.c | 5 +++--
 tests/qtest/npcm7xx_pwm-test.c | 8 ++++----
 2 files changed, 7 insertions(+), 6 deletions(-)
tests/qtest/npcm7xx_adc-test.c +3 -2
index 8bc89b8a8b9a..f2fee1c32c81 100644
@@ -137,7 +137,7 @@ static void adc_write_input(QTestState *qts, const ADC *adc,
 {
     char name[100];
 
-    sprintf(name, "adci[%u]", index);
+    snprintf(name, sizeof(name), "adci[%u]", index);
     adc_qom_set(qts, adc, name, value);
 }
 
@@ -319,7 +319,8 @@ static void test_calibrate(gconstpointer adc_p)
         char buf[100];
         QTestState *qts;
 
-        sprintf(buf, "-machine quanta-gsj -global npcm7xx-adc.iref=%u", iref);
+        snprintf(buf, sizeof(buf),
+                 "-machine quanta-gsj -global npcm7xx-adc.iref=%u", iref);
         qts = qtest_init(buf);
 
         /* Check the converted value is correct using the calibration value. */
tests/qtest/npcm7xx_pwm-test.c +4 -4
index 052ea876621a..cf24ba8de346 100644
@@ -241,8 +241,8 @@ static uint64_t pwm_get_freq(QTestState *qts, int module_index, int pwm_index)
     char path[100];
     char name[100];
 
-    sprintf(path, "/machine/soc/pwm[%d]", module_index);
-    sprintf(name, "freq[%d]", pwm_index);
+    snprintf(path, sizeof(path), "/machine/soc/pwm[%d]", module_index);
+    snprintf(name, sizeof(name), "freq[%d]", pwm_index);
 
     return pwm_qom_get(qts, path, name);
 }
@@ -252,8 +252,8 @@ static uint64_t pwm_get_duty(QTestState *qts, int module_index, int pwm_index)
     char path[100];
     char name[100];
 
-    sprintf(path, "/machine/soc/pwm[%d]", module_index);
-    sprintf(name, "duty[%d]", pwm_index);
+    snprintf(path, sizeof(path), "/machine/soc/pwm[%d]", module_index);
+    snprintf(name, sizeof(name), "duty[%d]", pwm_index);
 
     return pwm_qom_get(qts, path, name);
 }
Signature · 1 line
2.55.0
#3 lore raw

Re: [PATCH 2/5] tests/qtest/npcm7xx: Replace deprecated sprintf() with snprintf()

Quoted 11 lines · Philippe Mathieu-Daudé

Replace sprintf() by safer snprintf() to fill the QOM path and property names. This silences when using Apple clang 17.0.0:

../tests/qtest/npcm7xx_adc-test.c:140:5: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/qtest/npcm7xx_adc-test.c:322:9: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/qtest/npcm7xx_pwm-test.c:244:5: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/qtest/npcm7xx_pwm-test.c:245:5: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/qtest/npcm7xx_pwm-test.c:255:5: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/qtest/npcm7xx_pwm-test.c:256:5: warning: 'sprintf' is deprecated [-Wdeprecated-declarations]

Quoted 5 lines
---
 tests/qtest/npcm7xx_adc-test.c | 5 +++--
 tests/qtest/npcm7xx_pwm-test.c | 8 ++++----
 2 files changed, 7 insertions(+), 6 deletions(-)

g_strdup_printf() would be the preferred upgrade here for all of these, I think. That gets rid of the fixed-sized buffers entirely.

thanks
-- PMM

#4 lore raw

PATCH 3/5 tests/unit: Replace deprecated sprintf() with snprintf()

In reply to Philippe Mathieu-Daudé

Replace sprintf() by safer snprintf() to avoid when using Apple clang 17.0.0 on macOS ARM64:

../tests/unit/test-qobject-output-visitor.c:598:9: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/unit/test-qobject-input-visitor.c:600:9: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/unit/test-visitor-serialization.c:1031:5: warning: 'sprintf' is deprecated [-Wdeprecated-declarations] ../tests/unit/test-visitor-serialization.c:1035:13: warning: 'sprintf' is deprecated [-Wdeprecated-declarations]
...

---
 tests/unit/test-qobject-input-visitor.c  |  2 +-
 tests/unit/test-qobject-output-visitor.c |  2 +-
 tests/unit/test-visitor-serialization.c  | 19 +++++++++++--------
 3 files changed, 13 insertions(+), 10 deletions(-)
tests/unit/test-qobject-input-visitor.c +1 -1
index beee11db4e47..41d3dd983b3c 100644
@@ -597,7 +597,7 @@ static void test_visitor_in_list_struct(TestInputVisitorData *data,
     for (str_list = arrs->string; str_list; str_list = str_list->next) {
         char expected[32];
 
-        sprintf(expected, "%d", i);
+        snprintf(expected, sizeof(expected), "%d", i);
         g_assert_cmpstr(str_list->value, ==, expected);
         i++;
     }
tests/unit/test-qobject-output-visitor.c +1 -1
index 3c47b28f6638..ff571cd91ed6 100644
@@ -595,7 +595,7 @@ static void test_visitor_out_list_struct(TestOutputVisitorData *data,
         char expected[32];
 
         g_assert(qvalue);
-        sprintf(expected, "%d", i);
+        snprintf(expected, sizeof(expected), "%d", i);
         g_assert_cmpstr(qstring_get_str(qvalue), ==, expected);
         i++;
     }
tests/unit/test-visitor-serialization.c +11 -8
index 2d365999fc82..21755d27036b 100644
@@ -1028,12 +1028,13 @@ static void add_visitor_type(const SerializeOps *ops)
     TestArgs *args;
     int i = 0;
 
-    sprintf(testname_prefix, "/visitor/serialization/%s", ops->type);
+    snprintf(testname_prefix, sizeof(testname_prefix),
+             "/visitor/serialization/%s", ops->type);
 
     if (ops->caps & VCAP_PRIMITIVES) {
         while (pt_values[i].type != PTYPE_EOL) {
-            sprintf(testname, "%s/primitives/%s", testname_prefix,
-                    pt_values[i].description);
+            snprintf(testname, sizeof(testname), "%s/primitives/%s",
+                     testname_prefix, pt_values[i].description);
             args = g_malloc0(sizeof(*args));
             args->ops = ops;
             args->test_data = &pt_values[i];
@@ -1043,13 +1044,14 @@ static void add_visitor_type(const SerializeOps *ops)
     }
 
     if (ops->caps & VCAP_STRUCTURES) {
-        sprintf(testname, "%s/struct", testname_prefix);
+        snprintf(testname, sizeof(testname), "%s/struct", testname_prefix);
         args = g_malloc0(sizeof(*args));
         args->ops = ops;
         args->test_data = NULL;
         g_test_add_data_func(testname, args, test_struct);
 
-        sprintf(testname, "%s/nested_struct", testname_prefix);
+        snprintf(testname, sizeof(testname),
+                 "%s/nested_struct", testname_prefix);
         args = g_malloc0(sizeof(*args));
         args->ops = ops;
         args->test_data = NULL;
@@ -1057,7 +1059,8 @@ static void add_visitor_type(const SerializeOps *ops)
     }
 
     if (ops->caps & VCAP_LISTS) {
-        sprintf(testname, "%s/nested_struct_list", testname_prefix);
+        snprintf(testname, sizeof(testname),
+                 "%s/nested_struct_list", testname_prefix);
         args = g_malloc0(sizeof(*args));
         args->ops = ops;
         args->test_data = NULL;
@@ -1067,8 +1070,8 @@ static void add_visitor_type(const SerializeOps *ops)
     if (ops->caps & VCAP_PRIMITIVE_LISTS) {
         i = 0;
         while (pt_values[i].type != PTYPE_EOL) {
-            sprintf(testname, "%s/primitive_list/%s", testname_prefix,
-                    pt_values[i].description);
+            snprintf(testname, sizeof(testname), "%s/primitive_list/%s",
+                     testname_prefix, pt_values[i].description);
             args = g_malloc0(sizeof(*args));
             args->ops = ops;
             args->test_data = &pt_values[i];
Signature · 1 line
2.55.0
#5 lore raw

PATCH 4/5 hw/ppc/vof: Replace deprecated sprintf() with snprintf()

In reply to Philippe Mathieu-Daudé

Replace sprintf() by safer snprintf() to avoid when using Apple clang 17.0.0 on macOS ARM64:

../hw/ppc/vof.c:104:14: warning: 'sprintf' is deprecated [-Wdeprecated-declarations]

---
 hw/ppc/vof.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/vof.c +1 -1
index 873e967f1e4d..ef89b71f7336 100644
@@ -101,7 +101,7 @@ static void prop_format(char *tval, int tlen, const void *prop, int len)
             strcat(t, " ");
             ++t;
         }
-        t += sprintf(t, "%02X", *c & 0xFF);
+        t += snprintf(t, tval + tlen - t, "%02X", *c & 0xFF);
     }
 }
 
Signature · 1 line
2.55.0
#6 lore raw

PATCH 5/5 hw/usb/hcd-ohci: Replace deprecated sprintf() with snprintf()

In reply to Philippe Mathieu-Daudé

Replace sprintf() by safer snprintf() to avoid when using Apple clang 17.0.0 on macOS ARM64:

../hw/usb/hcd-ohci.c:875:14: warning: 'sprintf' is deprecated [-Wdeprecated-declarations]

---
 hw/usb/hcd-ohci.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/usb/hcd-ohci.c +1 -1
index 4d38150a8458..d9fcee6c0b4b 100644
@@ -872,7 +872,7 @@ static void ohci_td_pkt(const char *msg, const uint8_t *buf, size_t len)
             break;
         }
 
-        p += sprintf(p, " %.2x", buf[i]);
+        p += snprintf(p, tmp + sizeof(tmp) - p, " %.2x", buf[i]);
     }
 }
 
Signature · 1 line
2.55.0