From owner-svn-src-head@freebsd.org Fri Oct 2 18:35:56 2020 Return-Path: Delivered-To: svn-src-head@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id D75E5433D0D; Fri, 2 Oct 2020 18:35:56 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "Let's Encrypt Authority X3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4C2zG45L6Gz4W7h; Fri, 2 Oct 2020 18:35:56 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org (repo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:0]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id 8188B2676D; Fri, 2 Oct 2020 18:35:56 +0000 (UTC) (envelope-from markj@FreeBSD.org) Received: from repo.freebsd.org ([127.0.1.37]) by repo.freebsd.org (8.15.2/8.15.2) with ESMTP id 092IZuqN030498; Fri, 2 Oct 2020 18:35:56 GMT (envelope-from markj@FreeBSD.org) Received: (from markj@localhost) by repo.freebsd.org (8.15.2/8.15.2/Submit) id 092IZuhK030497; Fri, 2 Oct 2020 18:35:56 GMT (envelope-from markj@FreeBSD.org) Message-Id: <202010021835.092IZuhK030497@repo.freebsd.org> X-Authentication-Warning: repo.freebsd.org: markj set sender to markj@FreeBSD.org using -f From: Mark Johnston Date: Fri, 2 Oct 2020 18:35:56 +0000 (UTC) To: src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org Subject: svn commit: r366375 - head/lib/libc/locale X-SVN-Group: head X-SVN-Commit-Author: markj X-SVN-Commit-Paths: head/lib/libc/locale X-SVN-Commit-Revision: 366375 X-SVN-Commit-Repository: base MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-BeenThere: svn-src-head@freebsd.org X-Mailman-Version: 2.1.33 Precedence: list List-Id: SVN commit messages for the src tree for head/-current List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 02 Oct 2020 18:35:56 -0000 Author: markj Date: Fri Oct 2 18:35:55 2020 New Revision: 366375 URL: https://svnweb.freebsd.org/changeset/base/366375 Log: newlocale(3): Fix a memory leak. newlocale() optionally takes a "base" locale, from which components not specified in the mask are inherited. POSIX says that newlocale() may modify "base" and return it, or free "base" and return a newly allocated locale. We were not doing either, so applications which use newlocale() to modify an existing base locale end up leaking memory on FreeBSD. This diff fixes the leak by releasing a reference to the base locale before returning. This is less efficient than modifying "base" directly, but is simpler for an initial bug fix. Also, update the man page to clarify behaviour with respect to "base". PR: 249416 MFC after: 3 weeks Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D26522 Modified: head/lib/libc/locale/newlocale.3 head/lib/libc/locale/xlocale.c Modified: head/lib/libc/locale/newlocale.3 ============================================================================== --- head/lib/libc/locale/newlocale.3 Fri Oct 2 18:29:25 2020 (r366374) +++ head/lib/libc/locale/newlocale.3 Fri Oct 2 18:35:55 2020 (r366375) @@ -26,7 +26,7 @@ .\" SUCH DAMAGE. .\" .\" $FreeBSD$ -.Dd September 17, 2011 +.Dd October 2, 2020 .Dt NEWLOCALE 3 .Os .Sh NAME @@ -46,7 +46,20 @@ defines the components that the new locale will have s name specified in the .Fa locale parameter. -Any other components will be inherited from +Any components not specified in +.Fa mask +will be inherited from the locale referenced by +.Fa base , +if +.Fa base +is not +.Dv NULL . +If the call is successful, the state of the locale referenced by +.Fa base +is unspecified, and it must not be accessed. +The special locale +.Dv LC_GLOBAL_LOCALE +may not be specified for .Fa base . The .Fa mask Modified: head/lib/libc/locale/xlocale.c ============================================================================== --- head/lib/libc/locale/xlocale.c Fri Oct 2 18:29:25 2020 (r366374) +++ head/lib/libc/locale/xlocale.c Fri Oct 2 18:35:55 2020 (r366375) @@ -251,6 +251,7 @@ static int dupcomponent(int type, locale_t base, local locale_t newlocale(int mask, const char *locale, locale_t base) { + locale_t orig_base; int type; const char *realLocale = locale; int useenv = 0; @@ -263,6 +264,7 @@ locale_t newlocale(int mask, const char *locale, local return (NULL); } + orig_base = base; FIX_LOCALE(base); copyflags(new, base); @@ -297,6 +299,8 @@ locale_t newlocale(int mask, const char *locale, local if (0 == success) { xlocale_release(new); new = NULL; + } else if (base == orig_base) { + xlocale_release(base); } return (new);