public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: "Thomas Weißschuh" <linux@weissschuh.net>
Cc: linux-kernel@vger.kernel.org
Subject: Re: [PATCH 3/3] tools/nolibc: add support for asprintf()
Date: Sat, 4 Apr 2026 10:53:05 +0200	[thread overview]
Message-ID: <adDRcZONUZfhVFov@1wt.eu> (raw)
In-Reply-To: <20260401-nolibc-asprintf-v1-3-46292313439f@weissschuh.net>

On Wed, Apr 01, 2026 at 05:07:29PM +0200, Thomas Weißschuh wrote:
> Add support for dynamically allocating formatted strings through
> asprintf() and vasprintf().
> 
> Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

I see how this can be convenient in test or init tools.

Acked-by: Willy Tarreau <w@1wt.eu>
Willy

> ---
>  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;
> +
> +	buf = malloc(len + 1);
> +	if (!buf)
> +		return -1;
> +
> +	len = vsnprintf(buf, len + 1, fmt, args2);
> +	if (len < 0) {
> +		free(buf);
> +		return -1;
> +	}
> +
> +	*strp = buf;
> +	return len;
> +}
> +
> +static __attribute__((unused, format(printf, 2, 0)))
> +int vasprintf(char **strp, const char *fmt, va_list args)
> +{
> +	va_list args2;
> +	int ret;
> +
> +	va_copy(args2, args);
> +	ret = __nolibc_vasprintf(strp, fmt, args, args2);
> +	va_end(args2);
> +
> +	return ret;
> +}
> +
> +static __attribute__((unused, format(printf, 2, 3)))
> +int asprintf(char **strp, const char *fmt, ...)
> +{
> +	va_list args;
> +	int ret;
> +
> +	va_start(args, fmt);
> +	ret = vasprintf(strp, fmt, args);
> +	va_end(args);
> +
> +	return ret;
> +}
> +
>  static __attribute__((unused))
>  int vsscanf(const char *str, const char *format, va_list args)
>  {
> diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
> index c888e13c6bd8..98070b805e49 100644
> --- a/tools/testing/selftests/nolibc/nolibc-test.c
> +++ b/tools/testing/selftests/nolibc/nolibc-test.c
> @@ -1859,6 +1859,29 @@ static int test_printf_error(void)
>  	return 0;
>  }
>  
> +int test_asprintf(void)
> +{
> +	char *str;
> +	int ret;
> +
> +	ret = asprintf(&str, "foo%s", "bar");
> +	if (ret == -1)
> +		return 1;
> +
> +	if (ret != 6) {
> +		free(str);
> +		return 2;
> +	}
> +
> +	if (memcmp(str, "foobar", 6) != 0) {
> +		free(str);
> +		return 3;
> +	}
> +
> +	free(str);
> +	return 0;
> +}
> +
>  static int run_printf(int min, int max)
>  {
>  	int test;
> @@ -1921,6 +1944,7 @@ static int run_printf(int min, int max)
>  		CASE_TEST(errno-neg);    errno = -22; EXPECT_VFPRINTF(is_nolibc, "errno=-22   ", "%-12m"); break;
>  		CASE_TEST(scanf);        EXPECT_ZR(1, test_scanf()); break;
>  		CASE_TEST(printf_error); EXPECT_ZR(1, test_printf_error()); break;
> +		CASE_TEST(asprintf);     EXPECT_ZR(1, test_asprintf()); break;
>  		case __LINE__:
>  			return ret; /* must be last */
>  		/* note: do not set any defaults so as to permit holes above */
> 
> -- 
> 2.53.0

  reply	other threads:[~2026-04-04  8:53 UTC|newest]

Thread overview: 10+ 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 [this message]
2026-04-04 15:34   ` David Laight
2026-04-05 15:39     ` Thomas Weißschuh

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=adDRcZONUZfhVFov@1wt.eu \
    --to=w@1wt.eu \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@weissschuh.net \
    /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