diff mbox series

[v3,06/27] hw/mips/malta: Add re-usable rng_seed_hex_new() method

Message ID 20240412073346.458116-7-richard.henderson@linaro.org
State Superseded
Headers show
Series misc: Replace sprintf | expand

Commit Message

Richard Henderson April 12, 2024, 7:33 a.m. UTC
From: Philippe Mathieu-Daudé <philmd@linaro.org>

sprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1.

Extract common code from reinitialize_rng_seed and load_kernel
to rng_seed_hex_new.  Using qemu_hexdump_line both fixes the
deprecation warning and simplifies the code base.

Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
[rth: Use qemu_hexdump_line.]
Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
---
 hw/mips/malta.c | 25 +++++++++++++------------
 1 file changed, 13 insertions(+), 12 deletions(-)

Comments

Pierrick Bouvier June 3, 2024, 6:56 p.m. UTC | #1
On 4/12/24 00:33, Richard Henderson wrote:
> From: Philippe Mathieu-Daudé <philmd@linaro.org>
> 
> sprintf() is deprecated on Darwin since macOS 13.0 / XCode 14.1.
> 
> Extract common code from reinitialize_rng_seed and load_kernel
> to rng_seed_hex_new.  Using qemu_hexdump_line both fixes the
> deprecation warning and simplifies the code base.
> 
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> [rth: Use qemu_hexdump_line.]
> Signed-off-by: Richard Henderson <richard.henderson@linaro.org>
> ---
>   hw/mips/malta.c | 25 +++++++++++++------------
>   1 file changed, 13 insertions(+), 12 deletions(-)
> 
> diff --git a/hw/mips/malta.c b/hw/mips/malta.c
> index af74008c82..664a2ae0a9 100644
> --- a/hw/mips/malta.c
> +++ b/hw/mips/malta.c
> @@ -26,6 +26,7 @@
>   #include "qemu/units.h"
>   #include "qemu/bitops.h"
>   #include "qemu/datadir.h"
> +#include "qemu/cutils.h"
>   #include "qemu/guest-random.h"
>   #include "hw/clock.h"
>   #include "hw/southbridge/piix.h"
> @@ -850,15 +851,18 @@ 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 GString *rng_seed_hex_new(void)
>   {
> -    char *rng_seed_hex = opaque;
>       uint8_t rng_seed[32];
>   
>       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 qemu_hexdump_line(NULL, rng_seed, sizeof(rng_seed), 0, 0);
> +}
> +
> +static void reinitialize_rng_seed(void *opaque)
> +{
> +    g_autoptr(GString) hex = rng_seed_hex_new();
> +    memcpy(opaque, hex->str, hex->len);
>   }
>   
>   /* Kernel */
> @@ -870,8 +874,6 @@ 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];
>       size_t rng_seed_prom_offset;
>   
>       kernel_size = load_elf(loaderparams.kernel_filename, NULL,
> @@ -946,14 +948,13 @@ 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;
> -    prom_set(prom_buf, prom_index++, "%s", rng_seed_hex);
> +    {
> +        g_autoptr(GString) hex = rng_seed_hex_new();
> +        prom_set(prom_buf, prom_index++, "%s", hex->str);
> +    }
>   
>       prom_set(prom_buf, prom_index++, NULL);
>   

Reviewed-by: Pierrick Bouvier <pierrick.bouvier@linaro.org>
diff mbox series

Patch

diff --git a/hw/mips/malta.c b/hw/mips/malta.c
index af74008c82..664a2ae0a9 100644
--- a/hw/mips/malta.c
+++ b/hw/mips/malta.c
@@ -26,6 +26,7 @@ 
 #include "qemu/units.h"
 #include "qemu/bitops.h"
 #include "qemu/datadir.h"
+#include "qemu/cutils.h"
 #include "qemu/guest-random.h"
 #include "hw/clock.h"
 #include "hw/southbridge/piix.h"
@@ -850,15 +851,18 @@  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 GString *rng_seed_hex_new(void)
 {
-    char *rng_seed_hex = opaque;
     uint8_t rng_seed[32];
 
     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 qemu_hexdump_line(NULL, rng_seed, sizeof(rng_seed), 0, 0);
+}
+
+static void reinitialize_rng_seed(void *opaque)
+{
+    g_autoptr(GString) hex = rng_seed_hex_new();
+    memcpy(opaque, hex->str, hex->len);
 }
 
 /* Kernel */
@@ -870,8 +874,6 @@  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];
     size_t rng_seed_prom_offset;
 
     kernel_size = load_elf(loaderparams.kernel_filename, NULL,
@@ -946,14 +948,13 @@  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;
-    prom_set(prom_buf, prom_index++, "%s", rng_seed_hex);
+    {
+        g_autoptr(GString) hex = rng_seed_hex_new();
+        prom_set(prom_buf, prom_index++, "%s", hex->str);
+    }
 
     prom_set(prom_buf, prom_index++, NULL);