From patchwork Tue Mar 17 19:26:16 2026 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Alejandro Colomar X-Patchwork-Id: 131882 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from vm01.sourceware.org (localhost [127.0.0.1]) by sourceware.org (Postfix) with ESMTP id 7D52F4B9DB77 for ; Tue, 17 Mar 2026 19:27:42 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 7D52F4B9DB77 Authentication-Results: sourceware.org; dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20201202 header.b=at+4Txhw X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from sea.source.kernel.org (sea.source.kernel.org [172.234.252.31]) by sourceware.org (Postfix) with ESMTPS id 315694BBCDE5 for ; Tue, 17 Mar 2026 19:26:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 315694BBCDE5 Authentication-Results: sourceware.org; dmarc=pass (p=quarantine dis=none) header.from=kernel.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=kernel.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 315694BBCDE5 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=172.234.252.31 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1773775583; cv=none; b=o1k4GWRSt+eA1qCqaUvsQNKTSKj9Y4cVOyxtZ8/3ck3PqREWXogb7NKOgPVEZackPcbFYISGYSd7B5jnmuSEwwKPlv+lyFHrhs22o0NP10/LIdUHVSEHQIEAWd3HQkda418omI9yU7egrbiEYG3RJUB8Q1DOQtaZH/lTLhbqS1E= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1773775583; c=relaxed/simple; bh=xhf/1FlrTsgvXwTI+alFMTvi4JdGdthi0BrJps7FR8M=; h=DKIM-Signature:Date:From:To:Subject:Message-ID:MIME-Version; b=PJO1i0gl/LPmmMQNHOLTVZ9P4/1Kkei/v0AaVhHmhhoJa2kyI2zyutTRt4d5KcJDi+B/cCTIgt7uyVOCk37l7tWbdfO8gFa3YW9/N5T0nQ86YIKqhAMdU7UOaUkWfjPU0OxfgkRzCXwbb1Pr9LW9LUXF0QuQQ/fCoB4bVVFuel0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 315694BBCDE5 Received: from smtp.kernel.org (transwarp.subspace.kernel.org [100.75.92.58]) by sea.source.kernel.org (Postfix) with ESMTP id 2003A4165A; Tue, 17 Mar 2026 19:26:20 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id F37A5C4CEF7; Tue, 17 Mar 2026 19:26:18 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1773775580; bh=xhf/1FlrTsgvXwTI+alFMTvi4JdGdthi0BrJps7FR8M=; h=Date:From:To:Cc:Subject:References:In-Reply-To:From; b=at+4TxhwohQaWF/2D8AUG74cMY5AnpXGX40uJMCQXOIEvIb9l8xYCVT1Dr2Y1l9Ub 3kHc+VpOzQLECEqmTpo7iuChxjQ8ph2HprS+NAoJqkzhqGkmKl5u3g7VUQMw0REBvD 2KwIquCpumrPajQSTUrRBkqe4f2IvO1sBEa3KGMh9EbacQ1yiAd48IsGr2lTSTwwoJ 1Vkse8rb0By9tZPIaAm2+CHovU5eiAoL9IxsCUiZesRk6htGm95PllPZdh6IV+isGX l+65m1qfHBC/RSrKnaAO7TkyAXbjeeaPLH8uoyxaqetCN2PORw3wp9R4ua1qnS5n94 Y9T8KK6NIov1w== Date: Tue, 17 Mar 2026 20:26:16 +0100 From: Alejandro Colomar To: libc-alpha@sourceware.org, Joseph Myers , Paul Eggert Cc: Alejandro Colomar Subject: [RFC v2] Add [v]aprintf(3) Message-ID: <916855e5ad90ca3611a235ecbcf08a1359771eb9.1773775432.git.alx@kernel.org> X-Mailer: git-send-email 2.53.0 References: MIME-Version: 1.0 Content-Disposition: inline In-Reply-To: X-Spam-Status: No, score=-9.2 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_BLOCKED, RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED, SPF_HELO_NONE, SPF_PASS, TXREP, T_FILL_THIS_FORM_SHORT, URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: libc-alpha-bounces~patchwork=sourceware.org@sourceware.org Signed-off-by: Alejandro Colomar --- Hi! I've figured out how to add these to the Makefiles. I'm now having issues with the aliases, which I don't understand at all. I more or less imitated how [v]asprintf(3) are implemented, but obviously did something wrong. Would you mind helping with this error? In file included from : ./../include/libc-symbols.h:474:33: error: ‘__EI_aprintf’ aliased to undefined symbol ‘__GI_aprintf’ 474 | extern thread __typeof (name) __EI_##name \ | ^~~~~ ./../include/libc-symbols.h:470:3: note: in expansion of macro ‘__hidden_ver2’ 470 | __hidden_ver2 (, local, internal, name) | ^~~~~~~~~~~~~ ./../include/libc-symbols.h:478:41: note: in expansion of macro ‘__hidden_ver1’ 478 | # define hidden_def(name) __hidden_ver1(__GI_##name, name, name); | ^~~~~~~~~~~~~ ./../include/libc-symbols.h:559:32: note: in expansion of macro ‘hidden_def’ 559 | # define libc_hidden_def(name) hidden_def (name) | ^~~~~~~~~~ ../sysdeps/generic/math_ldbl_opt.h:13:38: note: in expansion of macro ‘libc_hidden_def’ 13 | #define ldbl_hidden_def(local, name) libc_hidden_def (name) | ^~~~~~~~~~~~~~~ aprintf.c:37:1: note: in expansion of macro ‘ldbl_hidden_def’ 37 | ldbl_hidden_def (__aprintf, aprintf) | ^~~~~~~~~~~~~~~ Cheers, Alex libio/Makefile | 3 ++- libio/stdio.h | 8 ++++++++ libio/vaprintf.c | 39 +++++++++++++++++++++++++++++++++++++++ stdio-common/Makefile | 2 ++ stdio-common/aprintf.c | 40 ++++++++++++++++++++++++++++++++++++++++ 5 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 libio/vaprintf.c create mode 100644 stdio-common/aprintf.c Range-diff against v1: 1: e5defcbd ! 1: 916855e5 libio/: Add [v]aprintf() @@ Metadata Author: Alejandro Colomar ## Commit message ## - libio/: Add [v]aprintf() + Add [v]aprintf(3) Signed-off-by: Alejandro Colomar + ## libio/Makefile ## +@@ libio/Makefile: routines := \ + \ + clearerr feof ferror fileno fputc freopen fseek getc getchar \ + memstream pclose putc putchar rewind setbuf setlinebuf vasprintf \ +- iovdprintf vscanf vsnprintf obprintf fcloseall fseeko ftello \ ++ vaprintf iovdprintf vscanf vsnprintf obprintf fcloseall fseeko ftello \ + freopen64 fseeko64 ftello64 \ + \ + __fbufsize __freading __fwriting __freadable __fwritable __flbf \ +@@ libio/Makefile: routines_no_fortify += \ + iovdprintf \ + swprintf \ + vasprintf \ ++ vaprintf \ + vsnprintf \ + vswprintf \ + vwprintf \ + ## libio/stdio.h ## @@ libio/stdio.h: extern int __asprintf (char **__restrict __ptr, extern int asprintf (char **__restrict __ptr, @@ libio/vaprintf.c (new) + This exception applies to code released by its copyright holders + in files containing the exception. */ + -+#include "libioP.h" ++#include +#include +#include +#include + -+char ** ++char * +__vaprintf (const char *fmt, va_list args) +{ + char *p; + -+ return (__vasprintf_internal (&p, fmt, args, 0) < 0 ? NULL : p; ++ return __vasprintf_internal (&p, fmt, args, 0) < 0 ? NULL : p; +} +ldbl_weak_alias (__vaprintf, vaprintf) + + ## stdio-common/Makefile ## +@@ stdio-common/Makefile: routines := \ + _itoa \ + _itowa \ + asprintf \ ++ aprintf \ + ctermid \ + cuserid \ + dprintf \ +@@ stdio-common/Makefile: routines := \ + # Exclude fortified routines from being built with _FORTIFY_SOURCE + routines_no_fortify += \ + asprintf \ ++ aprintf \ + dprintf \ + fprintf \ + printf \ + + ## stdio-common/aprintf.c (new) ## +@@ ++/* Copyright (C) 2026 Free Software Foundation, Inc. ++ This file is part of the GNU C Library. + -+char ** -+__aprintf (const char *fmt, ...) ++ 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 ++ . */ ++ ++#include ++#include ++ ++/* Write formatted output from FORMAT to a string which is ++ allocated with malloc. */ ++/* VARARGS1 */ ++char * ++___aprintf (const char *fmt, ...) +{ + char *p; + va_list ap; + -+ va_start(ap, fmt); -+ p = __vaprintf(fmt, ap); -+ va_end(ap); ++ va_start (ap, fmt); ++ if (__vasprintf_internal (&p, fmt, ap, 0) < 0) ++ p = NULL; ++ va_end (ap); + + return p; +} -+ldbl_weak_alias (__aprintf, aprintf) ++ldbl_hidden_def (__aprintf, aprintf) ++ ++ldbl_strong_alias (___aprintf, __aprintf) ++ldbl_weak_alias (___aprintf, aprintf) diff --git a/libio/Makefile b/libio/Makefile index 08e1e0ec..acb578db 100644 --- a/libio/Makefile +++ b/libio/Makefile @@ -43,7 +43,7 @@ routines := \ \ clearerr feof ferror fileno fputc freopen fseek getc getchar \ memstream pclose putc putchar rewind setbuf setlinebuf vasprintf \ - iovdprintf vscanf vsnprintf obprintf fcloseall fseeko ftello \ + vaprintf iovdprintf vscanf vsnprintf obprintf fcloseall fseeko ftello \ freopen64 fseeko64 ftello64 \ \ __fbufsize __freading __fwriting __freadable __fwritable __flbf \ @@ -63,6 +63,7 @@ routines_no_fortify += \ iovdprintf \ swprintf \ vasprintf \ + vaprintf \ vsnprintf \ vswprintf \ vwprintf \ 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..6789052f --- /dev/null +++ b/libio/vaprintf.c @@ -0,0 +1,39 @@ +/* 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 + . + + 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 +#include +#include +#include + +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) diff --git a/stdio-common/Makefile b/stdio-common/Makefile index 21094483..ec52c1ea 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -90,6 +90,7 @@ routines := \ _itoa \ _itowa \ asprintf \ + aprintf \ ctermid \ cuserid \ dprintf \ @@ -178,6 +179,7 @@ routines := \ # Exclude fortified routines from being built with _FORTIFY_SOURCE routines_no_fortify += \ asprintf \ + aprintf \ dprintf \ fprintf \ printf \ diff --git a/stdio-common/aprintf.c b/stdio-common/aprintf.c new file mode 100644 index 00000000..f2d0dbb2 --- /dev/null +++ b/stdio-common/aprintf.c @@ -0,0 +1,40 @@ +/* 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 + . */ + +#include +#include + +/* Write formatted output from FORMAT to a string which is + allocated with malloc. */ +/* VARARGS1 */ +char * +___aprintf (const char *fmt, ...) +{ + char *p; + va_list ap; + + va_start (ap, fmt); + if (__vasprintf_internal (&p, fmt, ap, 0) < 0) + p = NULL; + va_end (ap); + + return p; +} +ldbl_hidden_def (__aprintf, aprintf) + +ldbl_strong_alias (___aprintf, __aprintf) +ldbl_weak_alias (___aprintf, aprintf)