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 X-Spam-Level: X-Spam-Status: No, score=-7.5 required=3.0 tests=BAYES_00,DKIM_ADSP_CUSTOM_MED, DKIM_INVALID,DKIM_SIGNED,FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM, HEADER_FROM_DIFFERENT_DOMAINS,HTML_MESSAGE,INCLUDES_CR_TRAILER,INCLUDES_PATCH, MAILING_LIST_MULTI,SPF_HELO_NONE,SPF_PASS autolearn=no autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 3174DC433B4 for ; Wed, 14 Apr 2021 07:33:57 +0000 (UTC) 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 mail.kernel.org (Postfix) with ESMTPS id 0CBD961166 for ; Wed, 14 Apr 2021 07:33:55 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mail.kernel.org 0CBD961166 Authentication-Results: mail.kernel.org; dmarc=fail (p=none dis=none) header.from=gmail.com Authentication-Results: mail.kernel.org; spf=pass smtp.mailfrom=qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Received: from localhost ([::1]:34170 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1lWa26-0001tJ-Ke for qemu-devel@archiver.kernel.org; Wed, 14 Apr 2021 03:33:54 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:41826) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1lWa1S-0001TC-BI for qemu-devel@nongnu.org; Wed, 14 Apr 2021 03:33:14 -0400 Received: from mail-ed1-x532.google.com ([2a00:1450:4864:20::532]:35836) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1lWa1Q-0001hY-IU for qemu-devel@nongnu.org; Wed, 14 Apr 2021 03:33:14 -0400 Received: by mail-ed1-x532.google.com with SMTP id x4so22490248edd.2 for ; Wed, 14 Apr 2021 00:33:11 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=N3IxtXXo1v/GGpL1EbxBq6LB3YJCEZlD2bAw2LUJVWI=; b=cpBJHpeMODzqm3LnbsGCNrlqjWTLCc0NhPzgaHHqvvL1pw9gjvpihgFUvZAQzsl+G9 /KeaghquRR/DL0bwFLm/yBriH5wPNuHtDR7hpkjc7dNXQ8BcBLaDSqvvTAycvYXgZEnp FfPcmRyQERDrrr8Lm6V3DFtLHvSHO9rff8ur+qc8E9o+7H/vP8p3LjIo11wbJAwwqbiy mK263j8KUFCJELeA690XjTOn28j4FLbKECwLJxb60Vf8ykYpgQdq8UL5aCj5xZdSDNnf M0Bnj7gdmwiG+oVcmLxX/JyO+8dd7B5thQQPjF/AVAqq+JedSPKFIMZw0PB3vp3RdPbD ZTKQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=N3IxtXXo1v/GGpL1EbxBq6LB3YJCEZlD2bAw2LUJVWI=; b=LY2ZdsUJ+yP4i94iPBhDiUEy0uXTddq2yA8j8VVH9appCxT9drmPAj6a2hOIXUe7VZ 544b401OFriIIHpo7PZRNtrvNf1CGp2WYOLcf6PTmRO9F0JSXeFAEisA7ed590gJWezO /itSjDQ57HV7xM9FHhBT32n24t+nG4Q1mfJroNx86gi9oXG27IqvQzgN0B39SZhdCBVQ fOd4KycMF+RqlUzrFyHNYu8MDUOJp/K5cUJbadtVzz9zwuPnUkqDcpqtkdN4hKWcCB54 tzpIrcctU+ZwgsUmACIpYZiPq0LYtiT45bsDbjdvC/C9HXI5NngkYLcyyZaRRQrr8peF U+Vw== X-Gm-Message-State: AOAM532j1LSsoDFBefLpRUb7Pfp82k92bNblnhtJeLPYP/aWL9ka2jhp tj0gXJwkNV4hprkMojUPDKkiOIzNLjNzwTPAKpY= X-Google-Smtp-Source: ABdhPJxlow0Y+HLXP1yTQwhB6GXdV51PVVbNwpq1F6R1TMYOQaBMGUK7j2/xB3VOt0k4/gtwflScxlmV5xBFxuzJX0E= X-Received: by 2002:aa7:da46:: with SMTP id w6mr40826487eds.30.1618385590476; Wed, 14 Apr 2021 00:33:10 -0700 (PDT) MIME-Version: 1.0 References: <20210412170255.231406-1-sgarzare@redhat.com> In-Reply-To: <20210412170255.231406-1-sgarzare@redhat.com> From: =?UTF-8?B?TWFyYy1BbmRyw6kgTHVyZWF1?= Date: Wed, 14 Apr 2021 11:32:58 +0400 Message-ID: Subject: Re: [PATCH] cutils: fix memory leak in get_relocated_path() To: Stefano Garzarella Content-Type: multipart/alternative; boundary="0000000000003ae40605bfe9c292" Received-SPF: pass client-ip=2a00:1450:4864:20::532; envelope-from=marcandre.lureau@gmail.com; helo=mail-ed1-x532.google.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, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, HTML_MESSAGE=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Paolo Bonzini , =?UTF-8?B?QWxleCBCZW5uw6ll?= , QEMU , =?UTF-8?Q?Philippe_Mathieu=2DDaud=C3=A9?= Errors-To: qemu-devel-bounces+qemu-devel=archiver.kernel.org@nongnu.org Sender: "Qemu-devel" --0000000000003ae40605bfe9c292 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable On Mon, Apr 12, 2021 at 9:06 PM Stefano Garzarella wrote: > get_relocated_path() allocates a GString object and returns the > character data (C string) to the caller without freeing the memory > allocated for that object as reported by valgrind: > > 24 bytes in 1 blocks are definitely lost in loss record 2,805 of 6,532 > at 0x4839809: malloc (vg_replace_malloc.c:307) > by 0x55AABB8: g_malloc (in /usr/lib64/libglib-2.0.so.0.6600.8) > by 0x55C2481: g_slice_alloc (in /usr/lib64/libglib-2.0.so.0.6600.8) > by 0x55C4827: g_string_sized_new (in > /usr/lib64/libglib-2.0.so.0.6600.8) > by 0x55C4CEA: g_string_new (in /usr/lib64/libglib-2.0.so.0.6600.8) > by 0x906314: get_relocated_path (cutils.c:1036) > by 0x6E1F77: qemu_read_default_config_file (vl.c:2122) > by 0x6E1F77: qemu_init (vl.c:2687) > by 0x3E3AF8: main (main.c:49) > > Let's use g_string_free(gstring, false) to free only the GString object > and transfer the ownership of the character data to the caller. > > Fixes: f4f5ed2cbd ("cutils: introduce get_relocated_path") > Signed-off-by: Stefano Garzarella > Reviewed-by: Marc-Andr=C3=A9 Lureau --- > util/cutils.c | 2 +- > 1 file changed, 1 insertion(+), 1 deletion(-) > > diff --git a/util/cutils.c b/util/cutils.c > index ee908486da..c9b91e7535 100644 > --- a/util/cutils.c > +++ b/util/cutils.c > @@ -1055,5 +1055,5 @@ char *get_relocated_path(const char *dir) > assert(G_IS_DIR_SEPARATOR(dir[-1])); > g_string_append(result, dir - 1); > } > - return result->str; > + return g_string_free(result, false); > } > -- > 2.30.2 > > > --=20 Marc-Andr=C3=A9 Lureau --0000000000003ae40605bfe9c292 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


=
On Mon, Apr 12, 2021 at 9:06 PM Stefa= no Garzarella <sgarzare@redhat.co= m> wrote:
get_relocated_path() allocates a GString object and returns the
character data (C string) to the caller without freeing the memory
allocated for that object as reported by valgrind:

=C2=A0 24 bytes in 1 blocks are definitely lost in loss record 2,805 of 6,5= 32
=C2=A0 =C2=A0 =C2=A0at 0x4839809: malloc (vg_replace_malloc.c:307)
=C2=A0 =C2=A0 =C2=A0by 0x55AABB8: g_malloc (in /usr/lib64/libglib-2.0.so.0.= 6600.8)
=C2=A0 =C2=A0 =C2=A0by 0x55C2481: g_slice_alloc (in /usr/lib64/libglib-2.0.= so.0.6600.8)
=C2=A0 =C2=A0 =C2=A0by 0x55C4827: g_string_sized_new (in /usr/lib64/libglib= -2.0.so.0.6600.8)
=C2=A0 =C2=A0 =C2=A0by 0x55C4CEA: g_string_new (in /usr/lib64/libglib-2.0.s= o.0.6600.8)
=C2=A0 =C2=A0 =C2=A0by 0x906314: get_relocated_path (cutils.c:1036)
=C2=A0 =C2=A0 =C2=A0by 0x6E1F77: qemu_read_default_config_file (vl.c:2122)<= br> =C2=A0 =C2=A0 =C2=A0by 0x6E1F77: qemu_init (vl.c:2687)
=C2=A0 =C2=A0 =C2=A0by 0x3E3AF8: main (main.c:49)

Let's use g_string_free(gstring, false) to free only the GString object=
and transfer the ownership of the character data to the caller.

Fixes: f4f5ed2cbd ("cutils: introduce get_relocated_path")
Signed-off-by: Stefano Garzarella <sgarzare@redhat.com>


Reviewed-by: Marc-Andr=C3=A9 Lureau <marcandre.lureau@redhat.com>=C2= =A0

---
=C2=A0util/cutils.c | 2 +-
=C2=A01 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/util/cutils.c b/util/cutils.c
index ee908486da..c9b91e7535 100644
--- a/util/cutils.c
+++ b/util/cutils.c
@@ -1055,5 +1055,5 @@ char *get_relocated_path(const char *dir)
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0assert(G_IS_DIR_SEPARATOR(dir[-1]));
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0g_string_append(result, dir - 1);
=C2=A0 =C2=A0 =C2=A0}
-=C2=A0 =C2=A0 return result->str;
+=C2=A0 =C2=A0 return g_string_free(result, false);
=C2=A0}
--
2.30.2




--
Marc-Andr=C3=A9 Lureau
--0000000000003ae40605bfe9c292--