From: Fabiano Rosas <farosas@suse.de>
To: Prasad Pandit <ppandit@redhat.com>
Cc: qemu-devel@nongnu.org, peterx@redhat.com, armbru@redhat.com,
Peter Maydell <peter.maydell@linaro.org>
Subject: Re: [PATCH] migration/options: Fix leaks in StrOrNull accessors
Date: Fri, 23 Jan 2026 10:15:31 -0300 [thread overview]
Message-ID: <87v7gsii6k.fsf@suse.de> (raw)
In-Reply-To: <CAE8KmOwWhtTHSh=cbDS7CsgEKS7tf4+HRcJ8ax3Qfnxqgn-YyQ@mail.gmail.com>
Prasad Pandit <ppandit@redhat.com> writes:
> On Fri, 23 Jan 2026 at 17:51, Fabiano Rosas <farosas@suse.de> wrote:
>> >> diff --git a/migration/options.c b/migration/options.c
>> >> index 9a5a39c886..9dc44a3736 100644
>> >> --- a/migration/options.c
>> >> +++ b/migration/options.c
>> >> @@ -225,6 +225,7 @@ static void get_StrOrNull(Object *obj, Visitor *v, const char *name,
>> >> str_or_null = g_new0(StrOrNull, 1);
>> >> str_or_null->type = QTYPE_QSTRING;
>> >> str_or_null->u.s = g_strdup(""); <== [1]
>> >> + *ptr = str_or_null;
>> >> } else {
>> >> /* the setter doesn't allow QNULL */
>> >> assert(str_or_null->type != QTYPE_QNULL);
>> >> @@ -245,6 +246,7 @@ static void set_StrOrNull(Object *obj, Visitor *v, const char *name,
>> >> */
>> >> str_or_null->type = QTYPE_QSTRING;
>> >
>> > * Do we need to add: str_or_null->u.s = g_strdup(""); here?
>>
>> It would leak, the visitor will allocate new memory for it below.
>
> * Then do we need to remove it from get_StrOrNull()? <== [1] above.
>
The visitor behaves differently depending on the direction
(input/output), the getter is providing a new object with a default
empty string.
>> I'd rather leave to the caller. The errp will be set, I think it's
>> enough. Looking at the other instances of visit_type_str in qdev, they
>> all simply return without any handling. I think in practice it's
>> unlikely that the string visitors will ever set errp because they are
>> just a g_strdup() usually.
>
> * Hmmn, okay. Coverity reported a leak for it in set_StrOrNull(), not
> other places?
>
Well, the setter is slightly different because this property is a
wrapper that contains a string, so the string visitor in the setter can
(in theory) fail and that would indeed leak (the outer object's
memory). But the other properties are returning the string directly,
without any extra memory allocation so, as in this getter, they simply
return on (the theoretical) failure.
IOW, there is nothing to leak in the other setters and the other
getters, on failure leave the caller to deal with the memory that was
already allocated.
Now, I just double-checked and what I said in the previous email is not
entirely accurate:
"I think in practice it's unlikely that the string visitors will ever
set errp because they are just a g_strdup() usually." -- me
the input visitor can actually set errp, but it does so before
allocating the string.
> Thank you.
> ---
> - Prasad
next prev parent reply other threads:[~2026-01-23 13:21 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-01-22 23:24 [PATCH] migration/options: Fix leaks in StrOrNull accessors Fabiano Rosas
2026-01-23 10:08 ` Prasad Pandit
2026-01-23 12:21 ` Fabiano Rosas
2026-01-23 12:37 ` Prasad Pandit
2026-01-23 13:15 ` Fabiano Rosas [this message]
2026-01-26 16:27 ` Peter Xu
2026-01-26 19:16 ` Fabiano Rosas
2026-01-26 19:57 ` Peter Xu
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=87v7gsii6k.fsf@suse.de \
--to=farosas@suse.de \
--cc=armbru@redhat.com \
--cc=peter.maydell@linaro.org \
--cc=peterx@redhat.com \
--cc=ppandit@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.