Message ID | 20200420134920.93473-1-chenzefeng2@huawei.com |
---|---|
State | Superseded |
Headers |
Return-Path: <chenzefeng2@huawei.com> X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from huawei.com (szxga07-in.huawei.com [45.249.212.35]) by sourceware.org (Postfix) with ESMTPS id 5BDC9384B104 for <libc-alpha@sourceware.org>; Mon, 20 Apr 2020 13:49:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 5BDC9384B104 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=huawei.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=chenzefeng2@huawei.com Received: from DGGEMS406-HUB.china.huawei.com (unknown [172.30.72.60]) by Forcepoint Email with ESMTP id 39A975A09B9731D9A058 for <libc-alpha@sourceware.org>; Mon, 20 Apr 2020 21:49:29 +0800 (CST) Received: from linux_supse12sp4_workstation.huawei.com (10.67.189.177) by DGGEMS406-HUB.china.huawei.com (10.3.19.206) with Microsoft SMTP Server id 14.3.487.0; Mon, 20 Apr 2020 21:49:20 +0800 From: chenzefeng <chenzefeng2@huawei.com> To: <libc-alpha@sourceware.org> CC: <liucheng32@huawei.com>, <zengweilin@huawei.com>, <fw@deneb.enyo.de>, <chenzefeng2@huawei.com> Subject: [PATCH v2] malloc: fix memleak in function muntrace Date: Mon, 20 Apr 2020 21:49:20 +0800 Message-ID: <20200420134920.93473-1-chenzefeng2@huawei.com> X-Mailer: git-send-email 2.12.3 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.67.189.177] X-CFilter-Loop: Reflected X-Spam-Status: No, score=-24.2 required=5.0 tests=BAYES_00, GIT_PATCH_0, GIT_PATCH_1, GIT_PATCH_2, GIT_PATCH_3, KAM_DMARC_STATUS, SPF_HELO_PASS, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list <libc-alpha.sourceware.org> List-Unsubscribe: <http://sourceware.org/mailman/options/libc-alpha>, <mailto:libc-alpha-request@sourceware.org?subject=unsubscribe> List-Archive: <https://sourceware.org/pipermail/libc-alpha/> List-Post: <mailto:libc-alpha@sourceware.org> List-Help: <mailto:libc-alpha-request@sourceware.org?subject=help> List-Subscribe: <http://sourceware.org/mailman/listinfo/libc-alpha>, <mailto:libc-alpha-request@sourceware.org?subject=subscribe> X-List-Received-Date: Mon, 20 Apr 2020 13:49:33 -0000 |
Series |
[v2] malloc: fix memleak in function muntrace
|
|
Commit Message
chenzefeng
April 20, 2020, 1:49 p.m. UTC
when we call functons as follow:
mtrace();
...
muntrace();
It would cause memleak, for the mtrace malloc some memory:
mtd = malloc(TRACE_BUFFER_SIZE);
and it would not be free. Therefor it should be freed in muntrace.
Signed-off-by: chenzefeng <chenzefeng2@huawei.com>
---
malloc/mtrace.c | 2 ++
1 file changed, 2 insertions(+)
Comments
* chenzefeng: > when we call functons as follow: > mtrace(); > ... > muntrace(); > It would cause memleak, for the mtrace malloc some memory: > mtd = malloc(TRACE_BUFFER_SIZE); > and it would not be free. Therefor it should be freed in muntrace. > > Signed-off-by: chenzefeng <chenzefeng2@huawei.com> glibc uses copyright assignment to the FSF for contributions, and not the DCO. I trust this contribution is covered by Huawei's copyright assignment? > --- > malloc/mtrace.c | 2 ++ > 1 file changed, 2 insertions(+) > > diff --git a/malloc/mtrace.c b/malloc/mtrace.c > index 7e7719df97..ab65ebe764 100644 > --- a/malloc/mtrace.c > +++ b/malloc/mtrace.c > @@ -365,4 +365,6 @@ muntrace (void) > > fprintf (f, "= End\n"); > fclose (f); > + if (malloc_trace_buffer != NULL) > + free (malloc_trace_buffer); > } I think you can call free unconditionally.
On Mon, Apr 20, 2020 at 10:50 AM Florian Weimer <fw@deneb.enyo.de> wrote: > * chenzefeng: > > + if (malloc_trace_buffer != NULL) > > + free (malloc_trace_buffer); > > } > > I think you can call free unconditionally. I was going to say the same thing. (free(NULL) is specified to do nothing.) Also, it would be good to set malloc_trace_buffer to NULL after calling free. Also, __release_libc_mem should be calling muntrace (after calling __libc_freeres). zw
* Zack Weinberg: > On Mon, Apr 20, 2020 at 10:50 AM Florian Weimer <fw@deneb.enyo.de> wrote: >> * chenzefeng: >> > + if (malloc_trace_buffer != NULL) >> > + free (malloc_trace_buffer); >> > } >> >> I think you can call free unconditionally. > > I was going to say the same thing. (free(NULL) is specified to do nothing.) > > Also, it would be good to set malloc_trace_buffer to NULL after > calling free. This introduces an asymmetry with the handling of f. The existing code is fine either way.
On Mon, Apr 20, 2020 at 12:19 PM Florian Weimer <fw@deneb.enyo.de> wrote: > * Zack Weinberg: > > On Mon, Apr 20, 2020 at 10:50 AM Florian Weimer <fw@deneb.enyo.de> wrote: > >> * chenzefeng: > >> > + if (malloc_trace_buffer != NULL) > >> > + free (malloc_trace_buffer); > >> > } > >> > >> I think you can call free unconditionally. > > > > I was going to say the same thing. (free(NULL) is specified to do nothing.) > > > > Also, it would be good to set malloc_trace_buffer to NULL after > > calling free. > > This introduces an asymmetry with the handling of f. f is a local variable. muntrace does set mallstream to NULL. zw
* Zack Weinberg: > On Mon, Apr 20, 2020 at 12:19 PM Florian Weimer <fw@deneb.enyo.de> wrote: >> * Zack Weinberg: >> > On Mon, Apr 20, 2020 at 10:50 AM Florian Weimer <fw@deneb.enyo.de> wrote: >> >> * chenzefeng: >> >> > + if (malloc_trace_buffer != NULL) >> >> > + free (malloc_trace_buffer); >> >> > } >> >> >> >> I think you can call free unconditionally. >> > >> > I was going to say the same thing. (free(NULL) is specified to do nothing.) >> > >> > Also, it would be good to set malloc_trace_buffer to NULL after >> > calling free. >> >> This introduces an asymmetry with the handling of f. > > f is a local variable. muntrace does set mallstream to NULL. Oh in this case, malloc_trace_buffer should be reset as well.
diff --git a/malloc/mtrace.c b/malloc/mtrace.c index 7e7719df97..ab65ebe764 100644 --- a/malloc/mtrace.c +++ b/malloc/mtrace.c @@ -365,4 +365,6 @@ muntrace (void) fprintf (f, "= End\n"); fclose (f); + if (malloc_trace_buffer != NULL) + free (malloc_trace_buffer); }