From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from venus.tony.develop-help.com (develop-help.com [220.233.67.40]) by sourceware.org (Postfix) with ESMTPS id 243BA385840C for ; Mon, 22 Nov 2021 23:23:04 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 243BA385840C Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=develop-help.com Authentication-Results: sourceware.org; spf=none smtp.mailfrom=develop-help.com Received: from venus.tony.develop-help.com (localhost [127.0.0.1]) by venus.tony.develop-help.com (8.15.2/8.15.2/Debian-14~deb10u1) with ESMTP id 1AMNN2O5005590 for ; Tue, 23 Nov 2021 10:23:02 +1100 Received: (from tony@localhost) by venus.tony.develop-help.com (8.15.2/8.15.2/Submit) id 1AMNN2tg005589 for cygwin@cygwin.com; Tue, 23 Nov 2021 10:23:02 +1100 Date: Tue, 23 Nov 2021 10:23:02 +1100 From: Tony Cook To: cygwin@cygwin.com Subject: Re: possible snprintf() regression in 3.3.2 Message-ID: <20211122232302.GI10332@venus.tony.develop-help.com> References: <20211118000649.GG10332@venus.tony.develop-help.com> <20211118203538.a049809d57731fe375801c15@nifty.ne.jp> <7545bb24-43de-cd7d-0764-55c85f1af957@gmx.com> <20211121001613.GH10332@venus.tony.develop-help.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=0.5 required=5.0 tests=BAYES_00, KAM_DMARC_STATUS, KAM_LAZY_DOMAIN_SECURITY, KAM_NUMSUBJECT, KHOP_HELO_FCRDNS, SPF_HELO_NONE, SPF_NONE, TXREP autolearn=no autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: cygwin@cygwin.com X-Mailman-Version: 2.1.29 Precedence: list List-Id: General Cygwin discussions and problem reports List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 22 Nov 2021 23:23:12 -0000 On Mon, Nov 22, 2021 at 02:04:06PM +0100, Corinna Vinschen via Cygwin wrote: > On Nov 22 11:34, Corinna Vinschen via Cygwin wrote: > > On Nov 21 11:16, Tony Cook wrote: > > > On Thu, Nov 18, 2021 at 09:08:40PM +0000, Sam Edge via Cygwin wrote: > > > > I use newlib on embedded with threading libs that have predetermined > > > > fixed thread stack sizes. While we tend to have more RAM than in former > > > > times we also have multiple thread stacks. Use of alloca() or variable > > > > length automatic arrays makes me wince especially in code I might not be > > > > able to avoid calling which is often the case with XXXprintf() in > > > > third-party libraries' debug output. I'd usually rather take the > > > > performance hit from using heap instead of having to make all my stacks > > > > bigger. > > > > > > A simple option would be to use an small auto fixed buffer for most > > > conversions, but use malloc() for %f formats for numbers greater in > > > magnitude than some limit, though it would also need to be adjusted > > > for the precision (ndigits here), since they take extra space. > > > > > > This would avoid using the optional-to-implement VLA feature too. > > > > Good idea. I guess I create a simple fix doing just that. > > I created a patch: > https://sourceware.org/git/?p=newlib-cygwin.git;a=commitdiff;h=68faeef4be71 > > Please test the latest developer snapshot from http://cygwin.com/snapshots/ I don't think this solves the fundamental problem. Simply looking at ndigits isn't enough for %f. For %f with a large number (like 9e99), the buffer size required is ndigits plus (roughly) log10(n), which we can further estimate with log2(n)*146/485 (log2(10) is 3.32 ~== 485/146) I think something more like: size_t outsize; if (mode == 3) { /* %f */ int expon = (e[NI-1] & 0x7fff) - (EXONE - 1); /* exponent part of float */ /* log2(10) approximately 485/146 */ outsize = expon * 146 / 485 + ndigits + 10; } else { /* %g/%e */ outsize = ndigits + MAX_EXP_DIGITS + 10; } if (outsize > NDEC_SML) { outbuf = (char *)_malloc_r(ptr, outsize); } You'll probably need to pass outsize into etoasc() rather than calculating it. See https://github.com/Perl/perl5/blob/blead/sv.c#L13295 for code in perl that calculates the buffer size needed for %f (precis aka ndigits is added at line 13385). Tony