[PATCH 1/4] hw/arm: use g_autofree for filename in arm_load_dtb

Alex Bennée posted 4 patches 3 weeks, 6 days ago
There is a newer version of this series
[PATCH 1/4] hw/arm: use g_autofree for filename in arm_load_dtb
Posted by Alex Bennée 3 weeks, 6 days ago
The function has quite a number of exit cases so lets try and clean
things up with g_autofree. As the fdt hasn't be allocated yet we can
return directly from the fail point.

Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
---
 hw/arm/boot.c | 8 +++-----
 1 file changed, 3 insertions(+), 5 deletions(-)

diff --git a/hw/arm/boot.c b/hw/arm/boot.c
index d391cd01bb1..56fd13b9f7c 100644
--- a/hw/arm/boot.c
+++ b/hw/arm/boot.c
@@ -528,20 +528,18 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
     Error *err = NULL;
 
     if (binfo->dtb_filename) {
-        char *filename;
-        filename = qemu_find_file(QEMU_FILE_TYPE_DTB, binfo->dtb_filename);
+        g_autofree char *filename = qemu_find_file(QEMU_FILE_TYPE_DTB,
+                                                   binfo->dtb_filename);
         if (!filename) {
             fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
-            goto fail;
+            return -1;
         }
 
         fdt = load_device_tree(filename, &size);
         if (!fdt) {
             fprintf(stderr, "Couldn't open dtb file %s\n", filename);
-            g_free(filename);
             goto fail;
         }
-        g_free(filename);
     } else {
         fdt = binfo->get_dtb(binfo, &size);
         if (!fdt) {
-- 
2.47.2


Re: [PATCH 1/4] hw/arm: use g_autofree for filename in arm_load_dtb
Posted by Richard Henderson 3 weeks, 4 days ago
On 9/1/25 14:53, Alex Bennée wrote:
> The function has quite a number of exit cases so lets try and clean
> things up with g_autofree. As the fdt hasn't be allocated yet we can
> return directly from the fail point.
> 
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---
>   hw/arm/boot.c | 8 +++-----
>   1 file changed, 3 insertions(+), 5 deletions(-)

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

r~

> 
> diff --git a/hw/arm/boot.c b/hw/arm/boot.c
> index d391cd01bb1..56fd13b9f7c 100644
> --- a/hw/arm/boot.c
> +++ b/hw/arm/boot.c
> @@ -528,20 +528,18 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>       Error *err = NULL;
>   
>       if (binfo->dtb_filename) {
> -        char *filename;
> -        filename = qemu_find_file(QEMU_FILE_TYPE_DTB, binfo->dtb_filename);
> +        g_autofree char *filename = qemu_find_file(QEMU_FILE_TYPE_DTB,
> +                                                   binfo->dtb_filename);
>           if (!filename) {
>               fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
> -            goto fail;
> +            return -1;
>           }
>   
>           fdt = load_device_tree(filename, &size);
>           if (!fdt) {
>               fprintf(stderr, "Couldn't open dtb file %s\n", filename);
> -            g_free(filename);
>               goto fail;
>           }
> -        g_free(filename);
>       } else {
>           fdt = binfo->get_dtb(binfo, &size);
>           if (!fdt) {


Re: [PATCH 1/4] hw/arm: use g_autofree for filename in arm_load_dtb
Posted by Manos Pitsidianakis 3 weeks, 6 days ago
On Mon, Sep 1, 2025 at 3:54 PM Alex Bennée <alex.bennee@linaro.org> wrote:
>
> The function has quite a number of exit cases so lets try and clean
> things up with g_autofree. As the fdt hasn't be allocated yet we can
> return directly from the fail point.
>
> Signed-off-by: Alex Bennée <alex.bennee@linaro.org>
> ---

Reviewed-by: Manos Pitsidianakis <manos.pitsidianakis@linaro.org>


>  hw/arm/boot.c | 8 +++-----
>  1 file changed, 3 insertions(+), 5 deletions(-)
>
> diff --git a/hw/arm/boot.c b/hw/arm/boot.c
> index d391cd01bb1..56fd13b9f7c 100644
> --- a/hw/arm/boot.c
> +++ b/hw/arm/boot.c
> @@ -528,20 +528,18 @@ int arm_load_dtb(hwaddr addr, const struct arm_boot_info *binfo,
>      Error *err = NULL;
>
>      if (binfo->dtb_filename) {
> -        char *filename;
> -        filename = qemu_find_file(QEMU_FILE_TYPE_DTB, binfo->dtb_filename);
> +        g_autofree char *filename = qemu_find_file(QEMU_FILE_TYPE_DTB,
> +                                                   binfo->dtb_filename);
>          if (!filename) {
>              fprintf(stderr, "Couldn't open dtb file %s\n", binfo->dtb_filename);
> -            goto fail;
> +            return -1;
>          }
>
>          fdt = load_device_tree(filename, &size);
>          if (!fdt) {
>              fprintf(stderr, "Couldn't open dtb file %s\n", filename);
> -            g_free(filename);
>              goto fail;
>          }
> -        g_free(filename);
>      } else {
>          fdt = binfo->get_dtb(binfo, &size);
>          if (!fdt) {
> --
> 2.47.2
>
>