From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mta1.formilux.org (mta1.formilux.org [51.159.59.229]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 5A4373A5443 for ; Sat, 4 Apr 2026 08:53:07 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=51.159.59.229 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775292789; cv=none; b=ZRi45+ZOwZE60dU+aotqECWE3exOVdxkf78g2/Ee2qRkwuASJ0aj7M/3uctqy/f5a5/cPVAvMhljaXKy73ASuW19/IIbpJ6hFCN/Am8TY4GZdNGALgjx4didtImNgF4KVm9Ltjp0Z2UZEA4eqBu8oqMKuM2QuFVSZi3AWf9BwEQ= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1775292789; c=relaxed/simple; bh=sfHgKN9rPhbktaQAo7ESqzmO5cSTKxMwMZg3m2GTj3U=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=LjOl3VlMPJloXdJXwSctPlthfnTyD+C0civsqGMBgYXCaNoge8FnLceH6B1xxa0qjuwmmq/V+m1Q36OgpQNlzhp7nGGO5fVeAnoGyQjkva9XkAJMf2BGI/jSZm0A5bP9Ihw5ERfzTbaj4w7m/ZY1Bqfy+mGp894yKsypmZ1WiJA= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=1wt.eu; spf=pass smtp.mailfrom=1wt.eu; dkim=pass (1024-bit key) header.d=1wt.eu header.i=@1wt.eu header.b=fmYIMr4x; arc=none smtp.client-ip=51.159.59.229 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=1wt.eu Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=1wt.eu Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=1wt.eu header.i=@1wt.eu header.b="fmYIMr4x" DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1wt.eu; s=mail; t=1775292785; bh=OybAnaworEM3BkSANhb1oI3vj/Gx+d7GvycnigCiw7I=; h=From:Message-ID:From; b=fmYIMr4xt0Ykoqh3UkYhxNVIFnF12V2aIYcp6+0qsD4r8k3cS0SsHa2uoebT4mHTD x5fiGdLGkF5lYiIrBCt237lF3v9UIFcRHSV6fi3eESuMdxYjM1FPnfvVstLVvyQ5a+ qJsRmtnf+gOIc4he/SpG07fOTKF7SnwC6swcibv0= Received: from 1wt.eu (ded1.1wt.eu [163.172.96.212]) by mta1.formilux.org (Postfix) with ESMTP id 5EA17C0F42; Sat, 04 Apr 2026 10:53:05 +0200 (CEST) Date: Sat, 4 Apr 2026 10:53:05 +0200 From: Willy Tarreau To: Thomas =?iso-8859-1?Q?Wei=DFschuh?= Cc: linux-kernel@vger.kernel.org Subject: Re: [PATCH 3/3] tools/nolibc: add support for asprintf() Message-ID: References: <20260401-nolibc-asprintf-v1-0-46292313439f@weissschuh.net> <20260401-nolibc-asprintf-v1-3-46292313439f@weissschuh.net> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit 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 I see how this can be convenient in test or init tools. Acked-by: Willy Tarreau 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