From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.lore.kernel.org (Postfix) with ESMTPS id D5097C10DC1 for ; Fri, 1 Dec 2023 06:20:51 +0000 (UTC) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r8wsU-0001H5-JB; Fri, 01 Dec 2023 01:19:54 -0500 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r8wsS-0001Gn-Of for qemu-devel@nongnu.org; Fri, 01 Dec 2023 01:19:52 -0500 Received: from us-smtp-delivery-124.mimecast.com ([170.10.133.124]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1r8wsR-0002Iw-34 for qemu-devel@nongnu.org; Fri, 01 Dec 2023 01:19:52 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1701411590; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=mYQ2H1t2DQCaWXThrNVEowMP9TGqDX6eLsujagQjOqE=; b=AZErTX23vcE5V43IG7C1KJkmdopZxQa0y3/3WJFCzZFF/cL5QUw1b3FURUz7KmEPjZ6pg9 bVzlJNYAxoUHC0aDV0kl5nc11m9/5kmixJA0mLQ+wuCA/M2/wiXzHRFr3dScorWJ/JK/9S 5+8Kfn56Fygdfmfeglf5zJRPX2qg0hc= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-558-A2hY2WxJMp-AGUmjoMKEaA-1; Fri, 01 Dec 2023 01:19:47 -0500 X-MC-Unique: A2hY2WxJMp-AGUmjoMKEaA-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 9CD54811000; Fri, 1 Dec 2023 06:19:46 +0000 (UTC) Received: from blackfin.pond.sub.org (unknown [10.39.192.148]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6C3B71C060B1; Fri, 1 Dec 2023 06:19:46 +0000 (UTC) Received: by blackfin.pond.sub.org (Postfix, from userid 1000) id 5C1AF21E6A1F; Fri, 1 Dec 2023 07:19:45 +0100 (CET) From: Markus Armbruster To: Peter Xu Cc: Het Gala , qemu-devel@nongnu.org, prerna.saxena@nutanix.com, quintela@redhat.com, berrange@redhat.com, peter.maydell@linaro.org, farosas@suse.de Subject: Re: [PATCH v4] migration: Plug memory leak with migration URIs References: <20231129204301.131228-1-het.gala@nutanix.com> <87il5j2io0.fsf@pond.sub.org> Date: Fri, 01 Dec 2023 07:19:45 +0100 In-Reply-To: (Peter Xu's message of "Thu, 30 Nov 2023 14:16:44 -0500") Message-ID: <87y1ee1m2m.fsf@pond.sub.org> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.2 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.7 Received-SPF: pass client-ip=170.10.133.124; envelope-from=armbru@redhat.com; helo=us-smtp-delivery-124.mimecast.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Peter Xu writes: > On Thu, Nov 30, 2023 at 07:35:43PM +0100, Markus Armbruster wrote: >> Peter Xu writes: >> >> > On Wed, Nov 29, 2023 at 08:43:01PM +0000, Het Gala wrote: >> >> migrate_uri_parse() allocates memory to 'channel' if the user >> >> opts for old syntax - uri, which is leaked because there is no >> >> code for freeing 'channel'. >> >> So, free channel to avoid memory leak in case where 'channels' >> >> is empty and uri parsing is required. >> >> >> >> Fixes: 5994024f ("migration: Implement MigrateChannelList to qmp migration flow") >> >> Signed-off-by: Het Gala >> >> Suggested-by: Markus Armbruster >> > >> > Reviewed-by: Peter Xu >> > >> >> @@ -533,18 +533,18 @@ static void qemu_start_incoming_migration(const char *uri, bool has_channels, - MigrationChannel *channel = NULL; + g_autoptr(MigrationChannel) channel = NULL; MigrationAddress *addr = NULL; MigrationIncomingState *mis = migration_incoming_get_current(); /* * Having preliminary checks for uri and channel */ if (uri && has_channels) { error_setg(errp, "'uri' and 'channels' arguments are mutually " "exclusive; exactly one of the two should be present in " "'migrate-incoming' qmp command "); return; } else if (channels) { /* To verify that Migrate channel list has only item */ if (channels->next) { >> >> error_setg(errp, "Channel list has more than one entries"); >> >> return; >> >> } >> >> - channel = channels->value; >> >> + addr = channels->value->addr; >> >> } else if (uri) { >> >> /* caller uses the old URI syntax */ >> >> if (!migrate_uri_parse(uri, &channel, errp)) { >> >> return; >> >> } >> >> + addr = channel->addr; >> >> } else { >> >> error_setg(errp, "neither 'uri' or 'channels' argument are " >> >> "specified in 'migrate-incoming' qmp command "); >> >> return; >> >> } >> >> - addr = channel->addr; >> > >> > Why these "addr" lines need change? Won't that behave the same as before? >> >> In the first case, @channel is now null. If we left the assignment to >> @addr alone, it would crash. Clearer now? > > Is it this one? > > if (uri && has_channels) { > error_setg(errp, "'uri' and 'channels' arguments are mutually " > "exclusive; exactly one of the two should be present in " > "'migrate-incoming' qmp command "); > return; > } > > It returns already? I meant the first visible case, i.e. if (channels). Sorry for being less than clear! The problem is to free the result of migrate_uri_parse(). The patch's solution is to use @channel *only* for holding that result, so it can be g_autoptr: drop channel = channels->value from the if (channels) conditional. Since this breaks addr = channel->addr, we move that assignment into the conditionals that reach it, which lets us unbreak it the if (channels) one.