delorie.com/archives/browse.cgi   search  
Mail Archives: cygwin/2021/11/23/03:34:58

X-Recipient: archive-cygwin AT delorie DOT com
DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org B5ADA385840E
DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=cygwin.com;
s=default; t=1637656496;
bh=hdnuCBDOnlvE5PXi02eXrSi0dVLS4FNUmLoLl9RViXA=;
h=Date:To:Subject:In-Reply-To:References:List-Id:List-Unsubscribe:
List-Archive:List-Post:List-Help:List-Subscribe:From:Reply-To:
From;
b=LHUtyxiFZ5a48uwvXSCdVImRMtgARbdfAC52OLq0YKmu6oWPe2Qwf+ONTx+sq2x8I
93hTBMWGZ9soLwlU3rj1y16XJd8wjcxDoB/8ar5aHtRsI1c4OtG1wFiz+eH/BC+a6+
aAopIPfZt+FoHsO3i6A2NHX+8Bl1xN3JRd+kLl00=
X-Original-To: cygwin AT cygwin DOT com
Delivered-To: cygwin AT cygwin DOT com
DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 1E6993858405
DKIM-Filter: OpenDKIM Filter v2.10.3 conssluserg-01.nifty.com 1AN8Y9uA010331
X-Nifty-SrcIP: [110.4.221.123]
Date: Tue, 23 Nov 2021 17:34:09 +0900
To: cygwin AT cygwin DOT com
Subject: Re: possible snprintf() regression in 3.3.2
Message-Id: <20211123173409.0db4d5ccd94501ce1b8f69ea@nifty.ne.jp>
In-Reply-To: <20211122232302.GI10332@venus.tony.develop-help.com>
References: <YZT1S8wDnaBuYf5u AT calimero DOT vinschen DOT de>
<20211118000649 DOT GG10332 AT venus DOT tony DOT develop-help DOT com>
<20211118203538 DOT a049809d57731fe375801c15 AT nifty DOT ne DOT jp>
<YZZSzPhbqF6cQAiu AT calimero DOT vinschen DOT de>
<fa647f83-c5ed-6dea-fcba-c8b799abe12f AT gmail DOT com>
<YZZi3rI5msk6ksAV AT calimero DOT vinschen DOT de>
<7545bb24-43de-cd7d-0764-55c85f1af957 AT gmx DOT com>
<20211121001613 DOT GH10332 AT venus DOT tony DOT develop-help DOT com>
<YZtyJDXdrMzVR2lJ AT calimero DOT vinschen DOT de>
<YZuVRjYoG40cEGDV AT calimero DOT vinschen DOT de>
<20211122232302 DOT GI10332 AT venus DOT tony DOT develop-help DOT com>
X-Mailer: Sylpheed 3.7.0 (GTK+ 2.24.30; i686-pc-mingw32)
Mime-Version: 1.0
X-Spam-Status: No, score=-5.8 required=5.0 tests=BAYES_00, DKIM_SIGNED,
DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, KAM_NUMSUBJECT, NICE_REPLY_A,
RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS,
TXREP autolearn=ham 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 AT cygwin DOT com
X-Mailman-Version: 2.1.29
List-Id: General Cygwin discussions and problem reports <cygwin.cygwin.com>
List-Unsubscribe: <https://cygwin.com/mailman/options/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=unsubscribe>
List-Archive: <https://cygwin.com/pipermail/cygwin/>
List-Post: <mailto:cygwin AT cygwin DOT com>
List-Help: <mailto:cygwin-request AT cygwin DOT com?subject=help>
List-Subscribe: <https://cygwin.com/mailman/listinfo/cygwin>,
<mailto:cygwin-request AT cygwin DOT com?subject=subscribe>
From: Takashi Yano via Cygwin <cygwin AT cygwin DOT com>
Reply-To: Takashi Yano <takashi DOT yano AT nifty DOT ne DOT jp>
Errors-To: cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com
Sender: "Cygwin" <cygwin-bounces+archive-cygwin=delorie DOT com AT cygwin DOT com>

On Tue, 23 Nov 2021 10:23:02 +1100
Tony Cook wrote:
> 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).

I guess Corinna thinks that 'ndigits' keeps the total number
of digits to be printed.

However, in reality, for example in the case:
snprintf(buf, sizeof(buf), "%.3f", 1234567890123456.789);
'ndigits' is only 3 even though total digits will be 20.

So, Tony thinks current code does not correct.

However, I think something is wrong with interpretation
of 'ndigits' in dltoa.c.

printf("%.3f\n", sqrt(2)*1e70);
printf("%.50f\n", sqrt(2)*1e70);

outputs

14142135623730951759073108307330633613786387000000000000000000000000000.000
14142135623730951759073108307330633613786386978891021459448717416650727.13402790000888758223149296720949629080194006476078

Is this as intended?

-- 
Takashi Yano <takashi DOT yano AT nifty DOT ne DOT jp>

-- 
Problem reports:      https://cygwin.com/problems.html
FAQ:                  https://cygwin.com/faq/
Documentation:        https://cygwin.com/docs.html
Unsubscribe info:     https://cygwin.com/ml/#unsubscribe-simple

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019