From: Tony Cook <tony@develop-help.com>
To: cygwin@cygwin.com
Subject: Re: possible snprintf() regression in 3.3.2
Date: Tue, 23 Nov 2021 10:23:02 +1100 [thread overview]
Message-ID: <20211122232302.GI10332@venus.tony.develop-help.com> (raw)
In-Reply-To: <YZuVRjYoG40cEGDV@calimero.vinschen.de>
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
next prev parent reply other threads:[~2021-11-22 23:23 UTC|newest]
Thread overview: 23+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-11-17 0:37 Tony Cook
2021-11-17 9:21 ` Takashi Yano
2021-11-17 12:27 ` Corinna Vinschen
2021-11-18 0:06 ` Tony Cook
2021-11-18 11:35 ` Takashi Yano
2021-11-18 13:19 ` Corinna Vinschen
2021-11-18 14:11 ` Noel Grandin
2021-11-18 14:27 ` Corinna Vinschen
2021-11-18 21:08 ` Sam Edge
2021-11-21 0:16 ` Tony Cook
2021-11-22 10:34 ` Corinna Vinschen
2021-11-22 13:04 ` Corinna Vinschen
2021-11-22 23:23 ` Tony Cook [this message]
2021-11-23 8:34 ` Takashi Yano
2021-11-23 9:48 ` Corinna Vinschen
2021-11-24 3:40 ` Takashi Yano
2021-11-24 8:48 ` Corinna Vinschen
2021-11-24 8:52 ` Takashi Yano
2021-11-24 9:14 ` Takashi Yano
2021-11-24 9:28 ` Corinna Vinschen
2021-11-24 12:29 ` Lemke, Michael SF/HZA-ZI2E
2021-11-25 12:02 ` Takashi Yano
2021-11-25 12:45 ` Corinna Vinschen
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20211122232302.GI10332@venus.tony.develop-help.com \
--to=tony@develop-help.com \
--cc=cygwin@cygwin.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).