All of lore.kernel.org
 help / color / mirror / Atom feed
From: David Laight <david.laight.linux@gmail.com>
To: "Thomas Weißschuh" <linux@weissschuh.net>
Cc: Willy Tarreau <w@1wt.eu>, linux-kernel@vger.kernel.org
Subject: Re: [PATCH 3/3] tools/nolibc: add support for asprintf()
Date: Tue, 7 Apr 2026 11:46:20 +0100	[thread overview]
Message-ID: <20260407114620.40f02441@pumpkin> (raw)
In-Reply-To: <4dba6ad1-b151-4234-98b1-9b7269768def@t-8ch.de>

On Sun, 5 Apr 2026 17:39:22 +0200
Thomas Weißschuh <linux@weissschuh.net> wrote:

> On 2026-04-04 16:34:59+0100, David Laight wrote:
> > On Wed, 01 Apr 2026 17:07:29 +0200
> > Thomas Weißschuh <linux@weissschuh.net> wrote:
> >   
> > > Add support for dynamically allocating formatted strings through
> > > asprintf() and vasprintf().
> > > 
> > > Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>
> > > ---
> > >  tools/include/nolibc/stdio.h                 | 50 ++++++++++++++++++++++++++++
> > >  tools/testing/selftests/nolibc/nolibc-test.c | 24 +++++++++++++
> > >  2 files changed, 74 insertions(+)
> > > 
> > > diff --git a/tools/include/nolibc/stdio.h b/tools/include/nolibc/stdio.h
> > > index 8f7e1948a651..1c9287b558f0 100644
> > > --- a/tools/include/nolibc/stdio.h
> > > +++ b/tools/include/nolibc/stdio.h
> > > @@ -787,6 +787,56 @@ int sprintf(char *buf, const char *fmt, ...)
> > >  	return ret;
> > >  }
> > >  
> > > +static __attribute__((unused, format(printf, 2, 0)))
> > > +int __nolibc_vasprintf(char **strp, const char *fmt, va_list args1, va_list args2)
> > > +{
> > > +	char *buf;
> > > +	int len;
> > > +
> > > +	len = vsnprintf(NULL, 0, fmt, args1);
> > > +	if (len < 0)
> > > +		return -1;  
> > 
> > vsnprintf() can never fail.  
> 
> The one in nolibc not, according to the specification it could.
> So to be on the safe side, also against future changes in nolibc
> I'd like to keep the check.
> The asnprintf()/malloc() implementation will make any performance
> considerations moot anyways.

While a -1 return value from vsnprintf() would indicate a failure,
there are no conditions where that can happen.

The man page (and I think posix/sus) has a common page with fprintf()
and the only error is:
       If an output error is encountered, a negative value is returned.
which can't happen for the 's' variants.

In particular there is nothing at all about invalid formats.
Perhaps just a comment that says it can't fail.

	David

      reply	other threads:[~2026-04-07 10:46 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-01 15:07 [PATCH 0/3] tools/nolibc: add support for asprintf() Thomas Weißschuh
2026-04-01 15:07 ` [PATCH 1/3] tools/nolibc: use __builtin_offsetof() Thomas Weißschuh
2026-04-04  8:34   ` Willy Tarreau
2026-04-04 15:29     ` David Laight
2026-04-01 15:07 ` [PATCH 2/3] selftests/nolibc: test the memory allocator Thomas Weißschuh
2026-04-04  8:51   ` Willy Tarreau
2026-04-01 15:07 ` [PATCH 3/3] tools/nolibc: add support for asprintf() Thomas Weißschuh
2026-04-04  8:53   ` Willy Tarreau
2026-04-04 15:34   ` David Laight
2026-04-05 15:39     ` Thomas Weißschuh
2026-04-07 10:46       ` David Laight [this message]

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=20260407114620.40f02441@pumpkin \
    --to=david.laight.linux@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@weissschuh.net \
    --cc=w@1wt.eu \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.