Skip to contentSkip to conversation

hw/misc: Avoid sprintf() in the imx devices

PATCH In review Peter Maydell 1 participant 2 messages Apr 11, 2024 Arm i.MX and Layerscape

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 hw/misc: Avoid sprintf() in the imx devices

Various imx devices in hw/misc have a pattern in the debug logging where a function which prints register names has a fallback case for an unknown offset that uses sprintf(). Use snprintf() instead to avoid warnings on macos. (The buffer could never overflow because it is 20 bytes and we will write at most 10 digits plus 4 nondigits plus NUL.)

Commit created with:

sed -i -e 's/sprintf(unknown/snprintf(unknown, sizeof(unknown)/' hw/misc/imx*

---
Philippe: here's an easy sprintf->snprintf conversion for your pile :-)

 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 +-
 6 files changed, 8 insertions(+), 8 deletions(-)
hw/misc/imx25_ccm.c +1 -1
index d888966014d..faa726a86ac 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 a9059bb1f72..125d4fceebb 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 56489d8b57b..b1def7f05b9 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 0c6003559f5..3766bdf5619 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 bbc0be99211..0ac49ea34b3 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 b3725ff6e72..d19f0450d4a 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;
     }
 }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 8 lines · Peter Maydell

Various imx devices in hw/misc have a pattern in the debug logging where a function which prints register names has a fallback case for an unknown offset that uses sprintf(). Use snprintf() instead to avoid warnings on macos. (The buffer could never overflow because it is 20 bytes and we will write at most 10 digits plus 4 nondigits plus NUL.)

Quoted 7 lines

Commit created with:

sed -i -e 's/sprintf(unknown/snprintf(unknown, sizeof(unknown)/' hw/misc/imx*

---
Philippe: here's an easy sprintf->snprintf conversion for your pile :-)

Never mind, I hadn't noticed you'd sent a second patchset on the sprintf issue, and you've already covered this one.

-- PMM