From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x633.google.com (mail-pl1-x633.google.com [IPv6:2607:f8b0:4864:20::633]) by sourceware.org (Postfix) with ESMTPS id B1B17385829E for ; Fri, 16 Feb 2024 14:12:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B1B17385829E Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=linaro.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linaro.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org B1B17385829E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2607:f8b0:4864:20::633 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708092744; cv=none; b=fIf0mOvEU9mPUk/zE3WpkA5OQJA8bsRv4SRG4noSF9/Vhz4hwEfTZOiO3R9ooZY3axkNq7hVYN37I99//ZLGgmt0ZQ4bPRWA5d7fAT/CIlDZtquyUcuSJC6Ky73WY4QvlFUDAb3qyggCE3Onfh6sNR+An4Kzqt9W7T7qgcv5DXQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1708092744; c=relaxed/simple; bh=8wMDdn7rI7Rk6VUkes/wukEGXMoUcZRoXDF2uhGZasw=; h=DKIM-Signature:Message-ID:Date:MIME-Version:Subject:To:From; b=ln5IQ+T/C1J+Obefrt0I4TOm+Agqkrh1cPH+3IsRCTxk0q4PusRTegzAdLltFpipCatcjcksK6hWvLw2h0Ej8ejSQJP1P8QBLBFjdMrU+ZYbFalCGGxC7zPUkfIlfcYlf4XLkquLrUXC+T+t0+ZuKW6tcyiPYDHTmtBLbwJGlls= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-pl1-x633.google.com with SMTP id d9443c01a7336-1db562438e0so18539765ad.3 for ; Fri, 16 Feb 2024 06:12:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linaro.org; s=google; t=1708092740; x=1708697540; darn=sourceware.org; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=8g+LF+RQkq1OB3CwI+YPGZKVuNO/WT1kIGRBxL1uHMs=; b=w8PXOiqpt14w9a3voSV6EJvPwt2+MbplUrUTKc/eaeS/ONUAICXPJzXxOGXCN5MqwM lA/FQTgDPu8uX4E5L+CwNMLuW/iPbyIGG3PlCLx0NvT5CMz8w/qYCbuK/Vb1T3KNmL0T v3PdGqchkABFBAC580/nNtmNjEXX085UF1+mqKgJF45teTvz0OM4YwwBLsyKeAsatMtf lgIsE4BtJX2IX6e9O/y+Kjgs9qZgxYQu7zOjvr+T4PrGatO7fUKvYmtyiwac/9QYebGv iW3j666wsxHmrVgHF4puwMZ2LbARflu66ouxfjjCpK6ucrety1JY0QMVno36hgp+hhr2 3C1Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1708092740; x=1708697540; h=content-transfer-encoding:in-reply-to:organization:from:references :to:content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=8g+LF+RQkq1OB3CwI+YPGZKVuNO/WT1kIGRBxL1uHMs=; b=ljL2D2ErXkFvHt5Ar6EGssb1tuuyXCPNpreFNIlCo4feZ5mJ/L5rFXhwEsSL83nuq3 Qn1DYcKbTfxEH6VIck7ZAL87xz0Xk3CoJEvNjHGD+9+evUoYJ8hPuJQv8EW49THQhRhd FbWHiQ3uHD8SmZY6U4A91cU9rTdrqPXw+taIzzSZXEkAKuDFVbfVodsGIkKC2pUtGe5d J76vgfk/7GEH8g5k7Xuba8/js7LibtYlxQkrorn/7wd+kkP5a5Au2df4tlg8Y1opcYLA AIs+dKZQ21KOKHGI+5THGuuJJyDR0cQLanVFaVsWrRZhZlEiad1W8UTiyiTTYTrOJ2F5 sdRA== X-Forwarded-Encrypted: i=1; AJvYcCXA8AIB2bvvusc52awEbI8VjJ7J/FMgFKrFlC2hNIUa6lIc8n6ErKJ87H7GPlyPPYnyPmKLAdKFGpWEWgW05nDBYMc2qZm522qW X-Gm-Message-State: AOJu0Ywd2TzPITNdkKCs24eJCm9D6tJBBWrrjzv5wAANezExDNtpdpQb 6pTxNI4LQT7pMssOzwXljQxRqQec7yldVDJxV1+0O4uxOGwoLTMf9DDYPl569UQ= X-Google-Smtp-Source: AGHT+IFjCI3v574TJedkvV2H1JZpKvVM1EwvfU7QGoMsQQ+W40UNugzY1VcYLKUtL65OSZEiXUmbHQ== X-Received: by 2002:a17:902:b902:b0:1d9:8ddf:5fa0 with SMTP id bf2-20020a170902b90200b001d98ddf5fa0mr4700769plb.62.1708092740553; Fri, 16 Feb 2024 06:12:20 -0800 (PST) Received: from ?IPV6:2804:1b3:a7c0:8177:542a:9f26:15a9:5ce4? ([2804:1b3:a7c0:8177:542a:9f26:15a9:5ce4]) by smtp.gmail.com with ESMTPSA id w17-20020a1709027b9100b001da15580ca8sm3039585pll.52.2024.02.16.06.12.19 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 16 Feb 2024 06:12:20 -0800 (PST) Message-ID: Date: Fri, 16 Feb 2024 11:12:18 -0300 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Subject: Re: [PATCH 08/11] stdio-common: Introduce the __printf_buffer function Content-Language: en-US To: Florian Weimer , libc-alpha@sourceware.org References: From: Adhemerval Zanella Netto Organization: Linaro In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,KAM_SHORT,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 09/02/24 12:25, Florian Weimer wrote: > It can be used to write to a __print_buffer (incrementally). LGTM, thanks. Reviewed-by: Adhemerval Zanella > --- > include/printf_buffer.h | 5 ++ > stdio-common/Makefile | 5 ++ > stdio-common/printf_buffer.c | 30 +++++++ > stdio-common/tst-printf_buffer.c | 138 +++++++++++++++++++++++++++++++ > 4 files changed, 178 insertions(+) > create mode 100644 stdio-common/printf_buffer.c > create mode 100644 stdio-common/tst-printf_buffer.c > > diff --git a/include/printf_buffer.h b/include/printf_buffer.h > index cee5afb581..a5483e8a97 100644 > --- a/include/printf_buffer.h > +++ b/include/printf_buffer.h > @@ -206,6 +206,11 @@ bool __printf_buffer_flush (struct __printf_buffer *buf) attribute_hidden; > void __vprintf_buffer (struct __printf_buffer *buf, const char *format, > va_list ap, unsigned int mode_flags) attribute_hidden; > > +/* Write the argument list to BUF according to FORMAT. > + Unconditionally enables fortification (special procesing for %n). */ > +void __printf_buffer (struct __printf_buffer *buf, const char *format, ...) > + __attribute__ ((__format__ (__printf__, 2, 3))) attribute_hidden; > + > /* Wide version of struct __printf_buffer follows. */ > > enum __wprintf_buffer_mode Ok. > diff --git a/stdio-common/Makefile b/stdio-common/Makefile > index 6447b6b444..d610ed67e6 100644 > --- a/stdio-common/Makefile > +++ b/stdio-common/Makefile > @@ -64,6 +64,7 @@ routines := \ > perror \ > printf \ > printf-prs \ > + printf_buffer \ > printf_buffer_as_file \ > printf_buffer_done \ > printf_buffer_flush \ > @@ -290,6 +291,10 @@ test-srcs = \ > tst-unbputc \ > # test-srcs > > +# Test for the internal, non-exported __printf_buffer function. > +tests-internal += tst-printf_buffer > +tests-static += tst-printf_buffer > + Maybe put in a newline to be future-proof. > ifeq ($(run-built-tests),yes) > tests-special += \ > $(objpfx)tst-printf-bz18872-mem.out \ > diff --git a/stdio-common/printf_buffer.c b/stdio-common/printf_buffer.c > new file mode 100644 > index 0000000000..fabb3b5bb4 > --- /dev/null > +++ b/stdio-common/printf_buffer.c > @@ -0,0 +1,30 @@ > +/* Write a format string to a buffer. > + Copyright (C) 2024 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 > +#include > + > +void > +__printf_buffer (struct __printf_buffer *buf, const char *format, ...) > +{ > + va_list arg; > + va_start (arg, format); > + __vprintf_buffer (buf, format, arg, PRINTF_FORTIFY); > + va_end (arg); > +} > diff --git a/stdio-common/tst-printf_buffer.c b/stdio-common/tst-printf_buffer.c > new file mode 100644 > index 0000000000..d12da8c939 > --- /dev/null > +++ b/stdio-common/tst-printf_buffer.c > @@ -0,0 +1,138 @@ > +/* Basic test for the __printf_buffer function (via asprintf). > + Copyright (C) 2024 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 > +#include > +#include > + > +static int > +do_test (void) > +{ > + { > + struct __printf_buffer_asprintf buf; > + __printf_buffer_asprintf_init (&buf); > + TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); > + TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); > + } > + > + { > + struct __printf_buffer_asprintf buf; > + __printf_buffer_asprintf_init (&buf); > + __printf_buffer (&buf.base, "%s", ""); > + TEST_VERIFY (buf.base.write_base == buf.direct); > + TEST_VERIFY (buf.base.write_ptr == buf.base.write_base); > + TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); > + TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); > + } > + > + { > + struct __printf_buffer_asprintf buf; > + __printf_buffer_asprintf_init (&buf); > + __printf_buffer (&buf.base, "abc"); > + TEST_VERIFY (buf.base.write_base == buf.direct); > + TEST_VERIFY (buf.base.write_ptr == buf.base.write_base + 3); > + TEST_COMPARE_BLOB (buf.direct, 3, "abc", 3); > + TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); > + TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); > + } > + > + { > + struct __printf_buffer_asprintf buf; > + __printf_buffer_asprintf_init (&buf); > + __printf_buffer (&buf.base, "abc[%d]DEF", 17); > + TEST_VERIFY (buf.base.write_base == buf.direct); > + TEST_VERIFY (buf.base.write_ptr == buf.base.write_base + 10); > + TEST_COMPARE_BLOB (buf.direct, 10, "abc[17]DEF", 10); > + TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); > + TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); > + } > + > + /* Test concatenation. */ > + { > + struct __printf_buffer_asprintf buf; > + __printf_buffer_asprintf_init (&buf); > + __printf_buffer (&buf.base, "abc"); > + TEST_VERIFY (buf.base.write_base == buf.direct); > + TEST_VERIFY (buf.base.write_ptr == buf.base.write_base + 3); > + TEST_COMPARE_BLOB (buf.direct, 3, "abc", 3); > + __printf_buffer (&buf.base, "[%d]DEF", 17); > + TEST_VERIFY (buf.base.write_base == buf.direct); > + TEST_VERIFY (buf.base.write_ptr == buf.base.write_base + 10); > + TEST_COMPARE_BLOB (buf.direct, 10, "abc[17]DEF", 10); > + TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); > + TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); > + } > + > + { > + struct __printf_buffer_asprintf buf; > + char large_string[500]; > + memset (large_string, 'X', array_length (large_string)); > + large_string[array_length (large_string) -1] = '\0'; > + int n; > + > + __printf_buffer_asprintf_init (&buf); > + __printf_buffer (&buf.base, "%s", large_string); > + TEST_VERIFY (buf.base.write_base != buf.direct); > + TEST_COMPARE_BLOB (buf.base.write_base, > + buf.base.write_ptr - buf.base.write_base, > + large_string, strlen (large_string)); > + TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); > + TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); > + > + __printf_buffer_asprintf_init (&buf); > + __printf_buffer (&buf.base, "abc"); > + __printf_buffer (&buf.base, "%n", &n); > + /* %n counts from the start of the buffer, not from within the > + printf call. */ > + TEST_COMPARE (n, 3); > + __printf_buffer (&buf.base, "%s", large_string); > + TEST_VERIFY (buf.base.write_base != buf.direct); > + TEST_COMPARE_BLOB (buf.base.write_base, 3, "abc", 3); > + TEST_COMPARE_BLOB (buf.base.write_base + 3, > + buf.base.write_ptr - buf.base.write_base - 3, > + large_string, strlen (large_string)); > + TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); > + __printf_buffer (&buf.base, "%n", &n); > + TEST_COMPARE (n, 3 + strlen (large_string)); > + TEST_COMPARE_BLOB (buf.base.write_base, 3, "abc", 3); > + TEST_COMPARE_BLOB (buf.base.write_base + 3, > + buf.base.write_ptr - buf.base.write_base - 3, > + large_string, strlen (large_string)); > + TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); > + TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); > + } > + > + /* Test %n with concatenation. */ > + { > + struct __printf_buffer_asprintf buf; > + __printf_buffer_asprintf_init (&buf); > + __printf_buffer (&buf.base, "abc"); > + int n = -1; > + __printf_buffer (&buf.base, "D%nEF", &n); > + TEST_COMPARE (n, 4); /* %n counts from the start of the buffer. */ > + TEST_VERIFY (buf.base.write_base == buf.direct); > + TEST_COMPARE_BLOB (buf.base.write_base, > + buf.base.write_ptr - buf.base.write_base, "abcDEF", 6); > + TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); > + TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); > + } > + return 0; > +} > + > +#include