From patchwork Fri Feb 9 15:25:44 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85534 X-Patchwork-Delegate: azanella@linux.vnet.ibm.com Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B30E93858432 for ; Fri, 9 Feb 2024 15:27:53 +0000 (GMT) X-Original-To: libc-alpha@sourceware.org Delivered-To: libc-alpha@sourceware.org Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) by sourceware.org (Postfix) with ESMTPS id 8C4C93858403 for ; Fri, 9 Feb 2024 15:25:48 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8C4C93858403 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=redhat.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=redhat.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 8C4C93858403 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.133.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492352; cv=none; b=wudcBCAFW/UGY+7wuCqR0nExpHVsyeXw5YMdKlI1sOS1iwrz2IBgEUcmOoLmgtodKhPz5jLn+61Vv8jBOAp7123vIoZEb0JWYZjkQ0w9/410EptWd8I4WemR+T1z6nWs51lWKUUUm6TybYqgvZyINlAPf9b8qMqGTMobX6OdTLc= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492352; c=relaxed/simple; bh=ZE+QGg/6dApvSeaFWGtIM3LqVb3ZSfEiioEM3Ctng6Q=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=ZhhEDwl+qRFghmAgdnD3Mf5urgAm1iiqLldyoay/0Bwo2bDvcCqRPYG1LuEE91picpzb1no2VNaiAjTz7wxcrIqK9e6oQYH3wHv9kZ8FXURfmcwXThzDGEF10tojZQYOAJIKKZQzMzL1bqUcx0kJq9Mj0kiuhiMOawstTaRXd74= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492348; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=c+VU3LaM6VvoPHW6BLivYIoAN/HVOfWlo5p0eWbZzi8=; b=eRmBIflpQmQWsqdHewf3HKn8ttYt5yE1dspfE+p6uqqWWRLVElJ+yEU6VZyU+v3kKN6FJx auZUAln3PSa29Wupp0j1rae+VyB2ZhsohAMr/5iMFMvZIBQCprApO/LqxH2xx+B5CSSwFR GRaVpI5woPK0kBgS8LbtK3YlSj7Rcsw= Received: from mimecast-mx02.redhat.com (mx-ext.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-634-outDb6MkMbOvDSaqg23hNg-1; Fri, 09 Feb 2024 10:25:46 -0500 X-MC-Unique: outDb6MkMbOvDSaqg23hNg-1 Received: from smtp.corp.redhat.com (int-mx10.intmail.prod.int.rdu2.redhat.com [10.11.54.10]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 8FD283C1E9CA for ; Fri, 9 Feb 2024 15:25:46 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B60AA492BE3 for ; Fri, 9 Feb 2024 15:25:45 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 08/11] stdio-common: Introduce the __printf_buffer function In-Reply-To: Message-ID: References: X-From-Line: f38d412ad4b7c65424502827cdfeff639858dc67 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:25:44 +0100 User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.3 (gnu/linux) MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.11.54.10 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-11.0 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_NONE, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_NONE, 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 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 It can be used to write to a __print_buffer (incrementally). 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 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 + 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