[RFC v1] libio/: Add [v]aprintf()
Adhemerval Zanella Netto
adhemerval.zanella@linaro.org
Tue Mar 17 19:26:06 GMT 2026
> Projects have come up with APIs for this over time. GNU and the
> BSDs have it as asprintf(3), but there seems to be consensus
> that this API isn't very well designed; evidence of this is that
> the behavior is slightly different in the various
> implementations, and has changes through history.
Could you add more details of what is wrong with the current asprintf glibc
implementation, besides the initial designed limitation ('int' return i
nstead of ssize_t)?
We don't have any bugs against asprintf on glibc bugzilla.
On 17/03/26 15:59, Alejandro Colomar wrote:
> Signed-off-by: Alejandro Colomar <alx@kernel.org>
> ---
>
> Hi Paul, Joseph,
>
> Since nobody replied, I guess people are not strongly opposed to it, but
> they probably want to see a patch before spending time with this.
>
> Thus, I've written a patch (or a draft of a patch).
>
> This is the first time I add a function to glibc, and I find it quite
> difficult, so I'll need some help. I've added the most obvious pieces:
> the prototypes and the implementation. However, this doesn't work at
> all, so I guess I'm missing stuff. Would you mind helping me figure out
> what I'm missing (or what's wrong)?
>
> I tried building this simple test program, but I get linker errors:
You need to export the symbol by adding a version tag and its name on
stdio-common/Versions.
I don't have a strong opinion about this, albeit since gnulib is already
providing and that committee has shown interest I would way for C standard
inclusion.
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int
> main(void)
> {
> char *p;
>
> p = aprintf("foo %d", 42);
> if (p == NULL)
> exit(1);
> puts(p);
> }
>
>
> Have a lovely night!
> Alex
>
>
> libio/stdio.h | 8 ++++++++
> libio/vaprintf.c | 53 ++++++++++++++++++++++++++++++++++++++++++++++++
> 2 files changed, 61 insertions(+)
> create mode 100644 libio/vaprintf.c
>
> diff --git a/libio/stdio.h b/libio/stdio.h
> index 3bf6a1f6..3db363f1 100644
> --- a/libio/stdio.h
> +++ b/libio/stdio.h
> @@ -410,6 +410,14 @@ extern int __asprintf (char **__restrict __ptr,
> extern int asprintf (char **__restrict __ptr,
> const char *__restrict __fmt, ...)
> __THROWNL __attribute__ ((__format__ (__printf__, 2, 3))) __wur;
> +
> +/* Write formatted output to a string dynamically allocated with `malloc'. */
> +extern char *vaprintf (const char *__restrict __f, __gnuc_va_list __arg)
> + __THROWNL __attribute__ ((__format__ (__printf__, 1, 0)))
> + __attribute_malloc__;
> +extern char *aprintf (const char *__restrict __fmt, ...)
> + __THROWNL __attribute__ ((__format__ (__printf__, 1, 2)))
> + __attribute_malloc__;
> #endif
>
> #ifdef __USE_XOPEN2K8
> diff --git a/libio/vaprintf.c b/libio/vaprintf.c
> new file mode 100644
> index 00000000..0ba147a0
> --- /dev/null
> +++ b/libio/vaprintf.c
> @@ -0,0 +1,53 @@
> +/* Copyright (C) 2026 Free Software Foundation, Inc.
> + This file is part of the GNU C Library.
> +
> + The GNU C Library is free software; you can redistribute it and/or
> + modify it under the terms of the GNU Lesser General Public
> + License as published by the Free Software Foundation; either
> + version 2.1 of the License, or (at your option) any later version.
> +
> + The GNU C Library is distributed in the hope that it will be useful,
> + but WITHOUT ANY WARRANTY; without even the implied warranty of
> + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
> + Lesser General Public License for more details.
> +
> + You should have received a copy of the GNU Lesser General Public
> + License along with the GNU C Library; if not, see
> + <https://www.gnu.org/licenses/>.
> +
> + As a special exception, if you link the code in this file with
> + files compiled with a GNU compiler to produce an executable,
> + that does not cause the resulting executable to be covered by
> + the GNU Lesser General Public License. This exception does not
> + however invalidate any other reasons why the executable file
> + might be covered by the GNU Lesser General Public License.
> + This exception applies to code released by its copyright holders
> + in files containing the exception. */
> +
> +#include "libioP.h"
> +#include <stdarg.h>
> +#include <stddef.h>
> +#include <stdio.h>
> +
> +char **
> +__vaprintf (const char *fmt, va_list args)
> +{
> + char *p;
> +
> + return (__vasprintf_internal (&p, fmt, args, 0) < 0 ? NULL : p;
> +}
> +ldbl_weak_alias (__vaprintf, vaprintf)
> +
> +char **
> +__aprintf (const char *fmt, ...)
> +{
> + char *p;
> + va_list ap;
> +
> + va_start(ap, fmt);
> + p = __vaprintf(fmt, ap);
> + va_end(ap);
> +
> + return p;
> +}
> +ldbl_weak_alias (__aprintf, aprintf)
>
> Range-diff against v0:
> -: -------- > 1: e5defcbd libio/: Add [v]aprintf()
More information about the Libc-alpha
mailing list