From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS31976 209.132.180.0/23 X-Spam-Status: No, score=-4.1 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id A397B1F461 for ; Mon, 20 May 2019 12:17:37 +0000 (UTC) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:mime-version :content-type; q=dns; s=default; b=DDlbJtSfnzoDAhun7I9CIPiXssI3u oYp2BRcjvOFTKOdYralByJ0B7DtjBpCA5M7dEyaE9gS0FGh4b/Tk0cp9I4b3jVuN zs9iGRtkA32Vy0uhdHvO5ZEDoe9G78YpdSh4KoC9DbLWQZJtfbRAPVkdKiGW2Jvt iMXSEfV/GFN3jo= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:from:to:subject:date:message-id:mime-version :content-type; s=default; bh=hsjefw08887oBhj5nTEp6J4k3uo=; b=eVX X0SoGwSKALYuE+8VXK4QGrVNb2rUHO0YgrHUYenN/HorMXNNGfpYmVTprW2+25Ey 02YOOHAO2w1LKs/pfk8grCExDcvIj5JssCpemMPWztR4xps65zGOkfX2ptxI2tVD 3hd0kBgMxyMSBGehlRtKQ2teNhvLq9xL4cSUxVo8= Received: (qmail 93890 invoked by alias); 20 May 2019 12:17:35 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 93882 invoked by uid 89); 20 May 2019 12:17:34 -0000 Authentication-Results: sourceware.org; auth=none X-HELO: mx1.redhat.com From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH] libio: Fix gconv-related memory leak [BZ #24583] Date: Mon, 20 May 2019 14:17:30 +0200 Message-ID: <87sgt9jp5x.fsf@oldenburg2.str.redhat.com> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.1 (gnu/linux) MIME-Version: 1.0 Content-Type: text/plain __gconv_close_transform performs locking internally, so remove the locking operations. 2019-05-20 Florian Weimer * libio/iofclose.c (_IO_new_fclose): Call __gconv_close_transform instead of __gconv_release_step. * libio/Makefile (tests): Add tst-wfile-gconv. (tst-wfile-gconv-ENV): Enable mtrace. (generated): Add tst-wfile-gconv.mtrace, tst-wfile-gconv.check. (tests-special): Add tst-wfile-gconv-mem.out. (tst-wfile-gconv.out): Depend on locales. (tst-wfile-gconv-mem.out): Add mtrace rule. * libio/tst-wfile-gconv.c: New file. diff --git a/libio/Makefile b/libio/Makefile index a5236c7042..641f4f00af 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -66,7 +66,7 @@ tests = tst_swprintf tst_wprintf tst_swscanf tst_wscanf tst_getwc tst_putwc \ tst-fwrite-error tst-ftell-partial-wide tst-ftell-active-handler \ tst-ftell-append tst-fputws tst-bz22415 tst-fgetc-after-eof \ tst-sprintf-ub tst-sprintf-chk-ub tst-bz24051 tst-bz24153 \ - tst-wfile-sync + tst-wfile-sync tst-wfile-gconv tests-internal = tst-vtables tst-vtables-interposed tst-readline @@ -163,10 +163,12 @@ tst_wprintf2-ARGS = "Some Text" test-fmemopen-ENV = MALLOC_TRACE=$(objpfx)test-fmemopen.mtrace tst-fopenloc-ENV = MALLOC_TRACE=$(objpfx)tst-fopenloc.mtrace tst-bz22415-ENV = MALLOC_TRACE=$(objpfx)tst-bz22415.mtrace +tst-wfile-gconv-ENV = MALLOC_TRACE=$(objpfx)tst-wfile-gconv.mtrace generated += test-fmemopen.mtrace test-fmemopen.check generated += tst-fopenloc.mtrace tst-fopenloc.check generated += tst-bz22415.mtrace tst-bz22415.check +generated += tst-wfile-gconv.mtrace tst-wfile-gconv.check aux := fileops genops stdfiles stdio strops @@ -181,7 +183,7 @@ shared-only-routines = oldiofopen oldiofdopen oldiofclose oldfileops \ ifeq ($(run-built-tests),yes) tests-special += $(objpfx)test-freopen.out $(objpfx)test-fmemopen-mem.out \ - $(objpfx)tst-bz22415-mem.out + $(objpfx)tst-bz22415-mem.out $(objpfx)tst-wfile-gconv-mem.out ifeq (yes,$(build-shared)) # Run tst-fopenloc-cmp.out and tst-openloc-mem.out only if shared # library is enabled since they depend on tst-fopenloc.out. @@ -214,6 +216,7 @@ $(objpfx)tst-ungetwc2.out: $(gen-locales) $(objpfx)tst-widetext.out: $(gen-locales) $(objpfx)tst_wprintf2.out: $(gen-locales) $(objpfx)tst-wfile-sync.out: $(gen-locales) +$(objpfx)tst-wfile-gconv.out: $(gen-locales) endif $(objpfx)test-freopen.out: test-freopen.sh $(objpfx)test-freopen @@ -237,3 +240,7 @@ $(objpfx)tst-fopenloc-mem.out: $(objpfx)tst-fopenloc.out $(objpfx)tst-bz22415-mem.out: $(objpfx)tst-bz22415.out $(common-objpfx)malloc/mtrace $(objpfx)tst-bz22415.mtrace > $@; \ $(evaluate-test) + +$(objpfx)tst-wfile-gconv-mem.out: $(objpfx)tst-wfile-gconv.out + $(common-objpfx)malloc/mtrace $(objpfx)tst-wfile-gconv.mtrace > $@; \ + $(evaluate-test) diff --git a/libio/iofclose.c b/libio/iofclose.c index 8a80dd0b78..f4c7299db3 100644 --- a/libio/iofclose.c +++ b/libio/iofclose.c @@ -61,10 +61,10 @@ _IO_new_fclose (FILE *fp) the conversion functions. */ struct _IO_codecvt *cc = fp->_codecvt; - __libc_lock_lock (__gconv_lock); - __gconv_release_step (cc->__cd_in.__cd.__steps); - __gconv_release_step (cc->__cd_out.__cd.__steps); - __libc_lock_unlock (__gconv_lock); + __gconv_close_transform (cc->__cd_in.__cd.__steps, + cc->__cd_in.__cd.__nsteps); + __gconv_close_transform (cc->__cd_out.__cd.__steps, + cc->__cd_out.__cd.__nsteps); } else { diff --git a/libio/tst-wfile-gconv.c b/libio/tst-wfile-gconv.c new file mode 100644 index 0000000000..de603b32d2 --- /dev/null +++ b/libio/tst-wfile-gconv.c @@ -0,0 +1,36 @@ +/* Test that non-built-in gconv modules do not cause memory leak (bug 24583). + Copyright (C) 2019 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General Public + License along with the GNU C Library; if not, see + . */ + +#include +#include +#include +#include + +static int +do_test (void) +{ + mtrace (); + + TEST_VERIFY_EXIT (setlocale (LC_ALL, "ja_JP.EUC-JP") != NULL); + xfclose (xfopen ("/etc/passwd", "r,ccs=UTF-8")); + xfclose (xfopen ("/etc/passwd", "r")); + + return 0; +} + +#include