misc: Remove sprintf() due to macOS deprecation
v2 cover misc: Remove sprintf() due to macOS deprecation
Since v1:
- Use snprintf() in patches 1-5
Hi,
sprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.
Suggestion to avoid the super-noisy warning on macOS forum are [*]:
* use -Wno-deprecated-declarations on the whole build
* surgically add #pragma clang diagnostic around each use.
None of these options seem reasonable, so we are somehow forced to spend time converting each sprintf() call, even if they are safe enough.
I'm so tired of seeing them than I started the conversion. This is the first part. Help for the rest is welcomed.
Regards,
Phil.
[*] https://forums.developer.apple.com/forums/thread/714675
Philippe Mathieu-Daudé (13):
ui/console-vc: Replace sprintf() by snprintf() hw/vfio/pci: Replace sprintf() by snprintf() hw/ppc/spapr: Replace sprintf() by snprintf() hw/mips/malta: Add re-usable rng_seed_hex_new() method hw/mips/malta: Replace sprintf() by snprintf() system/qtest: Replace sprintf() by g_string_append_printf() util/hexdump: Rename @offset argument in qemu_hexdump_line() util/hexdump: Have qemu_hexdump_line() return heap allocated buffer util/hexdump: Replace sprintf() by g_string_append_printf() hw/scsi/scsi-disk: Use qemu_hexdump_line() to avoid sprintf() hw/ide/atapi: Use qemu_hexdump_line() to avoid sprintf() hw/dma/pl330: Use qemu_hexdump_line() to avoid sprintf() backends/tpm: Use qemu_hexdump_line() to avoid sprintf()
include/qemu/cutils.h | 17 ++++++++++++++--- backends/tpm/tpm_util.c | 24 ++++++++---------------- hw/dma/pl330.c | 12 +++--------- hw/ide/atapi.c | 8 ++------ hw/mips/malta.c | 23 ++++++++++++++--------- hw/ppc/spapr.c | 2 +- hw/scsi/scsi-disk.c | 8 ++------ hw/vfio/pci.c | 2 +- hw/virtio/vhost-vdpa.c | 11 ++++++----- system/qtest.c | 8 +++----- ui/console-vc.c | 2 +- util/hexdump.c | 33 ++++++++++++++++++--------------- 12 files changed, 73 insertions(+), 77 deletions(-)
Signature · 1 line
2.41.0
PATCH v2 1/13 ui/console-vc: Replace sprintf() by snprintf()
sprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.
Replace sprintf() by snprintf() in order to avoid:
[702/1310] Compiling C object libcommon.fa.p/ui_console-vc.c.o ui/console-vc.c:824: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]
sprintf(response, "\033[%d;%dR",
^
1 warning generated.
--- ui/console-vc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
ui/console-vc.c +1 -1
@@ -821,7 +821,7 @@ static void vc_putchar(VCChardev *vc, int ch)break ;case 6 :/* report cursor position */ -sprintf (response,"\033[%d;%dR" , +snprintf (response,sizeof (response),"\033[%d;%dR" , (s->y_base + s->y) % s->total_height +1 , s->x +1 );vc_respond_str (vc, response);
Signature · 1 line
2.41.0
Re: [PATCH v2 01/13] ui/console-vc: Replace sprintf() by snprintf()
Quoted 16 lines · Philippe Mathieu-Daudé
sprintf()is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.Replace
sprintf()bysnprintf()in order to avoid:[702/1310] Compiling C object
libcommon.fa.p/ui_console-vc.c.oui/console-vc.c:824: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]
sprintf(response, "\033[%d;%dR",
^
1 warning generated.
Quoted 1 line
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
Quoted 20 lines
--- ui/console-vc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)ui/console-vc.c +1
-2@@ -821,7 +821,7 @@ static void vc_putchar(VCChardev *vc, int ch)break ;case 6 :/* report cursor position */ -sprintf (response,"\033[%d;%dR" , +snprintf (response,sizeof (response),"\033[%d;%dR" , (s->y_base + s->y) % s->total_height +1 , s->x +1 );vc_respond_str (vc, response); --2.41.0
Re: [PATCH v2 01/13] ui/console-vc: Replace sprintf() by snprintf()
In reply to Philippe Mathieu-DaudéQuoted 15 lines · Philippe Mathieu-Daudé
sprintf()is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.Replace
sprintf()bysnprintf()in order to avoid:[702/1310] Compiling C object
libcommon.fa.p/ui_console-vc.c.oui/console-vc.c:824: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]
sprintf(response, "\033[%d;%dR",
^
1 warning generated.
Quoted 4 lines
Signed-off-by Philippe Mathieu-Daudé<[email protected]>--- ui/console-vc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
r~
PATCH v2 2/13 hw/vfio/pci: Replace sprintf() by snprintf()
In reply to Philippe Mathieu-Daudésprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience. Use snprintf() instead.
--- hw/vfio/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/vfio/pci.c +1 -1
@@ -2444,7 +2444,7 @@ bool vfio_pci_host_match(PCIHostDeviceAddress *addr, const char *name) {char tmp[13 ]; -sprintf (tmp,"%04x:%02x:%02x.%1x" , addr->domain, +snprintf (tmp,sizeof (tmp),"%04x:%02x:%02x.%1x" , addr->domain, addr->bus, addr->slot, addr->function);return (strcmp (tmp, name) ==0 );
Signature · 1 line
2.41.0
Re: [PATCH v2 02/13] hw/vfio/pci: Replace sprintf() by snprintf()
Quoted 7 lines · Philippe Mathieu-Daudé
sprintf()is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience. Usesnprintf()instead.Signed-off-by Philippe Mathieu-Daudé<[email protected]>--- hw/vfio/pci.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
r~
PATCH v2 3/13 hw/ppc/spapr: Replace sprintf() by snprintf()
In reply to Philippe Mathieu-Daudésprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.
Replace sprintf() by snprintf() in order to avoid:
hw/ppc/spapr.c:385:5: 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]
sprintf(mem_name, "memory@%" HWADDR_PRIx, start); ^
1 warning generated.
--- hw/ppc/spapr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
hw/ppc/spapr.c +1 -1
@@ -382,7 +382,7 @@ static int spapr_dt_memory_node(SpaprMachineState *spapr, void *fdt, int nodeid, mem_reg_property[0 ] =cpu_to_be64 (start); mem_reg_property[1 ] =cpu_to_be64 (size); -sprintf (mem_name,"memory@%" HWADDR_PRIx, start); +snprintf (mem_name,sizeof (mem_name),"memory@%" HWADDR_PRIx, start); off =fdt_add_subnode (fdt,0 , mem_name);_FDT (off);_FDT ((fdt_setprop_string (fdt, off,"device_type" ,"memory" )));
Signature · 1 line
2.41.0
Re: [PATCH v2 03/13] hw/ppc/spapr: Replace sprintf() by snprintf()
Quoted 14 lines · Philippe Mathieu-Daudé
sprintf()is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.Replace
sprintf()bysnprintf()in order to avoid:
hw/ppc/spapr.c:385:5: 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]
sprintf(mem_name, "memory@%" HWADDR_PRIx, start); ^
1 warning generated.
Quoted 4 lines
Signed-off-by Philippe Mathieu-Daudé<[email protected]>--- hw/ppc/spapr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
r~
Re: [PATCH v2 03/13] hw/ppc/spapr: Replace sprintf() by snprintf()
In reply to Philippe Mathieu-DaudéQuoted 2 lines · Philippe Mathieu-Daudé
sprintf()is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.
s/developper/developer ?
Quoted 12 lines
Replace
sprintf()bysnprintf()in order to avoid:
hw/ppc/spapr.c:385:5: 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]
sprintf(mem_name, "memory@%" HWADDR_PRIx, start); ^
1 warning generated.
Quoted 1 line
Signed-off-by Philippe Mathieu-Daudé <[email protected]>
With the typo fixed,
Quoted 17 lines
--- hw/ppc/spapr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)hw/ppc/spapr.c +1
-1@@ -382,7 +382,7 @@ static int spapr_dt_memory_node(SpaprMachineState *spapr, void *fdt, int nodeid, mem_reg_property[0 ] =cpu_to_be64 (start); mem_reg_property[1 ] =cpu_to_be64 (size); -sprintf (mem_name,"memory@%" HWADDR_PRIx, start); +snprintf (mem_name,sizeof (mem_name),"memory@%" HWADDR_PRIx, start); off =fdt_add_subnode (fdt,0 , mem_name);_FDT (off);_FDT ((fdt_setprop_string (fdt, off,"device_type" ,"memory" )));
PATCH v2 4/13 hw/mips/malta: Add re-usable rng_seed_hex_new() method
In reply to Philippe Mathieu-DaudéExtract common code from reinitialize_rng_seed() and load_kernel() to rng_seed_hex_new().
--- hw/mips/malta.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)
hw/mips/malta.c +12 -8
@@ -850,15 +850,24 @@ static void G_GNUC_PRINTF(3, 4) prom_set(uint32_t *prom_buf, int index,va_end (ap); } -static void reinitialize_rng_seed (void *opaque) +static char *rng_seed_hex_new (void ) { -char *rng_seed_hex = opaque;uint8_t rng_seed[32 ]; +char rng_seed_hex[sizeof (rng_seed) *2 +1 ];qemu_guest_getrandom_nofail (rng_seed,sizeof (rng_seed));for (size_t i =0 ; i <sizeof (rng_seed); ++i) {sprintf (rng_seed_hex + i *2 ,"%02x" , rng_seed[i]); } + +return g_strdup (rng_seed_hex); +} + +static void reinitialize_rng_seed (void *opaque) +{ + g_autofree char *rng_seed_hex = rng_seed_hex_new(); + +strcpy (opaque, rng_seed_hex); }/* Kernel */
@@ -870,8 +879,7 @@ static uint64_t load_kernel(void)uint32_t *prom_buf;long prom_size;int prom_index =0 ; -uint8_t rng_seed[32 ]; -char rng_seed_hex[sizeof (rng_seed) *2 +1 ]; + g_autofree char *rng_seed_hex = rng_seed_hex_new();size_t rng_seed_prom_offset; kernel_size = load_elf(loaderparams.kernel_filename, NULL,
@@ -946,10 +954,6 @@ static uint64_t load_kernel(void)prom_set (prom_buf, prom_index++,"modetty0" );prom_set (prom_buf, prom_index++,"38400n8r" ); -qemu_guest_getrandom_nofail (rng_seed,sizeof (rng_seed)); -for (size_t i =0 ; i <sizeof (rng_seed); ++i) { -sprintf (rng_seed_hex + i *2 ,"%02x" , rng_seed[i]); - }prom_set (prom_buf, prom_index++,"rngseed" ); rng_seed_prom_offset = prom_index *ENVP_ENTRY_SIZE +sizeof (uint32_t ) *ENVP_NB_ENTRIES ;
Signature · 1 line
2.41.0
Re: [PATCH v2 04/13] hw/mips/malta: Add re-usable rng_seed_hex_new() method
Quoted 27 lines · Philippe Mathieu-Daudé
Extract common code from
reinitialize_rng_seed()andload_kernel()torng_seed_hex_new().Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/mips/malta.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-)hw/mips/malta.c +2
-2@@ -850,15 +850,24 @@ static void G_GNUC_PRINTF(3, 4) prom_set(uint32_t *prom_buf, int index,va_end (ap); } -static void reinitialize_rng_seed (void *opaque) +static char *rng_seed_hex_new (void ) { -char *rng_seed_hex = opaque;uint8_t rng_seed[32 ]; +char rng_seed_hex[sizeof (rng_seed) *2 +1 ];qemu_guest_getrandom_nofail (rng_seed,sizeof (rng_seed));for (size_t i =0 ; i <sizeof (rng_seed); ++i) {sprintf (rng_seed_hex + i *2 ,"%02x" , rng_seed[i]); }
hw/mips/malta.c static void G_GNUC_PRINTF(3, 4) prom_set(uint32_t *prom_buf, int index, · line 871
Quoted 10 lines
hw/mips/malta.c +9
-0+ +return g_strdup (rng_seed_hex); +} + +static void reinitialize_rng_seed (void *opaque) +{ + g_autofree char *rng_seed_hex = rng_seed_hex_new(); + +strcpy (opaque, rng_seed_hex); }
Though it isn't deprecated, strcpy isn't really any safer than sprintf. We don't need to be copying text around quite as much as this.
How about:
#define RNG_SEED_SIZE 32
static void rng_seed_hex_new(char buf[2 * RNG_SEED_SIZE + 1])
{
static const char hex = "0123456789abcdef";
uint8_t rng_seed[RNG_SEED_SIZE];
qemu_guest_getrandom_nofail(rng_seed, sizeof(rng_seed));
for (int i = 0; i < RNG_SEED_SIZE; ++i) {
buf[i * 2 + 0] = hex[rng_seed[i] / 16];
buf[i * 2 + 1] = hex[rng_seed[i] % 16];
}
buf[RNG_SEED_SIZE * 2] = '\0';
}static void reinitialize_rng_seed(void *opaque)
{
rng_seed_hex_new(opaque);
}with little change in load_kernel.
r~
Re: [PATCH v2 04/13] hw/mips/malta: Add re-usable rng_seed_hex_new() method
Quoted 50 lines · Richard Henderson
Quoted 26 lines · Philippe Mathieu-Daudé
Extract common code from
reinitialize_rng_seed()andload_kernel()torng_seed_hex_new().Signed-off-by Philippe Mathieu-Daudé <[email protected]>---
hw/mips/malta.c| 20 ++++++++++++--------1 file changed, 12 insertions(+), 8 deletions(-)hw/mips/malta.c +2
-2@@ -850,15 +850,24 @@ static void G_GNUC_PRINTF(3, 4) prom_set(uint32_t *prom_buf, int index, va_end(ap); } -static void reinitialize_rng_seed (void *opaque) +static char *rng_seed_hex_new (void ) { - char *rng_seed_hex = opaque; uint8_t rng_seed[32]; + char rng_seed_hex[sizeof(rng_seed) * 2 + 1]; qemu_guest_getrandom_nofail(rng_seed, sizeof(rng_seed)); for (size_t i = 0; i < sizeof(rng_seed); ++i) { sprintf(rng_seed_hex + i * 2, "%02x", rng_seed[i]); }hw/mips/malta.c static void G_GNUC_PRINTF(3, 4) prom_set(uint32_t *prom_buf, int · line 870
Quoted 10 lines
hw/mips/malta.c +9
-0+ + return g_strdup(rng_seed_hex); +} + +static void reinitialize_rng_seed (void *opaque) +{ + g_autofree char *rng_seed_hex = rng_seed_hex_new(); + + strcpy(opaque, rng_seed_hex); }Though it isn't deprecated, strcpy isn't really any safer than sprintf. We don't need to be copying text around quite as much as this.
How about:
#define
RNG_SEED_SIZE32static void
rng_seed_hex_new(char buf[2 * RNG_SEED_SIZE + 1]){
static const char hex = "0123456789abcdef";uint8_trng_seed[RNG_SEED_SIZE];
Quoted 4 lines
qemu_guest_getrandom_nofail(rng_seed, sizeof(rng_seed)); for (int i = 0; i <RNG_SEED_SIZE; ++i) { buf[i * 2 + 0] = hex[rng_seed[i] / 16]; buf[i * 2 + 1] = hex[rng_seed[i] % 16];
Hmm. Maybe a
static inline char hexdump_nibble(unsigned val)
{
return (val < 10 ? '0' : 'a') + val;
}static inline void hexdump_byte(char *out, uint8_t byte)
{
out[0] = hexdump_nibble(byte >> 4);
out[1] = hexdump_nibble(byte & 15);
}in "qemu/cutils.h", for use elsewhere including util/hexdump.c.
r~
PATCH v2 5/13 hw/mips/malta: Replace sprintf() by snprintf()
In reply to Philippe Mathieu-Daudésprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.
Replace sprintf() by snprintf() in order to avoid:
[120/169] Compiling C object libcommon.fa.p/system_qtest.c.o hw/mips/malta.c:860:9: warning: 'sprintf' is deprecated:
sprintf(rng_seed_hex + i * 2, "%02x", rng_seed[i]); ^
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]
--- hw/mips/malta.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/mips/malta.c +2 -1
@@ -857,7 +857,8 @@ static char *rng_seed_hex_new(void)qemu_guest_getrandom_nofail (rng_seed,sizeof (rng_seed));for (size_t i =0 ; i <sizeof (rng_seed); ++i) { -sprintf (rng_seed_hex + i *2 ,"%02x" , rng_seed[i]); +snprintf (rng_seed_hex + i *2 ,sizeof (rng_seed_hex) - i *2 , +"%02x" , rng_seed[i]); }return g_strdup (rng_seed_hex);
Signature · 1 line
2.41.0
PATCH v2 6/13 system/qtest: Replace sprintf() by g_string_append_printf()
In reply to Philippe Mathieu-Daudésprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.
Replace sprintf() by GString API uses in order to avoid:
[120/169] Compiling C object libcommon.fa.p/system_qtest.c.o system/qtest.c:623:13: 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]
sprintf(&enc[i * 2], "%02x", data[i]);
^
1 warning generated.
--- system/qtest.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
system/qtest.c +3 -5
@@ -601,9 +601,9 @@ static void qtest_process_command(CharBackend *chr, gchar **words)qtest_send_prefix (chr);qtest_sendf (chr,"OK 0x%016" PRIx64"\n" , value); }else if (strcmp (words[0 ],"read" ) ==0 ) { +g_autoptr (GString) enc =g_string_new ("" );uint64_t addr, len, i;uint8_t *data; -char *enc;int ret;g_assert (words[1 ] && words[2 ]);
@@ -618,16 +618,14 @@ static void qtest_process_command(CharBackend *chr, gchar **words)address_space_read (first_cpu->as, addr,MEMTXATTRS_UNSPECIFIED , data, len); - enc =g_malloc (2 * len +1 );for (i =0 ; i < len; i++) { -sprintf (&enc[i *2 ],"%02x" , data[i]); +g_string_append_printf (enc,"%02x" , data[i]); }qtest_send_prefix (chr); -qtest_sendf (chr,"OK 0x%s\n" , enc); +qtest_sendf (chr,"OK 0x%s\n" , enc->str);g_free (data); -g_free (enc); }else if (strcmp (words[0 ],"b64read" ) ==0 ) {uint64_t addr, len;uint8_t *data;
Signature · 1 line
2.41.0
Re: [PATCH v2 06/13] system/qtest: Replace sprintf() by g_string_append_printf()
Quoted 15 lines · Philippe Mathieu-Daudé
sprintf()is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.Replace
sprintf()by GString API uses in order to avoid:[120/169] Compiling C object
libcommon.fa.p/system_qtest.c.osystem/qtest.c:623:13: 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]
sprintf(&enc[i * 2], "%02x", data[i]);
^
1 warning generated.
Quoted 5 lines
Signed-off-by Philippe Mathieu-Daudé<[email protected]>Reviewed-by Thomas Huth<[email protected]>--- system/qtest.c | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-)
r~
PATCH v2 7/13 util/hexdump: Rename @offset argument in qemu_hexdump_line()
In reply to Philippe Mathieu-Daudé@offset argument is more descriptive than @b.
Inverse @bufptr <-> @offset arguments order.
Document qemu_hexdump_line().
--- include/qemu/cutils.h | 11 +++++++++-- hw/virtio/vhost-vdpa.c | 8 ++++---- util/hexdump.c | 16 ++++++++-------- 3 files changed, 21 insertions(+), 14 deletions(-)
include/qemu/cutils.h +9 -2
@@ -252,12 +252,19 @@ static inline const char *yes_no(bool b)*/ int parse_debug_env (const char *name,int max,int initial); -/* +/** +* qemu_hexdump_line: +* @line: Buffer to be filled by the hexadecimal/ASCII dump +* @bufptr: Buffer to dump +* @offset: Offset within @bufptr to start the dump +* @len: Length of the bytes do dump +* @ascii: Replace non-ASCII characters by the dot symbol +* * Hexdump a line of a byte buffer into a hexadecimal/ASCII buffer */ #define QEMU_HEXDUMP_LINE_BYTES 16/* Number of bytes to dump */ #define QEMU_HEXDUMP_LINE_LEN 75/* Number of characters in line */ -void qemu_hexdump_line (char *line,unsigned int b,const void *bufptr, +void qemu_hexdump_line (char *line,const void *bufptr,unsigned offset,unsigned int len,bool ascii);/*
hw/virtio/vhost-vdpa.c +4 -4
@@ -941,12 +941,12 @@ static int vhost_vdpa_set_config_call(struct vhost_dev *dev,static void vhost_vdpa_dump_config (struct vhost_dev *dev,const uint8_t *config,uint32_t config_len) { -int b, len; +int ofs, len;char line[QEMU_HEXDUMP_LINE_LEN ]; -for (b =0 ; b < config_len; b +=16 ) { - len = config_len - b; -qemu_hexdump_line (line, b, config, len, false); +for (ofs =0 ; ofs < config_len; ofs +=16 ) { + len = config_len - ofs; +qemu_hexdump_line (line, config, ofs, len, false);trace_vhost_vdpa_dump_config (dev, line); } }
util/hexdump.c +8 -8
@@ -16,7 +16,7 @@#include "qemu/osdep.h" #include "qemu/cutils.h" -void qemu_hexdump_line (char *line,unsigned int b,const void *bufptr, +void qemu_hexdump_line (char *line,const void *bufptr,unsigned offset,unsigned int len,bool ascii) {const char *buf = bufptr;
@@ -26,13 +26,13 @@ void qemu_hexdump_line(char *line, unsigned int b, const void *bufptr, len =QEMU_HEXDUMP_LINE_BYTES ; } - line +=snprintf (line,6 ,"%04x:" , b); + line +=snprintf (line,6 ,"%04x:" , offset);for (i =0 ; i <QEMU_HEXDUMP_LINE_BYTES ; i++) {if ((i %4 ) ==0 ) { *line++ =' ' ; }if (i < len) { - line +=sprintf (line," %02x" , (unsigned char )buf[b + i]); + line +=sprintf (line," %02x" , (unsigned char )buf[offset + i]); }else { line +=sprintf (line," " ); }
@@ -40,7 +40,7 @@ void qemu_hexdump_line(char *line, unsigned int b, const void *bufptr,if (ascii) { *line++ =' ' ;for (i =0 ; i < len; i++) { - c = buf[b + i]; + c = buf[offset + i];if (c <' ' || c >'~' ) { c ='.' ; }
@@ -53,12 +53,12 @@ void qemu_hexdump_line(char *line, unsigned int b, const void *bufptr,void qemu_hexdump (FILE *fp,const char *prefix,const void *bufptr,size_t size) { -unsigned int b, len; +unsigned int ofs, len;char line[QEMU_HEXDUMP_LINE_LEN ]; -for (b =0 ; b < size; b +=QEMU_HEXDUMP_LINE_BYTES ) { - len = size - b; -qemu_hexdump_line (line, b, bufptr, len, true); +for (ofs =0 ; ofs < size; ofs +=QEMU_HEXDUMP_LINE_BYTES ) { + len = size - ofs; +qemu_hexdump_line (line, bufptr, ofs, len, true);fprintf (fp,"%s: %s\n" , prefix, line); }
Signature · 1 line
2.41.0
Re: [PATCH v2 07/13] util/hexdump: Rename @offset argument in qemu_hexdump_line()
Quoted 4 lines · Philippe Mathieu-Daudé
@offset argument is more descriptive than @b.
Inverse @bufptr <-> @offset arguments order.
Quoted 8 lines
Document
qemu_hexdump_line().Signed-off-by Philippe Mathieu-Daudé<[email protected]>--- include/qemu/cutils.h | 11 +++++++++-- hw/virtio/vhost-vdpa.c | 8 ++++---- util/hexdump.c | 16 ++++++++-------- 3 files changed, 21 insertions(+), 14 deletions(-)
r~
PATCH v2 8/13 util/hexdump: Have qemu_hexdump_line() return heap allocated buffer
In reply to Philippe Mathieu-Daudé--- include/qemu/cutils.h | 10 +++++++--- hw/virtio/vhost-vdpa.c | 5 +++-- util/hexdump.c | 12 ++++++++---- 3 files changed, 18 insertions(+), 9 deletions(-)
include/qemu/cutils.h +7 -3
@@ -254,18 +254,22 @@ int parse_debug_env(const char *name, int max, int initial);/** * qemu_hexdump_line: -* @line: Buffer to be filled by the hexadecimal/ASCII dump * @bufptr: Buffer to dump * @offset: Offset within @bufptr to start the dump * @len: Length of the bytes do dump * @ascii: Replace non-ASCII characters by the dot symbol * * Hexdump a line of a byte buffer into a hexadecimal/ASCII buffer +* +* The caller must use g_free() to free the returned data when it is +* no longer required. +* +* Returns: Hexadecimal/ASCII dump */ #define QEMU_HEXDUMP_LINE_BYTES 16/* Number of bytes to dump */ #define QEMU_HEXDUMP_LINE_LEN 75/* Number of characters in line */ -void qemu_hexdump_line (char *line,const void *bufptr,unsigned offset, -unsigned int len,bool ascii); +char *qemu_hexdump_line (const void *bufptr,unsigned offset, +unsigned int len,bool ascii);/* * Hexdump a buffer to a file. An optional string prefix is added to every line
hw/virtio/vhost-vdpa.c +3 -2
@@ -942,12 +942,13 @@ static void vhost_vdpa_dump_config(struct vhost_dev *dev, const uint8_t *config, uint32_t config_len) {int ofs, len; -char line[QEMU_HEXDUMP_LINE_LEN ]; +char *line;for (ofs =0 ; ofs < config_len; ofs +=16 ) { len = config_len - ofs; -qemu_hexdump_line (line, config, ofs, len, false); + line =qemu_hexdump_line (config, ofs, len, false);trace_vhost_vdpa_dump_config (dev, line); +g_free (line); } }
util/hexdump.c +8 -4
@@ -16,9 +16,10 @@#include "qemu/osdep.h" #include "qemu/cutils.h" -void qemu_hexdump_line (char *line,const void *bufptr,unsigned offset, -unsigned int len,bool ascii) +char *qemu_hexdump_line (const void *bufptr,unsigned offset, +unsigned int len,bool ascii) { +char linebuf[QEMU_HEXDUMP_LINE_BYTES ], *line = linebuf;const char *buf = bufptr;int i, c;
@@ -48,18 +49,21 @@ void qemu_hexdump_line(char *line, const void *bufptr, unsigned offset, } } *line ='\0' ; + +return g_strdup (linebuf); }void qemu_hexdump (FILE *fp,const char *prefix,const void *bufptr,size_t size) {unsigned int ofs, len; -char line[QEMU_HEXDUMP_LINE_LEN ]; +char *line;for (ofs =0 ; ofs < size; ofs +=QEMU_HEXDUMP_LINE_BYTES ) { len = size - ofs; -qemu_hexdump_line (line, bufptr, ofs, len, true); + line =qemu_hexdump_line (bufptr, ofs, len, true);fprintf (fp,"%s: %s\n" , prefix, line); +g_free (line); } }
Signature · 1 line
2.41.0
Re: [PATCH v2 08/13] util/hexdump: Have qemu_hexdump_line() return heap allocated buffer
Quoted 88 lines · Philippe Mathieu-Daudé
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- include/qemu/cutils.h | 10 +++++++--- hw/virtio/vhost-vdpa.c | 5 +++-- util/hexdump.c | 12 ++++++++---- 3 files changed, 18 insertions(+), 9 deletions(-)include/qemu/cutils.h +7
-3@@ -254,18 +254,22 @@ int parse_debug_env(const char *name, int max, int initial);/** * qemu_hexdump_line: -* @line: Buffer to be filled by the hexadecimal/ASCII dump * @bufptr: Buffer to dump * @offset: Offset within @bufptr to start the dump * @len: Length of the bytes do dump * @ascii: Replace non-ASCII characters by the dot symbol * * Hexdump a line of a byte buffer into a hexadecimal/ASCII buffer +* +* The caller must use g_free() to free the returned data when it is +* no longer required. +* +* Returns: Hexadecimal/ASCII dump */ #define QEMU_HEXDUMP_LINE_BYTES 16/* Number of bytes to dump */ #define QEMU_HEXDUMP_LINE_LEN 75/* Number of characters in line */ -void qemu_hexdump_line (char *line,const void *bufptr,unsigned offset, -unsigned int len,bool ascii); +char *qemu_hexdump_line (const void *bufptr,unsigned offset, +unsigned int len,bool ascii);/* * Hexdump a buffer to a file. An optional string prefix is added to every line hw/virtio/vhost-vdpa.c +3
-2@@ -942,12 +942,13 @@ static void vhost_vdpa_dump_config(struct vhost_dev *dev, const uint8_t *config, uint32_t config_len) {int ofs, len; -char line[QEMU_HEXDUMP_LINE_LEN ]; +char *line;for (ofs =0 ; ofs < config_len; ofs +=16 ) { len = config_len - ofs; -qemu_hexdump_line (line, config, ofs, len, false); + line =qemu_hexdump_line (config, ofs, len, false);trace_vhost_vdpa_dump_config (dev, line); +g_free (line); } }util/hexdump.c +6
-3@@ -16,9 +16,10 @@#include "qemu/osdep.h" #include "qemu/cutils.h" -void qemu_hexdump_line (char *line,const void *bufptr,unsigned offset, -unsigned int len,bool ascii) +char *qemu_hexdump_line (const void *bufptr,unsigned offset, +unsigned int len,bool ascii) { +char linebuf[QEMU_HEXDUMP_LINE_BYTES ], *line = linebuf;const char *buf = bufptr;int i, c;@@ -48,18 +49,21 @@ void qemu_hexdump_line(char *line, const void *bufptr, unsigned offset, } } *line ='\0' ; + +return g_strdup (linebuf); }void qemu_hexdump (FILE *fp,const char *prefix,const void *bufptr,size_t size) {unsigned int ofs, len; -char line[QEMU_HEXDUMP_LINE_LEN ]; +char *line;
util/hexdump.c void qemu_hexdump_line(char *line, const void *bufptr, unsigned offset, · line 69
Quoted 10 lines
util/hexdump.c +2
-1for (ofs =0 ; ofs < size; ofs +=QEMU_HEXDUMP_LINE_BYTES ) { len = size - ofs; -qemu_hexdump_line (line, bufptr, ofs, len, true); + line =qemu_hexdump_line (bufptr, ofs, len, true);fprintf (fp,"%s: %s\n" , prefix, line); +g_free (line); } }
Not especially efficient, re-allocating for each line.
How about
GString *qemu_hexdump_line(GString *str, buf, offset, len, ascii)
{
if (str) {
g_string_truncate(str, 0);
} else {
str = g_string_sized_new(QEMU_HEXDUMP_LINE_LEN);
}
...
return str;
}void qemu_hexdump(FILE *fp, ...)
{
g_autoptr(GString) str = g_string_sized_new(QEMU_HEXDUMP_LINE_LEN);
for (...) {
qemu_hexdump_line(str, ...);
fprintf(fp, "%s: %s\n", prefix, str->str);
}
}So that we reuse the one allocation across the whole loop.
r~
PATCH v2 9/13 util/hexdump: Replace sprintf() by g_string_append_printf()
In reply to Philippe Mathieu-Daudé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.
--- util/hexdump.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)
util/hexdump.c +8 -9
@@ -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,
Signature · 1 line
2.41.0
Re: [PATCH v2 09/13] util/hexdump: Replace sprintf() by g_string_append_printf()
Quoted 40 lines · Philippe Mathieu-Daudé
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.outil/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é <[email protected]>--- util/hexdump.c | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-)util/hexdump.c +1
-1@@ -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 ; }
util/hexdump.c char *qemu_hexdump_line(const void *bufptr, unsigned offset, · line 36
Quoted 10 lines
util/hexdump.c +3
-3- 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]);
I find using g_string_append_printf() simpler than checking snprintf() return value, and don't expect this function to be in hot path, but if preferred I can try to not use the GString API.
Quoted 5 lines
util/hexdump.c +1
-1}else { - line +=sprintf (line," " ); +g_string_append (gs," " ); } }
Richard Henderson
Reviewed-byRe: [PATCH v2 09/13] util/hexdump: Replace sprintf() by g_string_append_printf()
Quoted 45 lines · Philippe Mathieu-Daudé
Quoted 44 lines · Philippe Mathieu-Daudé
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.outil/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é <[email protected]>---
util/hexdump.c| 17 ++++++++---------1 file changed, 8 insertions(+), 9 deletions(-)util/hexdump.c +3
-3@@ -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, ' ');
Quoted 8 lines
util/hexdump.c char *qemu_hexdump_line(const void *bufptr, unsigned offset, · line 36
Quoted 4 lines
util/hexdump.c +1
-1} if (i < len) { - line += sprintf(line, " %02x", (unsigned char)buf[offset + i]); + g_string_append_printf(gs, " %02x", (unsigned char)buf[offset + i]);I find using
g_string_append_printf()simpler than checkingsnprintf()return value, and don't expect this function to be in hot path, but if preferred I can try to not use the GString API.
GString api is pretty good.
r~
PATCH v2 10/13 hw/scsi/scsi-disk: Use qemu_hexdump_line() to avoid sprintf()
In reply to Philippe Mathieu-Daudésprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.
Use qemu_hexdump_line() to avoid sprintf() calls, silencing:
[105/169] Compiling C object libcommon.fa.p/hw_scsi_scsi-disk.c.o hw/scsi/scsi-disk.c:2659:14: 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]
p += sprintf(p, " 0x%02x", buf[i]);
^
--- hw/scsi/scsi-disk.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
hw/scsi/scsi-disk.c +2 -6
@@ -2648,16 +2648,12 @@ static const SCSIReqOps *const scsi_disk_reqops_dispatch[256] = {static void scsi_disk_new_request_dump (uint32_t lun,uint32_t tag,uint8_t *buf) { -int i;int len =scsi_cdb_length (buf); -char *line_buffer, *p; +char *line_buffer;assert (len >0 && len <=16 ); - line_buffer =g_malloc (len *5 +1 ); -for (i =0 , p = line_buffer; i < len; i++) { - p +=sprintf (p," 0x%02x" , buf[i]); - } + line_buffer =qemu_hexdump_line (buf,0 , len, false);trace_scsi_disk_new_request (lun, tag, line_buffer);g_free (line_buffer);
Signature · 1 line
2.41.0
Re: [PATCH v2 10/13] hw/scsi/scsi-disk: Use qemu_hexdump_line() to avoid sprintf()
Quoted 29 lines · Philippe Mathieu-Daudé
sprintf()is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.Use
qemu_hexdump_line()to avoidsprintf()calls, silencing:[105/169] Compiling C object libcommon.fa.p/hw_scsi_scsi-disk.c.o hw/scsi/scsi-disk.c:2659:14: 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] p += sprintf(p, " 0x%02x", buf[i]); ^Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/scsi/scsi-disk.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)hw/scsi/scsi-disk.c +0
-1@@ -2648,16 +2648,12 @@ static const SCSIReqOps *const scsi_disk_reqops_dispatch[256] = {static void scsi_disk_new_request_dump (uint32_t lun,uint32_t tag,uint8_t *buf) { -int i;int len =scsi_cdb_length (buf);
hw/scsi/scsi-disk.c static const SCSIReqOps *const scsi_disk_reqops_dispatch[256] = { · line 2656
Quoted 10 lines
hw/scsi/scsi-disk.c +2
-5-char *line_buffer, *p; +char *line_buffer;assert (len >0 && len <=16 ); - line_buffer =g_malloc (len *5 +1 ); -for (i =0 , p = line_buffer; i < len; i++) { - p +=sprintf (p," 0x%02x" , buf[i]); - } + line_buffer =qemu_hexdump_line (buf,0 , len, false);
This is adding "0000: " as an unnecessary prefix, because it's added by qemu_hexdump_line.
I think having qemu_hexdump_line as a primitive is good, but probably the offset argument should be dropped and printed by the two callers that need it (mostly qemu_hexdump).
r~
PATCH v2 11/13 hw/ide/atapi: Use qemu_hexdump_line() to avoid sprintf()
In reply to Philippe Mathieu-Daudésprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.
Use qemu_hexdump_line() to avoid sprintf() calls, silencing:
[1367/1604] Compiling C object libcommon.fa.p/backends_tpm_tpm_util.c.o backends/tpm/tpm_util.c:355:18: 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]
p += sprintf(p, "\n");
^
--- hw/ide/atapi.c | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-)
hw/ide/atapi.c +2 -6
@@ -24,6 +24,7 @@*/ #include "qemu/osdep.h" +#include "qemu/cutils.h" #include "hw/scsi/scsi.h" #include "sysemu/block-backend.h" #include "scsi/constants.h"
@@ -1309,12 +1310,7 @@ void ide_atapi_cmd(IDEState *s)trace_ide_atapi_cmd (s, s->io_buffer[0 ]);if (trace_event_get_state_backends (TRACE_IDE_ATAPI_CMD_PACKET )) { -/* Each pretty-printed byte needs two bytes and a space; */ -char *ppacket =g_malloc (ATAPI_PACKET_SIZE *3 +1 ); -int i; -for (i =0 ; i <ATAPI_PACKET_SIZE ; i++) { -sprintf (ppacket + (i *3 ),"%02x " , buf[i]); - } +char *ppacket =qemu_hexdump_line (buf,0 ,ATAPI_PACKET_SIZE , false);trace_ide_atapi_cmd_packet (s, s->lcyl | (s->hcyl <<8 ), ppacket);g_free (ppacket); }
Signature · 1 line
2.41.0
PATCH v2 12/13 hw/dma/pl330: Use qemu_hexdump_line() to avoid sprintf()
In reply to Philippe Mathieu-Daudésprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.
Use qemu_hexdump_line() to avoid sprintf() calls, silencing:
[5/8] Compiling C object libcommon.fa.p/hw_dma_pl330.c.o hw/dma/pl330.c:333:13: 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]
sprintf(tmpbuf + strlen(tmpbuf), " %02x", buf[b + i]); ^
--- hw/dma/pl330.c | 12 +++--------- 1 file changed, 3 insertions(+), 9 deletions(-)
hw/dma/pl330.c +3 -9
@@ -15,6 +15,7 @@*/ #include "qemu/osdep.h" +#include "qemu/cutils.h" #include "hw/irq.h" #include "hw/qdev-properties.h" #include "hw/sysbus.h"
@@ -317,21 +318,14 @@ typedef struct PL330InsnDesc {static void pl330_hexdump (uint8_t *buf,size_t size) { -unsigned int b, i, len; -char tmpbuf[80 ]; +unsigned int b, len;for (b =0 ; b < size; b +=16 ) { len = size - b;if (len >16 ) { len =16 ; } - tmpbuf[0 ] ='\0' ; -for (i =0 ; i < len; i++) { -if ((i %4 ) ==0 ) { -strcat (tmpbuf," " ); - } -sprintf (tmpbuf +strlen (tmpbuf)," %02x" , buf[b + i]); - } + g_autofree char *tmpbuf = qemu_hexdump_line(buf, b, len, false);trace_pl330_hexdump (b, tmpbuf); } }
Signature · 1 line
2.41.0
PATCH v2 13/13 backends/tpm: Use qemu_hexdump_line() to avoid sprintf()
In reply to Philippe Mathieu-Daudésprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience.
Use qemu_hexdump_line() to avoid sprintf() calls, silencing:
backends/tpm/tpm_util.c:357:14: 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]
p += sprintf(p, "%.2X ", buffer[i]);
^
--- backends/tpm/tpm_util.c | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-)
backends/tpm/tpm_util.c +8 -16
@@ -21,6 +21,7 @@#include "qemu/osdep.h" #include "qemu/error-report.h" +#include "qemu/cutils.h" #include "qapi/error.h" #include "qapi/visitor.h" #include "tpm_int.h"
@@ -336,27 +337,18 @@ void tpm_sized_buffer_reset(TPMSizedBuffer *tsb)void tpm_util_show_buffer (const unsigned char *buffer,size_t buffer_size,const char *string) { -size_t len, i; -char *line_buffer, *p; +size_t len; +char *line, *lineup;if (!trace_event_get_state_backends (TRACE_TPM_UTIL_SHOW_BUFFER )) {return ; } len =MIN (tpm_cmd_get_size (buffer), buffer_size); -/* -* allocate enough room for 3 chars per buffer entry plus a -* newline after every 16 chars and a final null terminator. -*/ - line_buffer =g_malloc (len *3 + (len /16 ) +1 ); + line =qemu_hexdump_line (buffer,0 , len, false); + lineup =g_ascii_strup (line,-1 ); +trace_tpm_util_show_buffer (string, len, lineup); -for (i =0 , p = line_buffer; i < len; i++) { -if (i && !(i %16 )) { - p +=sprintf (p,"\n" ); - } - p +=sprintf (p,"%.2X " , buffer[i]); - } -trace_tpm_util_show_buffer (string, len, line_buffer); - -g_free (line_buffer); +g_free (line); +g_free (lineup); }
Signature · 1 line
2.41.0