[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 08/12] util/hexdump: Replace sprintf() by g_string_append_printf(
From: |
Philippe Mathieu-Daudé |
Subject: |
[PATCH 08/12] util/hexdump: Replace sprintf() by g_string_append_printf() |
Date: |
Wed, 10 Apr 2024 18:06:09 +0200 |
sprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1,
resulting in painful developper experience.
Replace sprintf() by GString API in order to avoid:
[426/1310] Compiling C object libqemuutil.a.p/util_hexdump.c.o
util/hexdump.c:35:21: 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]
line += sprintf(line, " %02x", (unsigned char)buf[b + i]);
^
util/hexdump.c:37:21: warning: 'sprintf' is deprecated:
line += sprintf(line, " ");
^
2 warnings generated.
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
util/hexdump.c | 17 ++++++++---------
1 file changed, 8 insertions(+), 9 deletions(-)
diff --git a/util/hexdump.c b/util/hexdump.c
index b6f70e93bb..2ec1171de3 100644
--- a/util/hexdump.c
+++ b/util/hexdump.c
@@ -19,7 +19,7 @@
char *qemu_hexdump_line(const void *bufptr, unsigned offset,
unsigned int len, bool ascii)
{
- char linebuf[QEMU_HEXDUMP_LINE_BYTES], *line = linebuf;
+ g_autoptr(GString) gs = g_string_sized_new(QEMU_HEXDUMP_LINE_BYTES);
const char *buf = bufptr;
int i, c;
@@ -27,30 +27,29 @@ char *qemu_hexdump_line(const void *bufptr, unsigned offset,
len = QEMU_HEXDUMP_LINE_BYTES;
}
- line += snprintf(line, 6, "%04x:", offset);
+ g_string_append_printf(gs, "%04x:", offset);
for (i = 0; i < QEMU_HEXDUMP_LINE_BYTES; i++) {
if ((i % 4) == 0) {
- *line++ = ' ';
+ g_string_append_c(gs, ' ');
}
if (i < len) {
- line += sprintf(line, " %02x", (unsigned char)buf[offset + i]);
+ g_string_append_printf(gs, " %02x", (unsigned char)buf[offset +
i]);
} else {
- line += sprintf(line, " ");
+ g_string_append(gs, " ");
}
}
if (ascii) {
- *line++ = ' ';
+ g_string_append_c(gs, ' ');
for (i = 0; i < len; i++) {
c = buf[offset + i];
if (c < ' ' || c > '~') {
c = '.';
}
- *line++ = c;
+ g_string_append_c(gs, c);
}
}
- *line = '\0';
- return g_strdup(linebuf);
+ return g_strdup(gs->str);
}
void qemu_hexdump(FILE *fp, const char *prefix,
--
2.41.0
- Re: [PATCH 01/12] ui/console-vc: Replace sprintf() by g_strdup_printf(), (continued)
- [PATCH 02/12] hw/vfio/pci: Replace sprintf() by g_strdup_printf(), Philippe Mathieu-Daudé, 2024/04/10
- [PATCH 03/12] hw/ppc/spapr: Replace sprintf() by g_strdup_printf(), Philippe Mathieu-Daudé, 2024/04/10
- [PATCH 04/12] hw/mips/malta: Replace sprintf() by g_string_append_printf(), Philippe Mathieu-Daudé, 2024/04/10
- [PATCH 05/12] system/qtest: Replace sprintf() by g_string_append_printf(), Philippe Mathieu-Daudé, 2024/04/10
- [PATCH 06/12] util/hexdump: Rename @offset argument in qemu_hexdump_line(), Philippe Mathieu-Daudé, 2024/04/10
- [PATCH 07/12] util/hexdump: Have qemu_hexdump_line() return heap allocated buffer, Philippe Mathieu-Daudé, 2024/04/10
- [PATCH 08/12] util/hexdump: Replace sprintf() by g_string_append_printf(),
Philippe Mathieu-Daudé <=
- [PATCH 10/12] hw/ide/atapi: Use qemu_hexdump_line() to avoid sprintf(), Philippe Mathieu-Daudé, 2024/04/10
- [PATCH 09/12] hw/scsi/scsi-disk: Use qemu_hexdump_line() to avoid sprintf(), Philippe Mathieu-Daudé, 2024/04/10
- [PATCH 11/12] hw/dma/pl330: Use qemu_hexdump_line() to avoid sprintf(), Philippe Mathieu-Daudé, 2024/04/10
- [PATCH 12/12] backends/tpm: Use qemu_hexdump_line() to avoid sprintf(), Philippe Mathieu-Daudé, 2024/04/10
- Re: [PATCH 00/12] misc: Remove sprintf() due to macOS deprecation, Richard Henderson, 2024/04/10