All of lore.kernel.org
 help / color / mirror / Atom feed
From: Markus Armbruster <armbru@redhat.com>
To: Peter Xu <peterx@redhat.com>
Cc: "Markus Armbruster" <armbru@redhat.com>,
	"Marc-André Lureau" <marcandre.lureau@gmail.com>,
	qemu-devel@nongnu.org, farosas@suse.de, peter.maydell@linaro.org
Subject: Should functions that free memory clear the pointer? (was: [PATCH] migration: Fix double-free on error path)
Date: Wed, 26 Nov 2025 07:12:38 +0100	[thread overview]
Message-ID: <87wm3dfhk9.fsf_-_@pond.sub.org> (raw)
In-Reply-To: <aSYEO_S7FfAeZyHG@x1.local> (Peter Xu's message of "Tue, 25 Nov 2025 14:32:11 -0500")

Peter Xu <peterx@redhat.com> writes:

> On Tue, Nov 25, 2025 at 07:48:44PM +0100, Markus Armbruster wrote:
>> 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.

>                               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.

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

Thank you!



  reply	other threads:[~2025-11-26  6:14 UTC|newest]

Thread overview: 25+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-11-25  7:05 [PATCH] migration: Fix double-free on error path Markus Armbruster
2025-11-25  7:11 ` Markus Armbruster
2025-11-25 15:41   ` Peter Xu
2025-11-25  7:12 ` Marc-André Lureau
2025-11-25 12:59   ` Markus Armbruster
2025-11-25 15:47     ` Peter Xu
2025-11-25 18:48       ` Markus Armbruster
2025-11-25 19:32         ` Peter Xu
2025-11-26  6:12           ` Markus Armbruster [this message]
2025-11-26  8:21             ` Should functions that free memory clear the pointer? Cédric Le Goater
2025-11-25  7:16 ` [PATCH] migration: Fix double-free on error path Philippe Mathieu-Daudé
2025-11-25  7:40 ` g_autoptr(Error) (was: [PATCH] migration: Fix double-free on error path) Markus Armbruster
2025-11-25 11:25   ` Daniel P. Berrangé
2025-11-25 11:46     ` g_autoptr(Error) Markus Armbruster
2025-11-25 16:15       ` g_autoptr(Error) Peter Xu
2025-11-25 19:02         ` g_autoptr(Error) Markus Armbruster
2025-11-25 20:49           ` g_autoptr(Error) Peter Xu
2025-11-26  8:19         ` g_autoptr(Error) Cédric Le Goater
2025-11-26 10:26           ` g_autoptr(Error) Cédric Le Goater
2025-11-26 11:46             ` g_autoptr(Error) Markus Armbruster
2025-11-26 12:00               ` g_autoptr(Error) Daniel P. Berrangé
2025-11-26 12:41                 ` g_autoptr(Error) Markus Armbruster
2025-11-26 13:27                 ` g_autoptr(Error) Peter Maydell
2025-11-26 14:01                   ` g_autoptr(Error) Markus Armbruster
2025-12-02  8:34 ` [PATCH] migration: Fix double-free on error path Markus Armbruster

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wm3dfhk9.fsf_-_@pond.sub.org \
    --to=armbru@redhat.com \
    --cc=farosas@suse.de \
    --cc=marcandre.lureau@gmail.com \
    --cc=peter.maydell@linaro.org \
    --cc=peterx@redhat.com \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.