[PATCH] xen-mapcache: Fix a build error with -fexceptions

Alex Brett via qemu development posted 1 patch 1 day, 23 hours ago
There is a newer version of this series
hw/xen/xen-mapcache.c | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
[PATCH] xen-mapcache: Fix a build error with -fexceptions
Posted by Alex Brett via qemu development 1 day, 23 hours ago
When building with -Werror=maybe-uninitialized and -fexceptions, gcc can
report:
error: 'err' may be used uninitialized [-Werror=maybe-uninitialized]

err gets assigned to g_new0() unconditionally, but under -fexceptions
the calls to trace_xen_remap_bucket() or g_new0() might unwind, leading
the g_autofree cleanup to run g_free() on an uninitialised err. Avoid
the warning and any future issues by initialising it to NULL in the same
pattern as other definitions.

Signed-off-by: Alex Brett <alex.brett@citrix.com>
---
 hw/xen/xen-mapcache.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/hw/xen/xen-mapcache.c b/hw/xen/xen-mapcache.c
index e30c07c2ee..0a19cfdc1b 100644
--- a/hw/xen/xen-mapcache.c
+++ b/hw/xen/xen-mapcache.c
@@ -199,7 +199,7 @@ static void xen_remap_bucket(MapCache *mc,
     uint8_t *vaddr_base;
     g_autofree uint32_t *refs = NULL;
     g_autofree xen_pfn_t *pfns = NULL;
-    g_autofree int *err;
+    g_autofree int *err = NULL;
     unsigned int i;
     hwaddr nb_pfn = size >> XC_PAGE_SHIFT;
 
-- 
2.53.0
Re: [PATCH] xen-mapcache: Fix a build error with -fexceptions
Posted by Peter Maydell 1 day, 21 hours ago
On Tue, 24 Nov 2026 at 13:14, Alex Brett via qemu development
<qemu-devel@nongnu.org> wrote:
>
> When building with -Werror=maybe-uninitialized and -fexceptions, gcc can
> report:
> error: 'err' may be used uninitialized [-Werror=maybe-uninitialized]
>
> err gets assigned to g_new0() unconditionally, but under -fexceptions
> the calls to trace_xen_remap_bucket() or g_new0() might unwind, leading
> the g_autofree cleanup to run g_free() on an uninitialised err. Avoid
> the warning and any future issues by initialising it to NULL in the same
> pattern as other definitions.
>
> Signed-off-by: Alex Brett <alex.brett@citrix.com>
> ---
>  hw/xen/xen-mapcache.c | 2 +-
>  1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/hw/xen/xen-mapcache.c b/hw/xen/xen-mapcache.c
> index e30c07c2ee..0a19cfdc1b 100644
> --- a/hw/xen/xen-mapcache.c
> +++ b/hw/xen/xen-mapcache.c
> @@ -199,7 +199,7 @@ static void xen_remap_bucket(MapCache *mc,
>      uint8_t *vaddr_base;
>      g_autofree uint32_t *refs = NULL;
>      g_autofree xen_pfn_t *pfns = NULL;
> -    g_autofree int *err;
> +    g_autofree int *err = NULL;
>      unsigned int i;
>      hwaddr nb_pfn = size >> XC_PAGE_SHIFT;

Explicit init of a g_autofree pointer is the recommended pattern, so
Reviewed-by: Peter Maydell <peter.maydell@linaro.org>

but why are you building with "-fexceptions" ? Trying to throw
a C++ exception through QEMU's C code seems like it's likely
to run into all kinds of problems and corner cases...

thanks
-- PMM
Re: [PATCH] xen-mapcache: Fix a build error with -fexceptions
Posted by Alex Brett 1 day, 17 hours ago
> but why are you building with "-fexceptions" ? Trying to throw
> a C++ exception through QEMU's C code seems like it's likely
> to run into all kinds of problems and corner cases...

So "-fexceptions" is a default compiler flag for Fedora and derived
distributions - their justification is in the "Individual compiler flags"
section of
https://src.fedoraproject.org/rpms/redhat-rpm-config/blob/rawhide/f/buildflags.md

Alex