Skip to contentSkip to conversation

misc: Replace sprintf() by snprintf() due to macOS deprecation

PATCH In review Philippe Mathieu-Daudé 4 participants 25 messages Apr 11, 2024

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

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
#1 lore raw

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
index 1f16e295ab..800b4145ac 100644
@@ -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
Reviewed-by on v1 1/9
#3 lore raw

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. Use snprintf() instead.

---
  disas/m68k.c | 2 +-
  1 file changed, 1 insertion(+), 1 deletion(-)

r~

#4 lore raw

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
index 0b89b9c4fa..49a4c0fd40 100644
@@ -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) & REG_MSR_MASK) == 0 ? "pc" : "msr");
+  snprintf(tmpstr, sizeof(tmpstr), "%s%s", register_prefix,
+          (((instr & IMM_MASK) >> IMM_LOW) & REG_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
Reviewed-by on v1 2/9
#5 lore raw

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. Use snprintf() instead.

Quoted 99 lines
---
 disas/microblaze.c | 25 ++++++++++++++++---------
 1 file changed, 16 insertions(+), 9 deletions(-)
disas/microblaze.c +16 -10
index 0b89b9c4fa..49a4c0fd40 100644
@@ -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) & REG_MSR_MASK) == 0 ? "pc" : "msr");
+  snprintf(tmpstr, sizeof(tmpstr), "%s%s", register_prefix,
+          (((instr & IMM_MASK) >> IMM_LOW) & REG_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

#6 lore raw

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. Use snprintf() instead.

---
  disas/microblaze.c | 25 ++++++++++++++++---------
  1 file changed, 16 insertions(+), 9 deletions(-)
disas/microblaze.c +0 -0
index 0b89b9c4fa..49a4c0fd40 100644

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~

#7 lore raw

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
index e236c8b5b7..fec09e9922 100644
@@ -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
#9 lore raw

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. Use snprintf() instead.

---
  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~

#10 lore raw

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
index 5b62aa0a2b..b0f04af4b6 100644
@@ -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
#11 lore raw

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. Use snprintf() instead.

---
 linux-user/flatload.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
linux-user/flatload.c +0 -0
index 5b62aa0a2b..b0f04af4b6 100644

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

#12 lore raw

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
index d888966014..faa726a86a 100644
@@ -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
index a9059bb1f7..125d4fceeb 100644
@@ -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
index 56489d8b57..b1def7f05b 100644
@@ -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
index 0c6003559f..3766bdf561 100644
@@ -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
index bbc0be9921..0ac49ea34b 100644
@@ -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
index b3725ff6e7..d19f0450d4 100644
@@ -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
index cee84af7ba..8c91d20d44 100644
@@ -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
index d8a7583ff3..12d897d306 100644
@@ -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
Reviewed-by on v1 5/9
#13 lore raw

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. Use snprintf() instead.

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

#14 lore raw

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
index 7ea8eb6ba5..03ac7a7ae9 100644
@@ -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
#15 lore raw

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. Use snprintf() instead.

("developer")

hw/net/rocker/rocker.c static const char *rocker_reg_name(void *opaque, hwaddr addr) · line 930

Quoted 9 lines
---
 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

#16 lore raw

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. Use snprintf() instead.

("developer")

Quoted 4 lines
---
  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);
                                                                ^
#17 lore raw

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
index d171e74f7b..b3fede1207 100644
@@ -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
#18 lore raw

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. Use snprintf() instead.

---
  hw/riscv/virt.c | 5 +++--
  1 file changed, 3 insertions(+), 2 deletions(-)
hw/riscv/virt.c +0 -0
index d171e74f7b..b3fede1207 100644

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~

#19 lore raw

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
index 985b1efe16..f0f4fe6714 100644
@@ -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
Reviewed-by on v1 8/9
#20 lore raw

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. Use snprintf() instead.

---
 target/arm/cpu64.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/arm/cpu64.c +1 -1
index 985b1efe16..f0f4fe6714 100644
@@ -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

Reviewed-by on v1 8/9
#21 lore raw

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. Use snprintf() instead.

---
  target/arm/cpu64.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)

r~

#22 lore raw

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
index e68cbe9293..a46d1426bf 100644
@@ -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
#23 lore raw

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. Use snprintf() instead.

---
 target/i386/kvm/kvm.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)
target/i386/kvm/kvm.c +0 -0
index e68cbe9293..a46d1426bf 100644

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

#24 lore raw

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. Use snprintf() instead.

---
  target/i386/kvm/kvm.c | 3 ++-
  1 file changed, 2 insertions(+), 1 deletion(-)
target/i386/kvm/kvm.c +0 -0
index e68cbe9293..a46d1426bf 100644

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~