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

Peter Maydell posted 1 patch 1 month, 2 weeks ago
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(-)
[PATCH] hw/misc: Avoid sprintf() in the imx devices
Posted by Peter Maydell 1 month, 2 weeks ago
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*

Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
---
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(-)

diff --git a/hw/misc/imx25_ccm.c b/hw/misc/imx25_ccm.c
index d888966014d..faa726a86ac 100644
--- a/hw/misc/imx25_ccm.c
+++ b/hw/misc/imx25_ccm.c
@@ -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;
     }
 }
diff --git a/hw/misc/imx31_ccm.c b/hw/misc/imx31_ccm.c
index a9059bb1f72..125d4fceebb 100644
--- a/hw/misc/imx31_ccm.c
+++ b/hw/misc/imx31_ccm.c
@@ -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;
     }
 }
diff --git a/hw/misc/imx6_ccm.c b/hw/misc/imx6_ccm.c
index 56489d8b57b..b1def7f05b9 100644
--- a/hw/misc/imx6_ccm.c
+++ b/hw/misc/imx6_ccm.c
@@ -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;
     }
 }
diff --git a/hw/misc/imx6_src.c b/hw/misc/imx6_src.c
index 0c6003559f5..3766bdf5619 100644
--- a/hw/misc/imx6_src.c
+++ b/hw/misc/imx6_src.c
@@ -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;
     }
 }
diff --git a/hw/misc/imx6ul_ccm.c b/hw/misc/imx6ul_ccm.c
index bbc0be99211..0ac49ea34b3 100644
--- a/hw/misc/imx6ul_ccm.c
+++ b/hw/misc/imx6ul_ccm.c
@@ -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;
     }
 }
diff --git a/hw/misc/imx7_src.c b/hw/misc/imx7_src.c
index b3725ff6e72..d19f0450d4a 100644
--- a/hw/misc/imx7_src.c
+++ b/hw/misc/imx7_src.c
@@ -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;
     }
 }
-- 
2.34.1
Re: [PATCH] hw/misc: Avoid sprintf() in the imx devices
Posted by Peter Maydell 1 month, 2 weeks ago
On Thu, 11 Apr 2024 at 11:45, Peter Maydell <peter.maydell@linaro.org> wrote:
>
> 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*
>
> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
> ---
> 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