From: Markus Armbruster <armbru@redhat.com>
To: Steven Sistare <steven.sistare@oracle.com>
Cc: qemu-devel@nongnu.org, pbonzini@redhat.com,
berrange@redhat.com, eduardo@habkost.net
Subject: Re: [PATCH 1/5] qtest/qom-test: Plug memory leak with -p
Date: Tue, 05 Aug 2025 08:54:44 +0200 [thread overview]
Message-ID: <87h5ymdzrf.fsf@pond.sub.org> (raw)
In-Reply-To: <a7a3ba56-680a-4256-b6e1-ff78f131fdf0@oracle.com> (Steven Sistare's message of "Mon, 4 Aug 2025 13:11:04 -0400")
Steven Sistare <steven.sistare@oracle.com> writes:
> On 7/25/2025 9:50 AM, Markus Armbruster wrote:
>> The machine name g_strdup()ed by add_machine_test_case() is freed by
>> test_machine(). Since the former runs for all machines, whereas the
>> latter runs only for the selected test case's machines, this leaks the
>> names of machines not selected, if any. Harmless, but fix it anyway:
>> there is no need to dup in the first place, so don't.
>>
>> Signed-off-by: Markus Armbruster <armbru@redhat.com>
>> ---
>> tests/qtest/qom-test.c | 3 +--
>> 1 file changed, 1 insertion(+), 2 deletions(-)
>>
>> diff --git a/tests/qtest/qom-test.c b/tests/qtest/qom-test.c
>> index 4ade1c728c..cb5dbfe329 100644
>> --- a/tests/qtest/qom-test.c
>> +++ b/tests/qtest/qom-test.c
>> @@ -220,7 +220,6 @@ static void test_machine(gconstpointer data)
>> qobject_unref(response);
>> qtest_quit(qts);
>> - g_free((void *)machine);
>> }
>>
>> static void add_machine_test_case(const char *mname)
>> @@ -228,7 +227,7 @@ static void add_machine_test_case(const char *mname)
>> char *path;
>> path = g_strdup_printf("qom/%s", mname);
>> - qtest_add_data_func(path, g_strdup(mname), test_machine);
>> + qtest_add_data_func(path, mname, test_machine);
>> g_free(path);
>> }
>
> This will break if qtest_cb_for_every_machine ever composes a machine name on the
> stack and passes that to add_machine_test_case. Unlikely, but the strdup makes it
> future proof.
Hmm.
qtest obtains machine names via QMP on demand. This is
qtest_get_machines(). Once gotten, they live forever.
Used to live forever, actually: commit 41b2eba4e5c (tests/qtest: Allow
qtest_get_machines to use an alternate QEMU binary) throws them away
when qtest_get_machines() is asked for another QEMU's machine names.
migrate_start() does that. It appears get each one's machine names
twice, because it switches back and forth. Wasteful.
Anyway, you have a point: more stupid shit happens below the hood than I
expected, and even more may be added in the future.
Moreover, at least test-hmp has the same leak. Plugging it here and not
there makes no sense. I'm dropping this patch for now.
> Also, mname is not the only leak. path is also leaked when only a subset of
> tests are run:
>
> qtest_add_data_func(path, ...)
> gchar *path = g_strdup_printf(...)
> g_test_add_data_func(path, ...)
>
> Leaking seems to be par for this course. Maybe not worth fixing.
valgrind shows the machine name leak my patch plugs. It does not show
@path leaking.
Let's have a closer look:
static void add_machine_test_case(const char *mname)
{
char *path;
path = g_strdup_printf("qom/%s", mname);
qtest_add_data_func(path, mname, test_machine);
g_free(path);
}
Always frees @path.
void qtest_add_data_func(const char *str, const void *data,
void (*fn)(const void *))
{
gchar *path = g_strdup_printf("/%s/%s", qtest_get_arch(), str);
g_test_add_data_func(path, data, fn);
g_free(path);
}
Always frees @path.
g_test_add_data_func()'s contract[*] on its first argument: "The data is
owned by the caller of the function."
I can't see a leak.
[*] https://docs.gtk.org/glib/func.test_add_data_func.html
next prev parent reply other threads:[~2025-08-05 6:56 UTC|newest]
Thread overview: 15+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-07-25 13:50 [PATCH 0/5] qtest/qom-test: Leak plug, faster, better coverage Markus Armbruster
2025-07-25 13:50 ` [PATCH 1/5] qtest/qom-test: Plug memory leak with -p Markus Armbruster
2025-08-04 17:11 ` Steven Sistare
2025-08-05 6:54 ` Markus Armbruster [this message]
2025-08-05 12:07 ` Steven Sistare
2025-07-25 13:50 ` [PATCH 2/5] qtest/qom-test: Shallow testing of qom-list / qom-get Markus Armbruster
2025-08-04 16:38 ` Steven Sistare
2025-07-25 13:50 ` [PATCH 3/5] qtest/qom-test: Traverse entire QOM tree Markus Armbruster
2025-08-04 16:40 ` Steven Sistare
2025-07-25 13:50 ` [PATCH 4/5] qtest/qom-test: Don't bother to execute QMP command quit Markus Armbruster
2025-08-04 17:10 ` Steven Sistare
2025-08-05 6:06 ` Markus Armbruster
2025-08-05 12:03 ` Steven Sistare
2025-07-25 13:50 ` [PATCH 5/5] MAINTAINERS: Cover tests/qtest/qom-test.c Markus Armbruster
2025-08-04 7:01 ` [PATCH 0/5] qtest/qom-test: Leak plug, faster, better coverage 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=87h5ymdzrf.fsf@pond.sub.org \
--to=armbru@redhat.com \
--cc=berrange@redhat.com \
--cc=eduardo@habkost.net \
--cc=pbonzini@redhat.com \
--cc=qemu-devel@nongnu.org \
--cc=steven.sistare@oracle.com \
/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.