misc: Replace sprintf() by snprintf() due to macOS deprecation
Subsystems (3)
cover misc: Replace sprintf() by snprintf() due to macOS deprecation
Continuation of:
https://lore.kernel.org/qemu-devel/[email protected]/
*** BLURB HERE ***
Philippe Mathieu-Daudé (9):
disas/m68k: Replace sprintf() by snprintf() disas/microblaze: Replace sprintf() by snprintf() disas/riscv: Replace sprintf() by snprintf() linux-user/flatload: Replace sprintf() by snprintf() hw/misc/imx: Replace sprintf() by snprintf() hw/net/rocker: Replace sprintf() by snprintf() hw/riscv/virt: Replace sprintf() by snprintf() target/arm: Replace sprintf() by snprintf() target/i386: Replace sprintf() by snprintf()
disas/m68k.c | 2 +- disas/microblaze.c | 25 ++++++++++++++++--------- disas/riscv.c | 10 +++++----- hw/misc/imx25_ccm.c | 2 +- hw/misc/imx31_ccm.c | 2 +- hw/misc/imx6_ccm.c | 4 ++-- hw/misc/imx6_src.c | 2 +- hw/misc/imx6ul_ccm.c | 4 ++-- hw/misc/imx7_src.c | 2 +- hw/net/imx_fec.c | 2 +- hw/net/rocker/rocker.c | 24 ++++++++++++------------ hw/riscv/virt.c | 5 +++-- hw/ssi/imx_spi.c | 2 +- linux-user/flatload.c | 2 +- target/arm/cpu64.c | 4 ++-- target/i386/kvm/kvm.c | 3 ++- 16 files changed, 52 insertions(+), 43 deletions(-)
Signature · 1 line
2.41.0
PATCH 1/9 disas/m68k: Replace sprintf() by snprintf()
sprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience. Use snprintf() instead.
--- disas/m68k.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
disas/m68k.c +1 -1
@@ -1000,7 +1000,7 @@ print_indexed (int basereg,/* Generate the text for the index register. Where this will be output is not yet determined. */ -sprintf (buf,"%s:%c%s" , +snprintf (buf,sizeof (buf),"%s:%c%s" , reg_names[(word >>12 ) &0xf ], (word &0x800 ) ?'l' :'w' , scales[(word >>9 ) &3 ]);
Signature · 1 line
2.41.0
Re: [PATCH 1/9] disas/m68k: Replace sprintf() by snprintf()
Quoted 9 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]>--- disas/m68k.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
thanks
-- PMM
Re: [PATCH 1/9] disas/m68k: Replace sprintf() by snprintf()
In reply to Philippe Mathieu-Daudé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]>--- disas/m68k.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
r~
PATCH 2/9 disas/microblaze: 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.
--- disas/microblaze.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)
disas/microblaze.c +16 -9
@@ -600,7 +600,8 @@ static char * get_field (long instr, long mask, unsigned short low) {char tmpstr[25 ]; -sprintf (tmpstr,"%s%d" , register_prefix, (int )((instr & mask) >> low)); +snprintf (tmpstr,sizeof (tmpstr),"%s%d" , register_prefix, + (int )((instr & mask) >> low));return (strdup (tmpstr)); }
@@ -608,7 +609,8 @@ static char * get_field_imm (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%d" , (short )((instr &IMM_MASK ) >>IMM_LOW )); +snprintf (tmpstr,sizeof (tmpstr),"%d" , + (short )((instr &IMM_MASK ) >>IMM_LOW ));return (strdup (tmpstr)); }
@@ -616,7 +618,8 @@ static char * get_field_imm5 (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%d" , (short )((instr &IMM5_MASK ) >>IMM_LOW )); +snprintf (tmpstr,sizeof (tmpstr),"%d" , + (short )((instr &IMM5_MASK ) >>IMM_LOW ));return (strdup (tmpstr)); }
@@ -624,7 +627,8 @@ static char * get_field_rfsl (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%s%d" , fsl_register_prefix, (short )((instr &RFSL_MASK ) >>IMM_LOW )); +snprintf (tmpstr,sizeof (tmpstr),"%s%d" , fsl_register_prefix, + (short )((instr &RFSL_MASK ) >>IMM_LOW ));return (strdup (tmpstr)); }
@@ -632,7 +636,8 @@ static char * get_field_imm15 (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%d" , (short )((instr &IMM15_MASK ) >>IMM_LOW )); +snprintf (tmpstr,sizeof (tmpstr),"%d" , + (short )((instr &IMM15_MASK ) >>IMM_LOW ));return (strdup (tmpstr)); }
@@ -641,7 +646,8 @@ static char * get_field_unsigned_imm (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%d" , (int )((instr &IMM_MASK ) >>IMM_LOW )); +snprintf (tmpstr,sizeof (tmpstr),"%d" , + (int )((instr &IMM_MASK ) >>IMM_LOW ));return (strdup (tmpstr)); }#endif
@@ -653,7 +659,8 @@ get_field_unsigned_imm (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%s%s" , register_prefix, (((instr &IMM_MASK ) >>IMM_LOW ) ®_MSR_MASK ) ==0 ?"pc" :"msr" ); +snprintf (tmpstr,sizeof (tmpstr),"%s%s" , register_prefix, + (((instr &IMM_MASK ) >>IMM_LOW ) ®_MSR_MASK ) ==0 ?"pc" :"msr" );return (strdup (tmpstr)); }
@@ -709,7 +716,7 @@ get_field_special(long instr, const struct op_code_struct *op)default : {if ( ((((instr &IMM_MASK ) >>IMM_LOW ) ^ op->immval_mask) &0xE000 ) ==REG_PVR_MASK ) { -sprintf (tmpstr,"%s%u" , pvr_register_prefix, +snprintf (tmpstr,sizeof (tmpstr),"%s%u" , pvr_register_prefix, (unsigned short )(((instr &IMM_MASK ) >>IMM_LOW ) ^ op->immval_mask) ^REG_PVR_MASK );return (strdup (tmpstr));
@@ -720,7 +727,7 @@ get_field_special(long instr, const struct op_code_struct *op)break ; } -sprintf (tmpstr,"%s%s" , register_prefix, spr); +snprintf (tmpstr,sizeof (tmpstr),"%s%s" , register_prefix, spr);return (strdup (tmpstr)); }
Signature · 1 line
2.41.0
Re: [PATCH 2/9] disas/microblaze: Replace sprintf() by snprintf()
Quoted 5 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]>
Quoted 99 lines
--- disas/microblaze.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)disas/microblaze.c +16
-10@@ -600,7 +600,8 @@ static char * get_field (long instr, long mask, unsigned short low) {char tmpstr[25 ]; -sprintf (tmpstr,"%s%d" , register_prefix, (int )((instr & mask) >> low)); +snprintf (tmpstr,sizeof (tmpstr),"%s%d" , register_prefix, + (int )((instr & mask) >> low));return (strdup (tmpstr)); }@@ -608,7 +609,8 @@ static char * get_field_imm (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%d" , (short )((instr &IMM_MASK ) >>IMM_LOW )); +snprintf (tmpstr,sizeof (tmpstr),"%d" , + (short )((instr &IMM_MASK ) >>IMM_LOW ));return (strdup (tmpstr)); }@@ -616,7 +618,8 @@ static char * get_field_imm5 (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%d" , (short )((instr &IMM5_MASK ) >>IMM_LOW )); +snprintf (tmpstr,sizeof (tmpstr),"%d" , + (short )((instr &IMM5_MASK ) >>IMM_LOW ));return (strdup (tmpstr)); }@@ -624,7 +627,8 @@ static char * get_field_rfsl (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%s%d" , fsl_register_prefix, (short )((instr &RFSL_MASK ) >>IMM_LOW )); +snprintf (tmpstr,sizeof (tmpstr),"%s%d" , fsl_register_prefix, + (short )((instr &RFSL_MASK ) >>IMM_LOW ));return (strdup (tmpstr)); }@@ -632,7 +636,8 @@ static char * get_field_imm15 (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%d" , (short )((instr &IMM15_MASK ) >>IMM_LOW )); +snprintf (tmpstr,sizeof (tmpstr),"%d" , + (short )((instr &IMM15_MASK ) >>IMM_LOW ));return (strdup (tmpstr)); }@@ -641,7 +646,8 @@ static char * get_field_unsigned_imm (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%d" , (int )((instr &IMM_MASK ) >>IMM_LOW )); +snprintf (tmpstr,sizeof (tmpstr),"%d" , + (int )((instr &IMM_MASK ) >>IMM_LOW ));return (strdup (tmpstr)); }#endif @@ -653,7 +659,8 @@ get_field_unsigned_imm (long instr) {char tmpstr[25 ]; -sprintf (tmpstr,"%s%s" , register_prefix, (((instr &IMM_MASK ) >>IMM_LOW ) ®_MSR_MASK ) ==0 ?"pc" :"msr" ); +snprintf (tmpstr,sizeof (tmpstr),"%s%s" , register_prefix, + (((instr &IMM_MASK ) >>IMM_LOW ) ®_MSR_MASK ) ==0 ?"pc" :"msr" );return (strdup (tmpstr)); }@@ -709,7 +716,7 @@ get_field_special(long instr, const struct op_code_struct *op)default : {if ( ((((instr &IMM_MASK ) >>IMM_LOW ) ^ op->immval_mask) &0xE000 ) ==REG_PVR_MASK ) { -sprintf (tmpstr,"%s%u" , pvr_register_prefix, +snprintf (tmpstr,sizeof (tmpstr),"%s%u" , pvr_register_prefix, (unsigned short )(((instr &IMM_MASK ) >>IMM_LOW ) ^ op->immval_mask) ^REG_PVR_MASK );return (strdup (tmpstr));@@ -720,7 +727,7 @@ get_field_special(long instr, const struct op_code_struct *op)break ; } -sprintf (tmpstr,"%s%s" , register_prefix, spr); +snprintf (tmpstr,sizeof (tmpstr),"%s%s" , register_prefix, spr);return (strdup (tmpstr)); } --2.41.0
Re: [PATCH 2/9] disas/microblaze: Replace sprintf() by snprintf()
In reply to Philippe Mathieu-DaudéQuoted 13 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]>--- disas/microblaze.c | 25 ++++++++++++++++--------- 1 file changed, 16 insertions(+), 9 deletions(-)disas/microblaze.c +0
-0
disas/microblaze.c static char * · line 605
Quoted 7 lines
disas/microblaze.c +2
-1@@ -600,7 +600,8 @@ static char * get_field (long instr, long mask, unsigned short low) {char tmpstr[25 ]; -sprintf (tmpstr,"%s%d" , register_prefix, (int )((instr & mask) >> low)); +snprintf (tmpstr,sizeof (tmpstr),"%s%d" , register_prefix, + (int )((instr & mask) >> low));return (strdup (tmpstr));
Any s*printf followed by allocation should use g_strdup_printf to do it in one step.
r~
PATCH 3/9 disas/riscv: 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.
--- disas/riscv.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
disas/riscv.c +5 -5
@@ -4966,7 +4966,7 @@ static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec)case 'v' : {char nbuf[32 ] = {0 };const int sew =1 << (((dec->vzimm >>3 ) &0b111 ) +3 ); -sprintf (nbuf,"%d" , sew); +snprintf (nbuf,sizeof (nbuf),"%d" , sew);const int lmul = dec->vzimm &0b11 ;const int flmul = (dec->vzimm >>2 ) &1 ;const char *vta = (dec->vzimm >>6 ) &1 ?"ta" :"tu" ;
@@ -4977,18 +4977,18 @@ static void format_inst(char *buf, size_t buflen, size_t tab, rv_decode *dec)if (flmul) {switch (lmul) {case 3 : -sprintf (nbuf,"f2" ); +snprintf (nbuf,sizeof (nbuf),"f2" );break ;case 2 : -sprintf (nbuf,"f4" ); +snprintf (nbuf,sizeof (nbuf),"f4" );break ;case 1 : -sprintf (nbuf,"f8" ); +snprintf (nbuf,sizeof (nbuf),"f8" );break ; }append (buf, nbuf, buflen); }else { -sprintf (nbuf,"%d" ,1 << lmul); +snprintf (nbuf,sizeof (nbuf),"%d" ,1 << lmul);append (buf, nbuf, buflen); }append (buf,"," , buflen);
Signature · 1 line
2.41.0
Re: [PATCH 3/9] disas/riscv: Replace sprintf() by snprintf()
Quoted 6 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]>---
thanks
-- PMM
Re: [PATCH 3/9] disas/riscv: Replace sprintf() by snprintf()
In reply to Philippe Mathieu-Daudé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]>--- disas/riscv.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
Ug. Repeated strlen+strncat?
All of format_inst should be rewritten, probably using GString.
r~
PATCH 4/9 linux-user/flatload: 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.
--- linux-user/flatload.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/flatload.c +1 -1
@@ -692,7 +692,7 @@ static int load_flat_shared_library(int id, struct lib_info *libs)char buf[16 ];/* Create the file name */ -sprintf (buf,"/lib/lib%d.so" , id); + snprintf(buf, sizeofbuf), "/lib/lib%d.so", id);/* Open the file up */ bprm.filename = buf;
Signature · 1 line
2.41.0
Re: [PATCH 4/9] linux-user/flatload: 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. Usesnprintf()instead.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- linux-user/flatload.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)linux-user/flatload.c +0
-0
linux-user/flatload.c static int load_flat_shared_library(int id, struct lib_info *libs) · line 695
Quoted 5 lines
linux-user/flatload.c +1
-1@@ -692,7 +692,7 @@ static int load_flat_shared_library(int id, struct lib_info *libs)char buf[16 ];/* Create the file name */ -sprintf (buf,"/lib/lib%d.so" , id); + snprintf(buf, sizeofbuf), "/lib/lib%d.so", id);
Indentation has gone wrong, and another missing '('.
Looking at that, I notice that this function is actually dead code -- it's in code that is only compiled and called if CONFIG_BINFMT_SHARED_FLAT is defined, and if you try to define that you will hit the "#error needs checking" in the calc_reloc() function. It seems to have been like this since the flatload support was added in 2006.
So I think we should remove the dead code -- I'll send a patch.
thanks
-- PMM
PATCH 5/9 hw/misc/imx: 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/misc/imx25_ccm.c | 2 +- hw/misc/imx31_ccm.c | 2 +- hw/misc/imx6_ccm.c | 4 ++-- hw/misc/imx6_src.c | 2 +- hw/misc/imx6ul_ccm.c | 4 ++-- hw/misc/imx7_src.c | 2 +- hw/net/imx_fec.c | 2 +- hw/ssi/imx_spi.c | 2 +- 8 files changed, 10 insertions(+), 10 deletions(-)
hw/misc/imx25_ccm.c +1 -1
@@ -91,7 +91,7 @@ static const char *imx25_ccm_reg_name(uint32_t reg)case IMX25_CCM_LPIMR1_REG :return "lpimr1" ;default : -sprintf (unknown,"[%u ?]" , reg); +snprintf (unknown,sizeof (unknown),"[%u ?]" , reg);return unknown; } }
hw/misc/imx31_ccm.c +1 -1
@@ -89,7 +89,7 @@ static const char *imx31_ccm_reg_name(uint32_t reg)case IMX31_CCM_PDR2_REG :return "PDR2" ;default : -sprintf (unknown,"[%u ?]" , reg); +snprintf (unknown,sizeof (unknown),"[%u ?]" , reg);return unknown; } }
hw/misc/imx6_ccm.c +2 -2
@@ -85,7 +85,7 @@ static const char *imx6_ccm_reg_name(uint32_t reg)case CCM_CMEOR :return "CMEOR" ;default : -sprintf (unknown,"%u ?" , reg); +snprintf (unknown,sizeof (unknown),"%u ?" , reg);return unknown; } }
@@ -224,7 +224,7 @@ static const char *imx6_analog_reg_name(uint32_t reg)case USB_ANALOG_DIGPROG :return "USB_ANALOG_DIGPROG" ;default : -sprintf (unknown,"%u ?" , reg); +snprintf (unknown,sizeof (unknown),"%u ?" , reg);return unknown; } }
hw/misc/imx6_src.c +1 -1
@@ -68,7 +68,7 @@ static const char *imx6_src_reg_name(uint32_t reg)case SRC_GPR10 :return "SRC_GPR10" ;default : -sprintf (unknown,"%u ?" , reg); +snprintf (unknown,sizeof (unknown),"%u ?" , reg);return unknown; } }
hw/misc/imx6ul_ccm.c +2 -2
@@ -143,7 +143,7 @@ static const char *imx6ul_ccm_reg_name(uint32_t reg)case CCM_CMEOR :return "CMEOR" ;default : -sprintf (unknown,"%u ?" , reg); +snprintf (unknown,sizeof (unknown),"%u ?" , reg);return unknown; } }
@@ -274,7 +274,7 @@ static const char *imx6ul_analog_reg_name(uint32_t reg)case USB_ANALOG_DIGPROG :return "USB_ANALOG_DIGPROG" ;default : -sprintf (unknown,"%u ?" , reg); +snprintf (unknown,sizeof (unknown),"%u ?" , reg);return unknown; } }
hw/misc/imx7_src.c +1 -1
@@ -75,7 +75,7 @@ static const char *imx7_src_reg_name(uint32_t reg)case SRC_GPR10 :return "SRC_GPR10" ;default : -sprintf (unknown,"%u ?" , reg); +snprintf (unknown,sizeof (unknown),"%u ?" , reg);return unknown; } }
hw/net/imx_fec.c +1 -1
@@ -41,7 +41,7 @@static const char *imx_default_reg_name (IMXFECState *s,uint32_t index) {static char tmp[20 ]; -sprintf (tmp,"index %d" , index); +snprintf (tmp,sizeof (tmp),"index %d" , index);return tmp; }
hw/ssi/imx_spi.c +1 -1
@@ -53,7 +53,7 @@ static const char *imx_spi_reg_name(uint32_t reg)case ECSPI_MSGDATA :return "ECSPI_MSGDATA" ;default : -sprintf (unknown,"%u ?" , reg); +snprintf (unknown,sizeof (unknown),"%u ?" , reg);return unknown; } }
Signature · 1 line
2.41.0
Re: [PATCH 5/9] hw/misc/imx: Replace sprintf() by snprintf()
Quoted 5 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]>
Quoted 11 lines
--- hw/misc/imx25_ccm.c | 2 +- hw/misc/imx31_ccm.c | 2 +- hw/misc/imx6_ccm.c | 4 ++-- hw/misc/imx6_src.c | 2 +- hw/misc/imx6ul_ccm.c | 4 ++-- hw/misc/imx7_src.c | 2 +- hw/net/imx_fec.c | 2 +- hw/ssi/imx_spi.c | 2 +- 8 files changed, 10 insertions(+), 10 deletions(-)
thanks
-- PMM
PATCH 6/9 hw/net/rocker: 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/net/rocker/rocker.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
hw/net/rocker/rocker.c +12 -12
@@ -915,40 +915,40 @@ static const char *rocker_reg_name(void *opaque, hwaddr addr)switch (index) {case 0 : -sprintf (ring_name,"cmd" ); +snprintf (ring_name,sizeof (ring_name),"cmd" );break ;case 1 : -sprintf (ring_name,"event" ); +snprintf (ring_name,sizeof (ring_name),"event" );break ;default : -sprintf (ring_name,"%s-%d" , index %2 ?"rx" :"tx" , - (index -2 ) /2 ); +snprintf (ring_name,sizeof (ring_name),"%s-%d" , + index %2 ?"rx" :"tx" , (index -2 ) /2 ); }switch (offset) {case ROCKER_DMA_DESC_ADDR_OFFSET : -sprintf (buf,"Ring[%s] ADDR" , ring_name); + snprintf(buf, sizeofbuf), "Ring[%s] ADDR", ring_name);return buf;case ROCKER_DMA_DESC_ADDR_OFFSET +4 : -sprintf (buf,"Ring[%s] ADDR+4" , ring_name); + snprintf(buf, sizeofbuf), "Ring[%s] ADDR+4", ring_name);return buf;case ROCKER_DMA_DESC_SIZE_OFFSET : -sprintf (buf,"Ring[%s] SIZE" , ring_name); + snprintf(buf, sizeofbuf), "Ring[%s] SIZE", ring_name);return buf;case ROCKER_DMA_DESC_HEAD_OFFSET : -sprintf (buf,"Ring[%s] HEAD" , ring_name); + snprintf(buf, sizeofbuf), "Ring[%s] HEAD", ring_name);return buf;case ROCKER_DMA_DESC_TAIL_OFFSET : -sprintf (buf,"Ring[%s] TAIL" , ring_name); + snprintf(buf, sizeofbuf), "Ring[%s] TAIL", ring_name);return buf;case ROCKER_DMA_DESC_CTRL_OFFSET : -sprintf (buf,"Ring[%s] CTRL" , ring_name); + snprintf(buf, sizeofbuf), "Ring[%s] CTRL", ring_name);return buf;case ROCKER_DMA_DESC_CREDITS_OFFSET : -sprintf (buf,"Ring[%s] CREDITS" , ring_name); + snprintf(buf, sizeofbuf), "Ring[%s] CREDITS", ring_name);return buf;default : -sprintf (buf,"Ring[%s] ???" , ring_name); + snprintf(buf, sizeofbuf), "Ring[%s] ???", ring_name);return buf; } }else {
Signature · 1 line
2.41.0
Re: [PATCH 6/9] hw/net/rocker: Replace sprintf() by snprintf()
Quoted 3 lines · Philippe Mathieu-Daudé
sprintf()is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience. Usesnprintf()instead.
("developer")
hw/net/rocker/rocker.c static const char *rocker_reg_name(void *opaque, hwaddr addr) · line 930
Quoted 9 lines
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/net/rocker/rocker.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)switch (offset) { case ROCKER_DMA_DESC_ADDR_OFFSET:hw/net/rocker/rocker.c +1
-1-sprintf (buf,"Ring[%s] ADDR" , ring_name); + snprintf(buf, sizeofbuf), "Ring[%s] ADDR", ring_name);
Something seems to have gone wrong here. Shouldn't this have failed to compile ?
thanks
-- PMM
Re: [PATCH 6/9] hw/net/rocker: Replace sprintf() by snprintf()
Quoted 12 lines · Peter Maydell
Quoted 3 lines · Philippe Mathieu-Daudé
sprintf()is deprecated on Darwin since macOS 13.0 / XCode 14.1, resulting in painful developper experience. Usesnprintf()instead.("developer")
Quoted 4 lines
Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/net/rocker/rocker.c | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-)
Quoted 7 lines
hw/net/rocker/rocker.c static const char *rocker_reg_name(void *opaque, hwaddr addr) · line 930
Quoted 4 lines
switch (offset) { case ROCKER_DMA_DESC_ADDR_OFFSET:hw/net/rocker/rocker.c +1
-1-sprintf (buf,"Ring[%s] ADDR" , ring_name); + snprintf(buf, sizeofbuf), "Ring[%s] ADDR", ring_name);Something seems to have gone wrong here. Shouldn't this have failed to compile ?
This code is guarded by DEBUG_ROCKER, which is why I didn't noticed :)
Indeed when enabling:
../../hw/net/rocker/rocker.c:930:65: error: extraneous ')' before ';'
snprintf(buf, sizeofbuf), "Ring[%s] ADDR", ring_name);
^PATCH 7/9 hw/riscv/virt: 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/riscv/virt.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
hw/riscv/virt.c +3 -2
@@ -1619,7 +1619,7 @@ static char *virt_get_aia_guests(Object *obj, Error **errp)RISCVVirtState *s =RISCV_VIRT_MACHINE (obj);char val[32 ]; -sprintf (val,"%d" , s->aia_guests); +snprintf (val,sizeof (val),"%d" , s->aia_guests);return g_strdup (val); }
@@ -1785,7 +1785,8 @@ static void virt_machine_class_init(ObjectClass *oc, void *data)object_class_property_add_str (oc,"aia-guests" , virt_get_aia_guests, virt_set_aia_guests); -sprintf (str,"Set number of guest MMIO pages for AIA IMSIC. Valid value " +snprintf (str,sizeof (str), +"Set number of guest MMIO pages for AIA IMSIC. Valid value " "should be between 0 and %d." ,VIRT_IRQCHIP_MAX_GUESTS );object_class_property_set_description (oc,"aia-guests" , str); object_class_property_add(oc, "acpi", "OnOffAuto",
Signature · 1 line
2.41.0
Re: [PATCH 7/9] hw/riscv/virt: Replace sprintf() by snprintf()
Quoted 13 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/riscv/virt.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)hw/riscv/virt.c +0
-0
hw/riscv/virt.c static char *virt_get_aia_guests(Object *obj, Error **errp) · line 1624
Quoted 7 lines
hw/riscv/virt.c +1
-1@@ -1619,7 +1619,7 @@ static char *virt_get_aia_guests(Object *obj, Error **errp)RISCVVirtState *s =RISCV_VIRT_MACHINE (obj);char val[32 ]; -sprintf (val,"%d" , s->aia_guests); +snprintf (val,sizeof (val),"%d" , s->aia_guests);return g_strdup (val); }
g_strdup_printf.
hw/riscv/virt.c static void virt_machine_class_init(ObjectClass *oc, void *data) · line 1792
Quoted 10 lines
hw/riscv/virt.c +2
-1@@ -1785,7 +1785,8 @@ static void virt_machine_class_init(ObjectClass *oc, void *data)object_class_property_add_str (oc,"aia-guests" , virt_get_aia_guests, virt_set_aia_guests); -sprintf (str,"Set number of guest MMIO pages for AIA IMSIC. Valid value " +snprintf (str,sizeof (str), +"Set number of guest MMIO pages for AIA IMSIC. Valid value " "should be between 0 and %d." ,VIRT_IRQCHIP_MAX_GUESTS );object_class_property_set_description (oc,"aia-guests" , str); object_class_property_add(oc, "acpi", "OnOffAuto",
Ok.
r~
PATCH 8/9 target/arm: 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.
--- target/arm/cpu64.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/cpu64.c +2 -2
@@ -433,7 +433,7 @@ void aarch64_add_sve_properties(Object *obj)for (vq =1 ; vq <=ARM_MAX_VQ ; ++vq) {char name[8 ]; -sprintf (name,"sve%d" , vq *128 ); +snprintf (name,sizeof (name),"sve%d" , vq *128 );object_property_add (obj, name,"bool" , cpu_arm_get_vq, cpu_arm_set_vq,NULL , &cpu->sve_vq); }
@@ -458,7 +458,7 @@ void aarch64_add_sme_properties(Object *obj)for (vq =1 ; vq <=ARM_MAX_VQ ; vq <<=1 ) {char name[8 ]; -sprintf (name,"sme%d" , vq *128 ); +snprintf (name,sizeof (name),"sme%d" , vq *128 );object_property_add (obj, name,"bool" , cpu_arm_get_vq, cpu_arm_set_vq,NULL , &cpu->sme_vq); }
Signature · 1 line
2.41.0
Re: [PATCH 8/9] target/arm: Replace sprintf() by snprintf()
Quoted 23 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]>--- target/arm/cpu64.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)target/arm/cpu64.c +1
-1@@ -433,7 +433,7 @@ void aarch64_add_sve_properties(Object *obj)for (vq =1 ; vq <=ARM_MAX_VQ ; ++vq) {char name[8 ]; -sprintf (name,"sve%d" , vq *128 ); +snprintf (name,sizeof (name),"sve%d" , vq *128 );object_property_add (obj, name,"bool" , cpu_arm_get_vq, cpu_arm_set_vq,NULL , &cpu->sve_vq); }
target/arm/cpu64.c void aarch64_add_sme_properties(Object *obj) · line 464
Quoted 8 lines
target/arm/cpu64.c +1
-1@@ -458,7 +458,7 @@ void aarch64_add_sme_properties(Object *obj)for (vq =1 ; vq <=ARM_MAX_VQ ; vq <<=1 ) {char name[8 ]; -sprintf (name,"sme%d" , vq *128 ); +snprintf (name,sizeof (name),"sme%d" , vq *128 );object_property_add (obj, name,"bool" , cpu_arm_get_vq, cpu_arm_set_vq,NULL , &cpu->sme_vq); }
thanks
-- PMM
Re: [PATCH 8/9] target/arm: Replace sprintf() by snprintf()
In reply to Philippe Mathieu-Daudé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]>--- target/arm/cpu64.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
r~
PATCH 9/9 target/i386: 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.
--- target/i386/kvm/kvm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
target/i386/kvm/kvm.c +2 -1
@@ -5335,7 +5335,8 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)case KVM_EXIT_NOTIFY : ctx_invalid = !!(run->notify.flags &KVM_NOTIFY_CONTEXT_INVALID ); state =KVM_STATE (current_accel ()); -sprintf (str,"Encounter a notify exit with %svalid context in" +snprintf (str,sizeof (str), +"Encounter a notify exit with %svalid context in" " guest. There can be possible misbehaves in guest." " Please have a look." , ctx_invalid ?"in" :"" ); if (ctx_invalid ||
Signature · 1 line
2.41.0
Re: [PATCH 9/9] target/i386: 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. Usesnprintf()instead.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- target/i386/kvm/kvm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)target/i386/kvm/kvm.c +0
-0
target/i386/kvm/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)
Quoted 10 lines
target/i386/kvm/kvm.c +2
-2@@ -5335,7 +5335,8 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)case KVM_EXIT_NOTIFY : ctx_invalid = !!(run->notify.flags &KVM_NOTIFY_CONTEXT_INVALID ); state =KVM_STATE (current_accel ()); -sprintf (str,"Encounter a notify exit with %svalid context in" +snprintf (str,sizeof (str), +"Encounter a notify exit with %svalid context in" " guest. There can be possible misbehaves in guest." " Please have a look." , ctx_invalid ?"in" :"" ); if (ctx_invalid || --
This is a case where I think we would be better off with g_strdup_printf():
* the buffer declaration is a long way away from its use
* the string is long and it's not trivial to confirm that it will fit in the buffer
* it's quite plausible somebody will come along later to clean up the wording of the error message and not notice they need to enlarge the buffer
* it's only for printing a warning, so it's not going to be in a hot codepath
thanks
-- PMM
Re: [PATCH 9/9] target/i386: Replace sprintf() by snprintf()
In reply to Philippe Mathieu-DaudéQuoted 13 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]>--- target/i386/kvm/kvm.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)target/i386/kvm/kvm.c +0
-0
target/i386/kvm/kvm.c int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run) · line 5342
Quoted 9 lines
target/i386/kvm/kvm.c +2
-1@@ -5335,7 +5335,8 @@ int kvm_arch_handle_exit(CPUState *cs, struct kvm_run *run)case KVM_EXIT_NOTIFY : ctx_invalid = !!(run->notify.flags &KVM_NOTIFY_CONTEXT_INVALID ); state =KVM_STATE (current_accel ()); -sprintf (str,"Encounter a notify exit with %svalid context in" +snprintf (str,sizeof (str), +"Encounter a notify exit with %svalid context in" " guest. There can be possible misbehaves in guest." " Please have a look." , ctx_invalid ?"in" :"" ); if (ctx_invalid ||
In the larger context,
Quoted 8 lines
if (ctx_invalid ||
state->notify_vmexit == NOTIFY_VMEXIT_OPTION_INTERNAL_ERROR) {
warn_report("KVM internal error: %s", str);
ret = -1;
} else {
warn_report_once("KVM: %s", str);
ret = 0;
}so there's really no need to sprintf into a buffer at all -- just pass it all to warn_report_*.
The English text could use some improvement as well. :-)
r~