Skip to contentSkip to conversation

migration: Fix double-free on error path

PATCH In review Markus Armbruster 7 participants 25 messages Nov 25, 2025 – Dec 2, 2025

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 migration: Fix double-free on error path

Fixes ffaa1b50a879 (migration: Use warn_reportf_err() where appropriate)

Resolves: Coverity CID 1643463

---
 migration/multifd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/multifd.c +1 -1
index 6210454838..3203dc98e1 100644
@@ -450,7 +450,7 @@ static void multifd_send_set_error(Error *err)
  */
 static void migration_ioc_shutdown_gracefully(QIOChannel *ioc)
 {
-    g_autoptr(Error) local_err = NULL;
+    Error *local_err = NULL;
 
     if (!migration_has_failed(migrate_get_current()) &&
         object_dynamic_cast((Object *)ioc, TYPE_QIO_CHANNEL_TLS)) {
Signature · 1 line
2.49.0
#1 lore raw

I can include this in a PR I indend to send later today, but do feel free to commit it directly.

Acked-by
#2 lore raw
Quoted 2 lines · Markus Armbruster

I can include this in a PR I indend to send later today, but do feel free to commit it directly.

Works on my side:

Note: I think it's Richard's turn to collect pulls for 10.2.

Thanks,

Signature · 1 line
Peter Xu
Reviewed-by on v1 1/1
#3 lore raw
In reply to Markus Armbruster

Hi

Quoted 4 lines · Markus Armbruster
Fixes ffaa1b50a879 (migration: Use warn_reportf_err() where appropriate)

Resolves: Coverity CID 1643463

Quoted 18 lines
---
 migration/multifd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/multifd.c +1 -2
index 6210454838..3203dc98e1 100644
@@ -450,7 +450,7 @@ static void multifd_send_set_error(Error *err)
  */
 static void migration_ioc_shutdown_gracefully(QIOChannel *ioc)
 {
-    g_autoptr(Error) local_err = NULL;
+    Error *local_err = NULL;
 
     if (!migration_has_failed(migrate_get_current()) &&
         object_dynamic_cast((Object *)ioc, TYPE_QIO_CHANNEL_TLS)) {
--
Quoted 2 lines

2.49.0

Maybe warn_reportf_err() should take a Error **err instead, and clear it (and accept NULL values)

Signature · 1 line
Marc-André Lureau
#4 lore raw
Quoted 25 lines · Marc-André Lureau

Hi

Quoted 4 lines · Markus Armbruster
Fixes ffaa1b50a879 (migration: Use warn_reportf_err() where appropriate)

Resolves: Coverity CID 1643463

Quoted 15 lines
---
 migration/multifd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/multifd.c +1 -1
index 6210454838..3203dc98e1 100644
@@ -450,7 +450,7 @@ static void multifd_send_set_error(Error *err)
  */
 static void migration_ioc_shutdown_gracefully(QIOChannel *ioc)
 {
-    g_autoptr(Error) local_err = NULL;
+    Error *local_err = NULL;
 
Quoted 8 lines
Quoted 5 lines
migration/multifd.c +0 -1
     if (!migration_has_failed(migrate_get_current()) &&
         object_dynamic_cast((Object *)ioc, TYPE_QIO_CHANNEL_TLS)) {
--

2.49.0

Maybe warn_reportf_err() should take a Error **err instead, and clear it (and accept NULL values)

Our deallocating functions don't work that way.

Having them take a pointer by reference and clear it gets rid of *one* dangling reference. There may be more.

Coverity is fairly good at finding the kind of use after free this could avoid.

Error ** parameters are almost always for returning errors. Not having to wonder what such a parameter is for makes code easier to read.

Thanks for your review!

#5 lore raw
Quoted 36 lines · Markus Armbruster
Quoted 25 lines · Marc-André Lureau

Hi

Quoted 4 lines · Markus Armbruster
Fixes ffaa1b50a879 (migration: Use warn_reportf_err() where appropriate)

Resolves: Coverity CID 1643463

Quoted 15 lines
---
 migration/multifd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/multifd.c +1 -1
index 6210454838..3203dc98e1 100644
@@ -450,7 +450,7 @@ static void multifd_send_set_error(Error *err)
  */
 static void migration_ioc_shutdown_gracefully(QIOChannel *ioc)
 {
-    g_autoptr(Error) local_err = NULL;
+    Error *local_err = NULL;
 
Quoted 8 lines
Quoted 5 lines
migration/multifd.c +0 -1
     if (!migration_has_failed(migrate_get_current()) &&
         object_dynamic_cast((Object *)ioc, TYPE_QIO_CHANNEL_TLS)) {
--

2.49.0

Maybe warn_reportf_err() should take a Error **err instead, and clear it (and accept NULL values)

Quoted 4 lines

Our deallocating functions don't work that way.

Having them take a pointer by reference and clear it gets rid of *one* dangling reference. There may be more.

True. However I need to confess I like Marc-André's proposal.. Normally we only have one Error object, or >1 objects.

The only thing I'm not sure is such design doesn't match with the error API (e.g. current form matches the more famous error_report_err(), and likely others that I'm not familiar). So at least this will need some more thoughts before all the code churns.

Thanks,

Quoted 6 lines

Coverity is fairly good at finding the kind of use after free this could avoid.

Error ** parameters are almost always for returning errors. Not having to wonder what such a parameter is for makes code easier to read.

Signature · 1 line
Peter Xu
#6 lore raw
Quoted 37 lines · Peter Xu
Quoted 36 lines · Markus Armbruster
Quoted 33 lines · Marc-André Lureau

Hi

Quoted 4 lines · Markus Armbruster
Fixes ffaa1b50a879 (migration: Use warn_reportf_err() where appropriate)

Resolves: Coverity CID 1643463

Quoted 18 lines
---
 migration/multifd.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
migration/multifd.c +1 -2
index 6210454838..3203dc98e1 100644
@@ -450,7 +450,7 @@ static void multifd_send_set_error(Error *err)
  */
 static void migration_ioc_shutdown_gracefully(QIOChannel *ioc)
 {
-    g_autoptr(Error) local_err = NULL;
+    Error *local_err = NULL;
 
     if (!migration_has_failed(migrate_get_current()) &&
         object_dynamic_cast((Object *)ioc, TYPE_QIO_CHANNEL_TLS)) {
--
Quoted 2 lines

2.49.0

Maybe warn_reportf_err() should take a Error **err instead, and clear it (and accept NULL values)

Quoted 1 line
Quoted 1 line

Our deallocating functions don't work that way.

g_free(), g_realloc(), freeaddrinfo(), qapi_free_T(), visit_free(), qcrypto_FOO_free(), aio_task_pool_free(), qemu_opts_free(), timer_free(), ...

Quoted 10 lines
Quoted 2 lines

Having them take a pointer by reference and clear it gets rid of *one* dangling reference. There may be more.

True. However I need to confess I like Marc-André's proposal.. Normally we only have one Error object, or >1 objects.

The only thing I'm not sure is such design doesn't match with the error API (e.g. current form matches the more famous error_report_err(), and likely others that I'm not familiar). So at least this will need some more thoughts before all the code churns.

The error.h functions and macros that free an Error object are:

* error_free(), error_free_or_abort()

These take a single Error * argument.

* error_report_err(), warn_report_err(), error_reportf_err(), warn_reportf_err(). warn_report_err_once_cond(), warn_report_err_once()

These take also a single Error * argument.

* error_propagate(), error_propagate_prepend()

These take an Error ** destination, and an Error * object to be propagated. They take ownership of the latter. They either store it in the destination, or free it.

Changing the latter to Error ** makes these functions vulnerable to swapped arguments. See "Error ** parameters are almost always for returning errors" below.

* ERRP_GUARD()

Includes automatic error_propagate() on return, immune to use after free.

Quoted 9 lines

Thanks,

Quoted 6 lines

Coverity is fairly good at finding the kind of use after free this could avoid.

Error ** parameters are almost always for returning errors. Not having to wonder what such a parameter is for makes code easier to read.

My main argument remains this one: our deallocating functions don't work that way. For better or worse.

I don't want the Error API free differently.

#7 lore raw
Quoted 4 lines · Markus Armbruster

My main argument remains this one: our deallocating functions don't work that way. For better or worse.

I don't want the Error API free differently.

Yes, it's fair.

IMHO, it's a matter of taste, and maybe that's also why I liked g_clear_pointer() but not everyone likes it.. said that, when we have g_clear_pointer() it also makes the current form more flexible, because we can apply g_clear_pointer() on top when we need a reset..

I'll follow your advise on error reporting for migration.

Thanks,

Signature · 1 line
Peter Xu
#8 lore raw

Should functions that free memory clear the pointer? (was: [PATCH] migration: Fix double-free on error path)

Quoted 9 lines · Peter Xu
Quoted 4 lines · Markus Armbruster

My main argument remains this one: our deallocating functions don't work that way. For better or worse.

I don't want the Error API free differently.

Yes, it's fair.

IMHO, it's a matter of taste,

Depends on context.

Modula-2's DISPOSE takes the pointer by reference and clears it. C's free() takes it by value. Matter of taste when these were designed.

But when one style has become overwhelmingly prevalent, it's no longer a matter of taste. This is arguably the case in C.

Quoted 4 lines

and maybe that's also why I liked
g_clear_pointer() but not everyone likes it.. said that, when we have g_clear_pointer() it also makes the current form more flexible, because we can apply g_clear_pointer() on top when we need a reset..

I'm no friend of g_clear_pointer(). I find

    g_clear_pointer(&ptr, free_frob);

a rather roundabout way to say

    free_frob(ptr);
    ptr = NULL;

Any C programmer will immediately understand the latter. For the former, you need to know one more little thing. Yes, we can all get used to these little things, but it's one more little thing new people have to learn and internalize. Even little things add up.

If an entire project gets into the habit of using it religiously, it may reduce "forgot to zap the reference" bugs some. Until then, it feels like a net negative to me.

Quoted 1 line

I'll follow your advise on error reporting for migration.

Thank you!

#9 lore raw

Re: Should functions that free memory clear the pointer?

Quoted 34 lines · Markus Armbruster
Quoted 9 lines · Peter Xu
Quoted 4 lines · Markus Armbruster

My main argument remains this one: our deallocating functions don't work that way. For better or worse.

I don't want the Error API free differently.

Yes, it's fair.

IMHO, it's a matter of taste,

Depends on context.

Modula-2's DISPOSE takes the pointer by reference and clears it. C's free() takes it by value. Matter of taste when these were designed.

But when one style has become overwhelmingly prevalent, it's no longer a matter of taste. This is arguably the case in C.

Quoted 4 lines

and maybe that's also why I liked
g_clear_pointer() but not everyone likes it.. said that, when we have g_clear_pointer() it also makes the current form more flexible, because we can apply g_clear_pointer() on top when we need a reset..

I'm no friend of g_clear_pointer(). I find

     g_clear_pointer(&ptr, free_frob);

a rather roundabout way to say

     free_frob(ptr);
     ptr = NULL;
Quoted 8 lines

Any C programmer will immediately understand the latter. For the former, you need to know one more little thing. Yes, we can all get used to these little things, but it's one more little thing new people have to learn and internalize. Even little things add up.

If an entire project gets into the habit of using it religiously, it may reduce "forgot to zap the reference" bugs some. Until then, it feels like a net negative to me.

I'm not a big fan of g_clear_pointer() either; I prefer being explicit, even if it means a bit of redundancy.

Thanks,

C.

Quoted 6 lines
Quoted 2 lines

I'll follow your advise on error reporting for migration.

Thank you!

#11 lore raw

g_autoptr(Error) (was: [PATCH] migration: Fix double-free on error path)

In reply to Markus Armbruster

g_autoptr(T) is quite useful when the object's extent matches the function's.

This isn't the case for an Error object the function propagates to its caller. It is the case for an Error object the function reports or handles itself. However, the functions to report Error also free it.

Thus, g_autoptr(Error) is rarely applicable. We have just three instances out of >1100 local Error variables, all in migration code.

Two want to move the error to the MigrationState for later handling / reporting. Since migrate_set_error() doesn't move, but stores a copy, the original needs to be freed, and g_autoptr() is correct there. We have 17 more that instead manually free with error_free() or error_report_err() right after migrate_set_error().

We recently discussed storing a copy vs. move the original:

From: Peter Xu <[email protected]>
Subject: Re: [PATCH 0/3] migration: Error fixes and improvements
Date: Mon, 17 Nov 2025 11:03:37 -0500
Message-ID: <[email protected]>

The two g_autoptr() gave me pause when I investigated this topic, simply because they deviate from the common pattern migrate_set_error(s, err) followed by error_free() or error_report_err().

The third one became wrong when I cleaned up the reporting (missed in the cleanup patch, fixed in the patch I'm replying to). I suspect my mistake escaped review for the same reason I made it: g_autoptr(Error) is unusual and not visible in the patch hunk.

Would you like me to replace the two correct uses of g_autoptr(Error) by more common usage?

Reviewed-by Peter Xu
#12 lore raw

Re: g_autoptr(Error) (was: [PATCH] migration: Fix double-free on error path)

Quoted 27 lines · Markus Armbruster

g_autoptr(T) is quite useful when the object's extent matches the function's.

This isn't the case for an Error object the function propagates to its caller. It is the case for an Error object the function reports or handles itself. However, the functions to report Error also free it.

Thus, g_autoptr(Error) is rarely applicable. We have just three instances out of >1100 local Error variables, all in migration code.

Two want to move the error to the MigrationState for later handling / reporting. Since migrate_set_error() doesn't move, but stores a copy, the original needs to be freed, and g_autoptr() is correct there. We have 17 more that instead manually free with error_free() or error_report_err() right after migrate_set_error().

We recently discussed storing a copy vs. move the original:

From: Peter Xu <[email protected]>
Subject: Re: [PATCH 0/3] migration: Error fixes and improvements
Date: Mon, 17 Nov 2025 11:03:37 -0500
Message-ID: <[email protected]>

The two g_autoptr() gave me pause when I investigated this topic, simply because they deviate from the common pattern migrate_set_error(s, err) followed by error_free() or error_report_err().

Quoted 7 lines

The third one became wrong when I cleaned up the reporting (missed in the cleanup patch, fixed in the patch I'm replying to). I suspect my mistake escaped review for the same reason I made it: g_autoptr(Error) is unusual and not visible in the patch hunk.

Would you like me to replace the two correct uses of g_autoptr(Error) by more common usage?

I had previously proposed g_autoptr(Error) a year or two back and you rejected it then, so I'm surprised to see that it got into the code, because it requires explicit opt-in via a G_DEFINE_AUTOPTR_CLEANUP_FUNC.

Unfortunately it appears exactly that was added earlier this year in

commit 18eb55546a54e443d94a4c49286348176ad4b00a
Author: Maciej S. Szmigiero <[email protected]>
Date: Tue Mar 4 23:03:35 2025 +0100

error: define g_autoptr() cleanup function for the Error type

Automatic memory management helps avoid memory safety issues.

When removing this usage, ensure that commit is reverted too, which will prevent anyone unwittingly re-introducing g_autoptr(Error) usage

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#13 lore raw

Re: g_autoptr(Error)

Quoted 46 lines · Daniel P. Berrangé
Quoted 27 lines · Markus Armbruster

g_autoptr(T) is quite useful when the object's extent matches the function's.

This isn't the case for an Error object the function propagates to its caller. It is the case for an Error object the function reports or handles itself. However, the functions to report Error also free it.

Thus, g_autoptr(Error) is rarely applicable. We have just three instances out of >1100 local Error variables, all in migration code.

Two want to move the error to the MigrationState for later handling / reporting. Since migrate_set_error() doesn't move, but stores a copy, the original needs to be freed, and g_autoptr() is correct there. We have 17 more that instead manually free with error_free() or error_report_err() right after migrate_set_error().

We recently discussed storing a copy vs. move the original:

From: Peter Xu <[email protected]>
Subject: Re: [PATCH 0/3] migration: Error fixes and improvements
Date: Mon, 17 Nov 2025 11:03:37 -0500
Message-ID: <[email protected]>

The two g_autoptr() gave me pause when I investigated this topic, simply because they deviate from the common pattern migrate_set_error(s, err) followed by error_free() or error_report_err().

Quoted 7 lines

The third one became wrong when I cleaned up the reporting (missed in the cleanup patch, fixed in the patch I'm replying to). I suspect my mistake escaped review for the same reason I made it: g_autoptr(Error) is unusual and not visible in the patch hunk.

Would you like me to replace the two correct uses of g_autoptr(Error) by more common usage?

I had previously proposed g_autoptr(Error) a year or two back and you rejected it then, so I'm surprised to see that it got into the code, because it requires explicit opt-in via a G_DEFINE_AUTOPTR_CLEANUP_FUNC.

Unfortunately it appears exactly that was added earlier this year in

commit 18eb55546a54e443d94a4c49286348176ad4b00a
Author: Maciej S. Szmigiero <[email protected]>
Date: Tue Mar 4 23:03:35 2025 +0100

Quoted 8 lines

error: define g_autoptr() cleanup function for the Error type

Automatic memory management helps avoid memory safety issues.

I missed it. Not he submitter's fault; it was cc'ed to me.

Quoted 3 lines

When removing this usage, ensure that commit is reverted too, which will prevent anyone unwittingly re-introducing g_autoptr(Error) usage

Thanks for the pointer!

#14 lore raw

Re: g_autoptr(Error)

Quoted 9 lines · Markus Armbruster
Quoted 7 lines · Daniel P. Berrangé
Quoted 6 lines · Markus Armbruster

g_autoptr(T) is quite useful when the object's extent matches the function's.

This isn't the case for an Error object the function propagates to its caller. It is the case for an Error object the function reports or handles itself. However, the functions to report Error also free it.

I'd confess I didn't pay enough attention on how the error API was designed deliberately to always free the Error objects before almost whenever possible. But I see now, thanks for the write up.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

Thus, g_autoptr(Error) is rarely applicable. We have just three instances out of >1100 local Error variables, all in migration code.

Two want to move the error to the MigrationState for later handling / reporting. Since migrate_set_error() doesn't move, but stores a copy, the original needs to be freed, and g_autoptr() is correct there. We have 17 more that instead manually free with error_free() or error_report_err() right after migrate_set_error().

We recently discussed storing a copy vs. move the original:

From: Peter Xu <[email protected]>
Subject: Re: [PATCH 0/3] migration: Error fixes and improvements
Date: Mon, 17 Nov 2025 11:03:37 -0500
Message-ID: <[email protected]>

The two g_autoptr() gave me pause when I investigated this topic, simply because they deviate from the common pattern migrate_set_error(s, err) followed by error_free() or error_report_err().

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

The third one became wrong when I cleaned up the reporting (missed in the cleanup patch, fixed in the patch I'm replying to). I suspect my mistake escaped review for the same reason I made it: g_autoptr(Error) is unusual and not visible in the patch hunk.

Would you like me to replace the two correct uses of g_autoptr(Error) by more common usage?

Works for me.

Now I also think it should be good migrate_set_error() follow QEMU's Error API design if we decide to stick with it freeing errors in such APIs.

Said that, I wonder if you think we could still consider passing Error** into migrate_set_error(), though, which will be a merged solution of current Error API and what Marc-Andre proposed on resetting pointers to avoid any possible UAF, which I would still slightly prefer personally.

If we rework migrate_set_error() to take ownership first, then we can naturally drop the two use cases, and remove the cleanup function.

Markus, please also let me know if you want me to do it.

Quoted 13 lines
Quoted 13 lines

I had previously proposed g_autoptr(Error) a year or two back and you rejected it then, so I'm surprised to see that it got into the code, because it requires explicit opt-in via a G_DEFINE_AUTOPTR_CLEANUP_FUNC.

Unfortunately it appears exactly that was added earlier this year in

commit 18eb55546a54e443d94a4c49286348176ad4b00a
Author: Maciej S. Szmigiero <[email protected]>
Date: Tue Mar 4 23:03:35 2025 +0100

error: define g_autoptr() cleanup function for the Error type

Quoted 8 lines
Quoted 6 lines

Automatic memory management helps avoid memory safety issues.

I missed it. Not he submitter's fault; it was cc'ed to me.

If someone to blame, it's the reviewer.

Thanks!

Signature · 1 line
Peter Xu
#15 lore raw

Re: g_autoptr(Error)

Quoted 7 lines · Peter Xu
Quoted 6 lines · Markus Armbruster
Quoted 4 lines · Daniel P. Berrangé
Quoted 3 lines · Markus Armbruster

g_autoptr(T) is quite useful when the object's extent matches the function's.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

This isn't the case for an Error object the function propagates to its caller. It is the case for an Error object the function reports or handles itself. However, the functions to report Error also free it.

I'd confess I didn't pay enough attention on how the error API was designed deliberately to always free the Error objects before almost whenever possible. But I see now, thanks for the write up.

You're welcome!

Quoted 39 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

Thus, g_autoptr(Error) is rarely applicable. We have just three instances out of >1100 local Error variables, all in migration code.

Two want to move the error to the MigrationState for later handling / reporting. Since migrate_set_error() doesn't move, but stores a copy, the original needs to be freed, and g_autoptr() is correct there. We have 17 more that instead manually free with error_free() or error_report_err() right after migrate_set_error().

We recently discussed storing a copy vs. move the original:

From: Peter Xu <[email protected]>
Subject: Re: [PATCH 0/3] migration: Error fixes and improvements
Date: Mon, 17 Nov 2025 11:03:37 -0500
Message-ID: <[email protected]>

The two g_autoptr() gave me pause when I investigated this topic, simply because they deviate from the common pattern migrate_set_error(s, err) followed by error_free() or error_report_err().

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

The third one became wrong when I cleaned up the reporting (missed in the cleanup patch, fixed in the patch I'm replying to). I suspect my mistake escaped review for the same reason I made it: g_autoptr(Error) is unusual and not visible in the patch hunk.

Would you like me to replace the two correct uses of g_autoptr(Error) by more common usage?

Works for me.

Now I also think it should be good migrate_set_error() follow QEMU's Error API design if we decide to stick with it freeing errors in such APIs.

Said that, I wonder if you think we could still consider passing Error** into migrate_set_error(), though, which will be a merged solution of current Error API and what Marc-Andre proposed on resetting pointers to avoid any possible UAF, which I would still slightly prefer personally.

Quoted 4 lines

If we rework migrate_set_error() to take ownership first, then we can naturally drop the two use cases, and remove the cleanup function.

Markus, please also let me know if you want me to do it.

I think the first step should replace the two g_autoptr() by error_free(), then delete g_autoptr() support.

A possible second step is to replace migrate_set_error() by a function that takes ownership. "Replace" because I think migrate_set_error() would be a bad name for such a function. What's a better name? Naming is hard... migrate_error_propagate_to_state()? Because there's similarity:

    error_propagate(errp, err);

stores @err in @errp, or else frees it, and

migrate_error_propagate_to_state(s, err)

stores @err in @s, or else frees it.

We could also forgo encapsulation and simply use

    error_propagate(&s->error, err);

Matter of taste, which means migration maintainers decide.

I can do just the first step, or both. Up to you.

[...]

#16 lore raw

Re: g_autoptr(Error)

Quoted 72 lines · Markus Armbruster
Quoted 7 lines · Peter Xu
Quoted 6 lines · Markus Armbruster
Quoted 4 lines · Daniel P. Berrangé
Quoted 3 lines · Markus Armbruster

g_autoptr(T) is quite useful when the object's extent matches the function's.

Quoted 7 lines
Quoted 3 lines
Quoted 3 lines
Quoted 3 lines

This isn't the case for an Error object the function propagates to its caller. It is the case for an Error object the function reports or handles itself. However, the functions to report Error also free it.

I'd confess I didn't pay enough attention on how the error API was designed deliberately to always free the Error objects before almost whenever possible. But I see now, thanks for the write up.

You're welcome!

Quoted 39 lines
Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

Thus, g_autoptr(Error) is rarely applicable. We have just three instances out of >1100 local Error variables, all in migration code.

Two want to move the error to the MigrationState for later handling / reporting. Since migrate_set_error() doesn't move, but stores a copy, the original needs to be freed, and g_autoptr() is correct there. We have 17 more that instead manually free with error_free() or error_report_err() right after migrate_set_error().

We recently discussed storing a copy vs. move the original:

From: Peter Xu <[email protected]>
Subject: Re: [PATCH 0/3] migration: Error fixes and improvements
Date: Mon, 17 Nov 2025 11:03:37 -0500
Message-ID: <[email protected]>

The two g_autoptr() gave me pause when I investigated this topic, simply because they deviate from the common pattern migrate_set_error(s, err) followed by error_free() or error_report_err().

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

The third one became wrong when I cleaned up the reporting (missed in the cleanup patch, fixed in the patch I'm replying to). I suspect my mistake escaped review for the same reason I made it: g_autoptr(Error) is unusual and not visible in the patch hunk.

Would you like me to replace the two correct uses of g_autoptr(Error) by more common usage?

Works for me.

Now I also think it should be good migrate_set_error() follow QEMU's Error API design if we decide to stick with it freeing errors in such APIs.

Said that, I wonder if you think we could still consider passing Error** into migrate_set_error(), though, which will be a merged solution of current Error API and what Marc-Andre proposed on resetting pointers to avoid any possible UAF, which I would still slightly prefer personally.

Quoted 4 lines

If we rework migrate_set_error() to take ownership first, then we can naturally drop the two use cases, and remove the cleanup function.

Markus, please also let me know if you want me to do it.

I think the first step should replace the two g_autoptr() by error_free(), then delete g_autoptr() support.

A possible second step is to replace migrate_set_error() by a function that takes ownership. "Replace" because I think migrate_set_error() would be a bad name for such a function. What's a better name? Naming is hard... migrate_error_propagate_to_state()? Because there's similarity:

Quoted 5 lines
    error_propagate(errp, err);

stores @err in @errp, or else frees it, and

migrate_error_propagate_to_state(s, err)

I took this one but dropped to_state to make it shorter (and also dropped "s" to make it g_clear_pointer() friendly).

Quoted 10 lines

stores @err in @s, or else frees it.

We could also forgo encapsulation and simply use

    error_propagate(&s->error, err);

Matter of taste, which means migration maintainers decide.

I can do just the first step, or both. Up to you.

I sent the patches here for both of the issues discussed (I should still owe some other patches; I'll do them separately..):

https://lore.kernel.org/r/[email protected]

Comments more than welcomed.

Thanks,

Signature · 1 line
Peter Xu
#17 lore raw

Re: g_autoptr(Error)

In reply to Peter Xu
Quoted 74 lines · Peter Xu
Quoted 9 lines · Markus Armbruster
Quoted 7 lines · Daniel P. Berrangé
Quoted 6 lines · Markus Armbruster

g_autoptr(T) is quite useful when the object's extent matches the function's.

This isn't the case for an Error object the function propagates to its caller. It is the case for an Error object the function reports or handles itself. However, the functions to report Error also free it.

I'd confess I didn't pay enough attention on how the error API was designed deliberately to always free the Error objects before almost whenever possible. But I see now, thanks for the write up.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

Thus, g_autoptr(Error) is rarely applicable. We have just three instances out of >1100 local Error variables, all in migration code.

Two want to move the error to the MigrationState for later handling / reporting. Since migrate_set_error() doesn't move, but stores a copy, the original needs to be freed, and g_autoptr() is correct there. We have 17 more that instead manually free with error_free() or error_report_err() right after migrate_set_error().

We recently discussed storing a copy vs. move the original:

From: Peter Xu <[email protected]>
Subject: Re: [PATCH 0/3] migration: Error fixes and improvements
Date: Mon, 17 Nov 2025 11:03:37 -0500
Message-ID: <[email protected]>

The two g_autoptr() gave me pause when I investigated this topic, simply because they deviate from the common pattern migrate_set_error(s, err) followed by error_free() or error_report_err().

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

The third one became wrong when I cleaned up the reporting (missed in the cleanup patch, fixed in the patch I'm replying to). I suspect my mistake escaped review for the same reason I made it: g_autoptr(Error) is unusual and not visible in the patch hunk.

Would you like me to replace the two correct uses of g_autoptr(Error) by more common usage?

Works for me.

Now I also think it should be good migrate_set_error() follow QEMU's Error API design if we decide to stick with it freeing errors in such APIs.

Said that, I wonder if you think we could still consider passing Error** into migrate_set_error(), though, which will be a merged solution of current Error API and what Marc-Andre proposed on resetting pointers to avoid any possible UAF, which I would still slightly prefer personally.

If we rework migrate_set_error() to take ownership first, then we can naturally drop the two use cases, and remove the cleanup function.

Markus, please also let me know if you want me to do it.

Quoted 15 lines
Quoted 15 lines

I had previously proposed g_autoptr(Error) a year or two back and you rejected it then, so I'm surprised to see that it got into the code, because it requires explicit opt-in via a G_DEFINE_AUTOPTR_CLEANUP_FUNC.

Unfortunately it appears exactly that was added earlier this year in

commit 18eb55546a54e443d94a4c49286348176ad4b00a
Author: Maciej S. Szmigiero <[email protected]>
Date: Tue Mar 4 23:03:35 2025 +0100

error: define g_autoptr() cleanup function for the Error type

Automatic memory management helps avoid memory safety issues.

Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

I missed it. Not he submitter's fault; it was cc'ed to me.

If someone to blame, it's the reviewer.

At end, I was the one who merged this stuff. My bad.

I felt confident at the time, as it was only a single-line change reviewed by a subsystem maintainer and the patch was large enough that this didn't raise my attention.

But it should have been treated with greater caution, global features must be introduced together with concrete usage proposals. I think this would have raised some unconscious red flags.

Thanks,

C.

#18 lore raw

Re: g_autoptr(Error)

Quoted 78 lines · Cédric Le Goater
Quoted 77 lines · Peter Xu
Quoted 9 lines · Markus Armbruster
Quoted 7 lines · Daniel P. Berrangé
Quoted 6 lines · Markus Armbruster

g_autoptr(T) is quite useful when the object's extent matches the function's.

This isn't the case for an Error object the function propagates to its caller.  It is the case for an Error object the function reports or handles itself.  However, the functions to report Error also free it.

I'd confess I didn't pay enough attention on how the error API was designed deliberately to always free the Error objects before almost whenever possible.  But I see now, thanks for the write up.

Quoted 21 lines
Quoted 21 lines
Quoted 21 lines

Thus, g_autoptr(Error) is rarely applicable.  We have just three instances out of >1100 local Error variables, all in migration code.

Two want to move the error to the MigrationState for later handling / reporting.  Since migrate_set_error() doesn't move, but stores a copy, the original needs to be freed, and g_autoptr() is correct there.  We have 17 more that instead manually free with error_free() or error_report_err() right after migrate_set_error().

We recently discussed storing a copy vs. move the original:

     From: Peter Xu <[email protected]>
     Subject: Re: [PATCH 0/3] migration: Error fixes and improvements
     Date: Mon, 17 Nov 2025 11:03:37 -0500
     Message-ID: <[email protected]>

The two g_autoptr() gave me pause when I investigated this topic, simply because they deviate from the common pattern migrate_set_error(s, err) followed by error_free() or error_report_err().

Quoted 7 lines
Quoted 7 lines
Quoted 7 lines

The third one became wrong when I cleaned up the reporting (missed in the cleanup patch, fixed in the patch I'm replying to).  I suspect my mistake escaped review for the same reason I made it: g_autoptr(Error) is unusual and not visible in the patch hunk.

Would you like me to replace the two correct uses of g_autoptr(Error) by more common usage?

Works for me.

Now I also think it should be good migrate_set_error() follow QEMU's Error API design if we decide to stick with it freeing errors in such APIs.

Said that, I wonder if you think we could still consider passing Error** into migrate_set_error(), though, which will be a merged solution of current Error API and what Marc-Andre proposed on resetting pointers to avoid any possible UAF, which I would still slightly prefer personally.

If we rework migrate_set_error() to take ownership first, then we can naturally drop the two use cases, and remove the cleanup function.

Markus, please also let me know if you want me to do it.

Quoted 18 lines
Quoted 17 lines

I had previously proposed g_autoptr(Error) a year or two back and you rejected it then, so I'm surprised to see that it got into the code, because it requires explicit opt-in via a G_DEFINE_AUTOPTR_CLEANUP_FUNC.

Unfortunately it appears exactly that was added earlier this year in

   commit 18eb55546a54e443d94a4c49286348176ad4b00a
   Author: Maciej S. Szmigiero <[email protected]> Date:   Tue Mar 4 23:03:35 2025 +0100

     error: define g_autoptr() cleanup function for the Error type Automatic memory management helps avoid memory safety issues.

Quoted 7 lines
Quoted 3 lines
Quoted 1 line

I missed it.  Not he submitter's fault; it was cc'ed to me.

If someone to blame, it's the reviewer.

At end, I was the one who merged this stuff. My bad.

I felt confident at the time, as it was only a single-line change reviewed by a subsystem maintainer and the patch was large enough that this didn't

s/patch/series/ makes more sense.

Sorry for the noise.

C.

Quoted 15 lines

raise my attention.

But it should have been treated with greater caution, global features must be introduced together with concrete usage proposals. I think this would have raised some unconscious red flags.

Thanks,

C.

#19 lore raw

Re: g_autoptr(Error)

Quoted 3 lines · Cédric Le Goater
Quoted 2 lines · Cédric Le Goater
Quoted 1 line · Peter Xu

On Tue, Nov 25, 2025 at 12:46:01PM +0100, Markus Armbruster wrote:

[...]

On the review and merging of commit 18eb55546a5 (error: define g_autoptr() cleanup function for the Error type):

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

I missed it.  Not he submitter's fault; it was cc'ed to me.

If someone to blame, it's the reviewer.

At end, I was the one who merged this stuff. My bad.

Quoted 17 lines
Quoted 2 lines

I felt confident at the time, as it was only a single-line change reviewed by a subsystem maintainer and the patch was large enough that this didn't

s/patch/series/ makes more sense.

Sorry for the noise.

C.

Quoted 7 lines

raise my attention.

But it should have been treated with greater caution, global features must be introduced together with concrete usage proposals. I think this would have raised some unconscious red flags.
Thanks,
C.

Don't worry about it! From my point of view, the process worked okay. A big series got reviewed by maintainers, except for one little patch touching another subsystem, where that subsystem's maintainer (me) remained silent. The series was then merged without further delay.

Would I have appreciate a timely nudge on that little patch? Sure. Is not nudging me a failure of sorts? Nope.

#20 lore raw

Re: g_autoptr(Error)

Quoted 34 lines · Markus Armbruster
Quoted 3 lines · Cédric Le Goater
Quoted 2 lines · Cédric Le Goater
Quoted 1 line · Peter Xu

On Tue, Nov 25, 2025 at 12:46:01PM +0100, Markus Armbruster wrote:

[...]

On the review and merging of commit 18eb55546a5 (error: define g_autoptr() cleanup function for the Error type):

Quoted 5 lines
Quoted 5 lines
Quoted 3 lines
Quoted 1 line

I missed it.  Not he submitter's fault; it was cc'ed to me.

If someone to blame, it's the reviewer.

At end, I was the one who merged this stuff. My bad.

Quoted 17 lines
Quoted 2 lines

I felt confident at the time, as it was only a single-line change reviewed by a subsystem maintainer and the patch was large enough that this didn't

s/patch/series/ makes more sense.

Sorry for the noise.

C.

Quoted 7 lines

raise my attention.

But it should have been treated with greater caution, global features must be introduced together with concrete usage proposals. I think this would have raised some unconscious red flags.
Thanks,
C.

Quoted 7 lines

Don't worry about it! From my point of view, the process worked okay. A big series got reviewed by maintainers, except for one little patch touching another subsystem, where that subsystem's maintainer (me) remained silent. The series was then merged without further delay.

Would I have appreciate a timely nudge on that little patch? Sure. Is not nudging me a failure of sorts? Nope.

The other thing that plays in here is that we actively encourage use of g_autoptr everywhere. It is very unusual for "Error" to be a type that does NOT want g_autoptr, and thus the mistake is very much on the cards.

I've proposed it before myself & Markus caught it. I also caught one other proposals to add it since my attempt. This third time it slipped through review. I expect we'll see a 4th attempt to add it at some point.

With regards,
Daniel

Signature · 3 lines
|: https://berrange.com      -o-    https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org         -o-            https://fstop138.berrange.com :|
|: https://entangle-photo.org    -o-    https://www.instagram.com/dberrange :|
#21 lore raw

Re: g_autoptr(Error)

Quoted 1 line · Daniel P. Berrangé

On Wed, Nov 26, 2025 at 12:46:40PM +0100, Markus Armbruster wrote:

[...]

Quoted 10 lines
Quoted 7 lines

Don't worry about it! From my point of view, the process worked okay. A big series got reviewed by maintainers, except for one little patch touching another subsystem, where that subsystem's maintainer (me) remained silent. The series was then merged without further delay.

Would I have appreciate a timely nudge on that little patch? Sure. Is not nudging me a failure of sorts? Nope.

The other thing that plays in here is that we actively encourage use of g_autoptr everywhere.

We do, and for good reasons.

Quoted 6 lines

It is very unusual for "Error" to be a type that
does NOT want g_autoptr, and thus the mistake is very much on the cards.

I've proposed it before myself & Markus caught it. I also caught one other proposals to add it since my attempt. This third time it slipped through review. I expect we'll see a 4th attempt to add it at some point.

%-}

#22 lore raw

Re: g_autoptr(Error)

In reply to Daniel P. Berrangé
Quoted 7 lines · Daniel P. Berrangé

The other thing that plays in here is that we actively encourage use of g_autoptr everywhere. It is very unusual for "Error" to be a type that does NOT want g_autoptr, and thus the mistake is very much on the cards.

I've proposed it before myself & Markus caught it. I also caught one other proposals to add it since my attempt. This third time it slipped through review. I expect we'll see a 4th attempt to add it at some point.

We could add a comment to the header explaining the rationale for not providing a g_autoptr handler, so that at least we have it written down, and are more likely to remember it when it comes up again in future...

-- PMM

#23 lore raw

Re: g_autoptr(Error)

Quoted 5 lines · Peter Maydell
Quoted 4 lines · Daniel P. Berrangé

The other thing that plays in here is that we actively encourage use of g_autoptr everywhere. It is very unusual for "Error" to be a type that does NOT want g_autoptr, and thus the mistake is very much on the cards.

Quoted 8 lines
Quoted 3 lines

I've proposed it before myself & Markus caught it. I also caught one other proposals to add it since my attempt. This third time it slipped through review. I expect we'll see a 4th attempt to add it at some point.

We could add a comment to the header explaining the rationale for not providing a g_autoptr handler, so that at least we have it written down, and are more likely to remember it when it comes up again in future...

Good idea. I'll take care of it.