From owner-svn-src-projects@freebsd.org Sun Aug 9 17:03:12 2015 Return-Path: Delivered-To: svn-src-projects@mailman.ysv.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:1900:2254:206a::19:1]) by mailman.ysv.freebsd.org (Postfix) with ESMTP id 509F3998744 for ; Sun, 9 Aug 2015 17:03:12 +0000 (UTC) (envelope-from theraven@FreeBSD.org) Received: from theravensnest.org (theraven.freebsd.your.org [216.14.102.27]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (Client CN "cloud.theravensnest.org", Issuer "StartCom Class 1 Primary Intermediate Server CA" (not verified)) by mx1.freebsd.org (Postfix) with ESMTPS id F0BED835; Sun, 9 Aug 2015 17:03:11 +0000 (UTC) (envelope-from theraven@FreeBSD.org) Received: from [192.168.0.7] (cpc16-cmbg15-2-0-cust60.5-4.cable.virginm.net [86.5.162.61]) (authenticated bits=0) by theravensnest.org (8.15.1/8.15.1) with ESMTPSA id t79H2xXx034703 (version=TLSv1 cipher=ECDHE-RSA-AES256-SHA bits=256 verify=NO); Sun, 9 Aug 2015 17:03:03 GMT (envelope-from theraven@FreeBSD.org) X-Authentication-Warning: theravensnest.org: Host cpc16-cmbg15-2-0-cust60.5-4.cable.virginm.net [86.5.162.61] claimed to be [192.168.0.7] Content-Type: text/plain; charset=utf-8 Mime-Version: 1.0 (Mac OS X Mail 8.2 \(2102\)) Subject: Re: svn commit: r286521 - projects/collation/lib/libc/locale From: David Chisnall In-Reply-To: <20150809163935.GA96980@ivaldir.etoilebsd.net> Date: Sun, 9 Aug 2015 18:02:54 +0100 Cc: Bruce Evans , src-committers@freebsd.org, svn-src-projects@freebsd.org Content-Transfer-Encoding: quoted-printable Message-Id: References: <201508091150.t79Boo3v096088@repo.freebsd.org> <20150809223647.O2415@besplex.bde.org> <20150809163935.GA96980@ivaldir.etoilebsd.net> To: Baptiste Daroussin X-Mailer: Apple Mail (2.2102) X-BeenThere: svn-src-projects@freebsd.org X-Mailman-Version: 2.1.20 Precedence: list List-Id: "SVN commit messages for the src " projects" tree" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 09 Aug 2015 17:03:12 -0000 On 9 Aug 2015, at 17:39, Baptiste Daroussin wrote: >=20 > Well on the review David recommended to use asprintf rather than = snprintf. >=20 > While I dislike the option (as I stated in the review) given he wrote = most of > the recent changes (xlocale) I followed his recommendations. Having a large (1028 byte currently) buffer on the stack won=E2=80=99t = push you over the guard page if you run out of stack, but will mean that = if you=E2=80=99re near the end of the stack you=E2=80=99ll get a = segfault. And if there are bugs in the length calculation then we just = get a bigger allocation than expected, we don=E2=80=99t get a stack = buffer overflow. In both places, the buffer is only used in one place and so it should = only need freeing in one place, though it will require splitting the = next lines so that the open call is followed by the free and then the = check of the error return. We have this pattern in enough places in libc that it=E2=80=99s probably = worth pulling it out into a function something like this: int open_format(const char * restrict format, int oflag, ...) { char *path; int file; va_list ap; va_start(ap, format); vasprintf(&path, format, ap); va_end(ap); if (path =3D=3D NULL) return (NULL); file =3D open(path, oflag); free(path); return (file); } I think every single one of the locale lookup data loading functions = would benefit from using this instead of the existing code path. There = are a few other places in libc that seem to do this as well. As I said in the code review, when we=E2=80=99re doing a printf and some = file I/O, the cost of a short-lived heap allocation is likely to be in = the noise. David