From patchwork Fri Feb 9 15:24:47 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85524 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 9159F385829B for ; Fri, 9 Feb 2024 15:25:21 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 5A11D385841D for ; Fri, 9 Feb 2024 15:24:52 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 5A11D385841D 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 5A11D385841D Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492294; cv=none; b=uNmbWJ146EE2TCYqM3sbEjwxAPFBKa74kiCaW0rXA/0BEWpyzD1qFQpCRtLP3mOaLvm2ae8413J9Mk4w7ENmXSCEyT77VwVExvuBh6t+K4W7a3RO3Vm5sacKMV4m2fTTDI+5fi87VGAT6A/MqgdiGDNtrhYQPuT4WwMSpiu8Qus= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492294; c=relaxed/simple; bh=JKKLU7kX+EKTQsLe+sfaxcYdCD3PiZuUJLIViMFUToc=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=iMeqylpnmJr4mBhyJvPgaZt1vJ60ZNv/I/7lYrKhuRB2NzCZfynEi+qSa2ct/fFUoEbtNZxZADh7I1ey4xQMFdwwpIjj1KWvGYxCrnTtonhUutvXMCXxUzEEFnId5Pa6GDVaOZcCDXytUu6gEZ3mYtoavjP5qyU1/lDd42quh/A= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492291; 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=6WBS0MOErUE2FStW4oBg0Uj6cvf4phvfWYmNfTPvUKI=; b=SGKtkCAa9zJMwfHxobVNKAaPrG90Q7mELo+V5yaAcNstVv09VyK3uykdLFgKjMOgD6qFYz RivE87jNquheUuw1Nk7N0HlbiRFJ7RLOyq9JcA2X7aMC996AIvCIgaJ7UevLyPjOay2E3d TB0xB3EPEhz47mG8pP/M/lLtfMsmH9k= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-335-qzeqx4aQMJ-Gb0tmpWHkXw-1; Fri, 09 Feb 2024 10:24:50 -0500 X-MC-Unique: qzeqx4aQMJ-Gb0tmpWHkXw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (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 0F666106D0C3 for ; Fri, 9 Feb 2024 15:24:50 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6808EAC1F for ; Fri, 9 Feb 2024 15:24:49 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 01/11] misc: Build getdomainname with fortification In-Reply-To: Message-ID: <7710004342d09a8e62868a0bc659b5dca5b67324.1707491940.git.fweimer@redhat.com> References: X-From-Line: 7710004342d09a8e62868a0bc659b5dca5b67324 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:24:47 +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.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.7 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_H2, 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 Introduce __glibc_nofortify_getdomainname to request disabling the fortification wrapper. Reviewed-by: Adhemerval Zanella --- misc/Makefile | 1 - misc/getdomain.c | 2 ++ posix/bits/unistd.h | 3 ++- sysdeps/mach/hurd/getdomain.c | 2 ++ 4 files changed, 6 insertions(+), 2 deletions(-) diff --git a/misc/Makefile b/misc/Makefile index c273ec6974..44ae89670a 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -209,7 +209,6 @@ routines := \ # Exclude fortified routines from being built with _FORTIFY_SOURCE routines_no_fortify += \ - getdomain \ gethostname \ syslog \ # routines_no_fortify diff --git a/misc/getdomain.c b/misc/getdomain.c index f6325644c9..f96636821f 100644 --- a/misc/getdomain.c +++ b/misc/getdomain.c @@ -19,6 +19,8 @@ The result is null-terminated if LEN is large enough for the full name and the terminator. */ +#define __glibc_nofortify_getdomainname + #include #include #include diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h index bd209ec28e..6ed2943bf1 100644 --- a/posix/bits/unistd.h +++ b/posix/bits/unistd.h @@ -149,7 +149,8 @@ __NTH (gethostname (char *__buf, size_t __buflen)) #endif -#if defined __USE_MISC || (defined __USE_XOPEN && !defined __USE_UNIX98) +#if (defined __USE_MISC || (defined __USE_XOPEN && !defined __USE_UNIX98)) \ + && !defined __glibc_nofortify_getdomainname __fortify_function int __NTH (getdomainname (char *__buf, size_t __buflen)) { diff --git a/sysdeps/mach/hurd/getdomain.c b/sysdeps/mach/hurd/getdomain.c index 137ce9ad5b..e24ab2137e 100644 --- a/sysdeps/mach/hurd/getdomain.c +++ b/sysdeps/mach/hurd/getdomain.c @@ -15,6 +15,8 @@ License along with the GNU C Library; if not, see . */ +#define __glibc_nofortify_getdomainname + #include #include "hurdhost.h" From patchwork Fri Feb 9 15:24:54 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85525 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 D6A8F38582B0 for ; Fri, 9 Feb 2024 15:25:29 +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 7C5E13858283 for ; Fri, 9 Feb 2024 15:24:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7C5E13858283 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 7C5E13858283 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=1707492300; cv=none; b=Wccq8KDvuUabCuBmjApkXpB5qayb9rrR/KJ2RUH0+a/oNVcEDh+z12gh7WvUq4yVRWqRrfCMnkNMS++gEWPBRoi9Tk572A0vixCtwPgjdDCqP/hwluM7PE3k8e9tRSoQJTioBsOtZ57YIS94PkRuemMBrMfdYk5SRqh97Iuy+84= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492300; c=relaxed/simple; bh=8zDBdr3BoV+gCJkE7BxmTHidGHzHlbSFmQW6uejo3dw=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=hBUaDAfmL+KRC0XKTrCtmYip03JA2IlLXrLBx6SXU/zFvt5aQijBLX+QWrAnltgTWZoGEHb8fJdg/1jgyixFz+Z3DXswVAYpvjBtq3j1DE43f+toj0e6K3VwEAdPvrPT/b3bmU+rVwVCRgcAIQPQJGO14IEjfAnXDMyqE9MFKnk= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492298; 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=u1JrGcNwq2df22YFN/AkBjsycERcmQGNx4zRqVelG2s=; b=Q1Qbbp8ORIBGclYtGtE2eBxKKUA/eFAODrVHOskvkQhg7sI07rZo+bO/t14Qbrqk6G12++ lrcwliL3+eWKQDKvH6d5ZkCqvdiRQ28FulrI1rpc6sDZSLjCt1QxP1vKweIascSNbe3UsX 8CwDJWtQwEzMfyqM7awv1r9HItih3xU= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-350-RxjZWZbKOKmz0yT5CehfQg-1; Fri, 09 Feb 2024 10:24:56 -0500 X-MC-Unique: RxjZWZbKOKmz0yT5CehfQg-1 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.rdu2.redhat.com [10.11.54.7]) (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 9978E185A782 for ; Fri, 9 Feb 2024 15:24:56 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 15DCC1C14B04 for ; Fri, 9 Feb 2024 15:24:55 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 02/11] misc: Build gethostname with fortification In-Reply-To: Message-ID: References: X-From-Line: f0bd7ca129f6dd3df2e9b03270e6fbd5990ace70 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:24:54 +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.7 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.7 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 Introduce __glibc_nofortify_gethostname to request disabling the fortification wrapper. Reviewed-by: Adhemerval Zanella --- misc/Makefile | 1 - misc/gethostname.c | 2 ++ posix/bits/unistd.h | 3 ++- sysdeps/mach/hurd/gethostname.c | 2 ++ sysdeps/posix/gethostname.c | 2 ++ sysdeps/unix/sysv/linux/alpha/gethostname.c | 2 ++ 6 files changed, 10 insertions(+), 2 deletions(-) diff --git a/misc/Makefile b/misc/Makefile index 44ae89670a..6d8528c925 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -209,7 +209,6 @@ routines := \ # Exclude fortified routines from being built with _FORTIFY_SOURCE routines_no_fortify += \ - gethostname \ syslog \ # routines_no_fortify diff --git a/misc/gethostname.c b/misc/gethostname.c index 2c849a95c3..82800f8abb 100644 --- a/misc/gethostname.c +++ b/misc/gethostname.c @@ -15,6 +15,8 @@ License along with the GNU C Library; if not, see . */ +#define __glibc_nofortify_gethostname + #include #include diff --git a/posix/bits/unistd.h b/posix/bits/unistd.h index 6ed2943bf1..1230bba9a5 100644 --- a/posix/bits/unistd.h +++ b/posix/bits/unistd.h @@ -138,7 +138,8 @@ getlogin_r (char *__buf, size_t __buflen) #endif -#if defined __USE_MISC || defined __USE_UNIX98 +#if defined __USE_MISC || defined __USE_UNIX98 \ + && !defined __glibc_nofortify_gethostname __fortify_function int __NTH (gethostname (char *__buf, size_t __buflen)) { diff --git a/sysdeps/mach/hurd/gethostname.c b/sysdeps/mach/hurd/gethostname.c index 91d7f45d6e..dbb7d75713 100644 --- a/sysdeps/mach/hurd/gethostname.c +++ b/sysdeps/mach/hurd/gethostname.c @@ -15,6 +15,8 @@ License along with the GNU C Library; if not, see . */ +#define __glibc_nofortify_gethostname + #include #include "hurdhost.h" diff --git a/sysdeps/posix/gethostname.c b/sysdeps/posix/gethostname.c index 3c50706b58..3c79f77dd3 100644 --- a/sysdeps/posix/gethostname.c +++ b/sysdeps/posix/gethostname.c @@ -15,6 +15,8 @@ License along with the GNU C Library; if not, see . */ +#define __glibc_nofortify_gethostname + #include #include #include diff --git a/sysdeps/unix/sysv/linux/alpha/gethostname.c b/sysdeps/unix/sysv/linux/alpha/gethostname.c index 30da8f30e4..45eb1788d4 100644 --- a/sysdeps/unix/sysv/linux/alpha/gethostname.c +++ b/sysdeps/unix/sysv/linux/alpha/gethostname.c @@ -15,6 +15,8 @@ License along with the GNU C Library. If not, see . */ +#define __glibc_nofortify_gethostname + #include #include #include From patchwork Fri Feb 9 15:25:04 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85526 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 464DC38582B2 for ; Fri, 9 Feb 2024 15:25:40 +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 8745F3858439 for ; Fri, 9 Feb 2024 15:25:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8745F3858439 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 8745F3858439 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=1707492310; cv=none; b=aYCMd4nTejVfCal7yBRqeyMr3AjvGtpn59LS/9SOSj/0WN0Jsjx+41y16XfD1c3DuseNbEyqh77Kuokj/ao0e8+cRApB/IlCK0WeJtkZz+vhB0bkB0aNVtjjTy8K3gJVUcfkol947Xg3pGYnK8R2PIogCkyKjc0QgYDoXx6Qh4k= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492310; c=relaxed/simple; bh=pGqu5VgpySGJho/Xns/sIBSWBf2A+EznYEfvML6n2WM=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=Dm8IAjzVXnw9IzS+NfrzT4cTlPJoMIRESPXHem8cXJoaCZ/Hh3tdOmbXrs8EQTkUeaB9+gDE80cR8nSn6FnFs86f8OgHxBTJRcKNROBK7BsZkxCyoysqKOK0UO9zLX9HqUepDfCJF7en5NupcQucCcLGGIxiIcIBck+XWVVHTEs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492308; 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=ceCAv6pPsBH/3Q4mezG1E6VSZYnH1aE2y5J4FRuqLU4=; b=RQrPWYR13X7BoTaUH+D7wcjUVUoOvERvE6CspmHdhI2XM8d8FMgGfY1mEHYQEEyO9uzmxN qNs3TjL/psvQv4lJcNn5JchO5ZyZszo1KW2kyNk8a9wjhRuz5XFZbPfexvZ/mpGnE+Qrga KhVxv5ZDQBdS2CWifA+DksJl2M8n3vU= 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-692-c14AtKyPMnq1lbnQPkL-tA-1; Fri, 09 Feb 2024 10:25:06 -0500 X-MC-Unique: c14AtKyPMnq1lbnQPkL-tA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 9E0B11C05129 for ; Fri, 9 Feb 2024 15:25:06 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 038921103A for ; Fri, 9 Feb 2024 15:25:05 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 03/11] libio: Add fortify wrapper for internal __snprintf In-Reply-To: Message-ID: <45d399f3e70726984705466772370ba33ce3aa8a.1707491940.git.fweimer@redhat.com> References: X-From-Line: 45d399f3e70726984705466772370ba33ce3aa8a Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:25:04 +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.5 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, 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 --- debug/snprintf_chk.c | 1 + include/bits/stdio2.h | 9 +++++++++ include/stdio.h | 1 + 3 files changed, 11 insertions(+) diff --git a/debug/snprintf_chk.c b/debug/snprintf_chk.c index 995d805f5d..ea02efec3e 100644 --- a/debug/snprintf_chk.c +++ b/debug/snprintf_chk.c @@ -40,4 +40,5 @@ ___snprintf_chk (char *s, size_t maxlen, int flag, size_t slen, return ret; } +ldbl_hidden_def (___snprintf_chk, __snprintf_chk) ldbl_strong_alias (___snprintf_chk, __snprintf_chk) diff --git a/include/bits/stdio2.h b/include/bits/stdio2.h index 6d7cf4306f..fb909c21ee 100644 --- a/include/bits/stdio2.h +++ b/include/bits/stdio2.h @@ -1 +1,10 @@ #include + +__fortify_function int +__NTH (__snprintf (char *__restrict __s, size_t __n, + const char *__restrict __fmt, ...)) +{ + return __builtin___snprintf_chk (__s, __n, __USE_FORTIFY_LEVEL - 1, + __glibc_objsize (__s), __fmt, + __va_arg_pack ()); +} diff --git a/include/stdio.h b/include/stdio.h index 24f1652f19..364f4d22a1 100644 --- a/include/stdio.h +++ b/include/stdio.h @@ -62,6 +62,7 @@ extern int __vsscanf (const char *__restrict __s, extern int __sprintf_chk (char *, int, size_t, const char *, ...) __THROW; extern int __snprintf_chk (char *, size_t, int, size_t, const char *, ...) __THROW; +stdio_hidden_ldbl_proto (__, snprintf_chk) extern int __vsprintf_chk (char *, int, size_t, const char *, __gnuc_va_list) __THROW; extern int __vsnprintf_chk (char *, size_t, int, size_t, const char *, From patchwork Fri Feb 9 15:25:13 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85529 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 158AC3858022 for ; Fri, 9 Feb 2024 15:26:43 +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 ED6383858014 for ; Fri, 9 Feb 2024 15:25:17 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org ED6383858014 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 ED6383858014 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=1707492319; cv=none; b=UMob6gz2AgjxLwk6+C+YLxIktFJTWADoRm60XL1FdkhrH3bSJiTYItmKz9TJwg7IGVBXBBdZqJJtUhgx3rIspJkkNjika4LP3qgsVfEf4MYrDX8kuk3dESGe7VKgYlO5kgdwm/3D/nciMWK90UPsNotf2p96TWsM/UHm09DsymY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492319; c=relaxed/simple; bh=9FvvSzgWAMVSuymzi8d16HxTp53gFWlDHQL5enAi0dE=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=s51It5nyFZghL8rIWjFRDl2SWnM4NTumKKi76g6EKO/V96z7kMUCFCyhGc8l5BC2U/oxEIqcRdJhbLiKW8ntGHYkHxHhnK5qJoowG2VfHup3GSx8ok75DTfLHdWKa3Op9ZjY8RFajukW2MygsOSua6RL22GLOs2d5auiTFVTLp0= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492317; 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=jxonwt4NHHJXGEuHnhbvhj4kYhoREmBC2PCKbvzMsZc=; b=cZ83R0pXHDW/QGgw0Dd85TLqECJZOZ0oIPAOzhHJBkQGT24Uwepe51Z9TOncIRy+6SdBS+ YPb4kGJyKhxI5+Aaoernm+GM12e3qarSHQK3zwZTef6bL682LExkfRRbUAt1m6K70n7Xz8 Wz/mzQMy2aCQl6l6qAQi/2UEbH2C+o4= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-44--MDeuozzO2m0gu4c_k5MvQ-1; Fri, 09 Feb 2024 10:25:16 -0500 X-MC-Unique: -MDeuozzO2m0gu4c_k5MvQ-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 D49E38F4768 for ; Fri, 9 Feb 2024 15:25:15 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6A4C711217 for ; Fri, 9 Feb 2024 15:25:15 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 04/11] syslog: Update misc/tst-syslog to check reported %n value In-Reply-To: Message-ID: <8502804480ab69e7ae48dfe43552e9a6be596711.1707491940.git.fweimer@redhat.com> References: X-From-Line: 8502804480ab69e7ae48dfe43552e9a6be596711 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:25:13 +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.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 --- misc/tst-syslog.c | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/misc/tst-syslog.c b/misc/tst-syslog.c index ced9ae8467..741693a7ff 100644 --- a/misc/tst-syslog.c +++ b/misc/tst-syslog.c @@ -102,8 +102,10 @@ send_vsyslog (int options) { int facility = facilities[i]; int priority = priorities[j]; - call_vsyslog (facility | priority, "%s %d %d", SYSLOG_MSG_BASE, - facility, priority); + int pos = -1; + call_vsyslog (facility | priority, "%n%s %d %d", + &pos, SYSLOG_MSG_BASE, facility, priority); + TEST_COMPARE (pos, 0); } } } @@ -117,8 +119,10 @@ send_syslog (int options) { int facility = facilities[i]; int priority = priorities[j]; - syslog (facility | priority, "%s %d %d", SYSLOG_MSG_BASE, facility, - priority); + int pos = -1; + syslog (facility | priority, "%n%s %d %d", + &pos, SYSLOG_MSG_BASE, facility, priority); + TEST_COMPARE (pos, 0); } } } From patchwork Fri Feb 9 15:25:23 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85527 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 559D73858012 for ; Fri, 9 Feb 2024 15:25:55 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 0BDAB3858407 for ; Fri, 9 Feb 2024 15:25:27 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0BDAB3858407 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 0BDAB3858407 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492328; cv=none; b=ONOMwKMWjRlOZE+eqwZZSJx+zqHxDmizmTx+HtcgmTb/DlFtIbQnsO+TshOaH5dAgo3cDopkyUo8mhBR5hVR2PRnqNBMZ4MXV1pmUzTyP9po1LK97CiE1Kgwxha913oX7AGj3oJ6FVadjUJdeXiqefPsKQP4uUEzBxyR0wJqyC8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492328; c=relaxed/simple; bh=asvuEZGDVQ68oiWI91gSjabHaw8A+GYk3Izkt+jOi8A=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=tsKgA3wvLMRSIqqlqSwbIDzD5URr3CkrjDLsBp8kwrUKF1I4YQnCGIKRwbS45enVSk2x85yD7twXhb+/POSnc++oqVe00VNDyJfD8iWIO1CkzZiY+mJ1hoK2kPkACCtJueCucibC0bEm9MPI9IzZxwm+ipxoOVah0sWIFQLDRfM= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492326; 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=x0OdSTbXNOPxgL0QktflHI7piP1rkMFBjh4nhoRtlMc=; b=MHtHBcpNpzb6PHikUgSOrwwjHb+92A0l01Evp18TGozI5Hh3+0rW2mSWZqtRA0C19/lnqW o8lwgERLhyxbwxDMiW4eenqbite0Op+xNeHsPEG/y/GdmdXcZuKABjw27FgAGsCU5uCDMI fKHUxfECReZ7IZ7eNJvCPWufMkzioaA= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-197-7afrDYFGPcqaQKTDCoVh6g-1; Fri, 09 Feb 2024 10:25:25 -0500 X-MC-Unique: 7afrDYFGPcqaQKTDCoVh6g-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 E155F185A780 for ; Fri, 9 Feb 2024 15:25:24 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 866DCC08EF7 for ; Fri, 9 Feb 2024 15:25:24 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 05/11] syslog: Build with fortification In-Reply-To: Message-ID: <3025a0556566ebbe0955666387d1285b39605de4.1707491940.git.fweimer@redhat.com> References: X-From-Line: 3025a0556566ebbe0955666387d1285b39605de4 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:25:23 +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.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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 This causes /proc/self/maps to be processed for certain syslog calls due to the use of %n. Reviewed-by: Adhemerval Zanella --- misc/Makefile | 5 ----- misc/bits/syslog.h | 4 ++++ misc/syslog.c | 3 +++ 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/misc/Makefile b/misc/Makefile index 6d8528c925..236076a9d6 100644 --- a/misc/Makefile +++ b/misc/Makefile @@ -207,11 +207,6 @@ routines := \ writev \ # routines -# Exclude fortified routines from being built with _FORTIFY_SOURCE -routines_no_fortify += \ - syslog \ - # routines_no_fortify - generated += \ tst-allocate_once-mem.out \ tst-allocate_once.mtrace \ diff --git a/misc/bits/syslog.h b/misc/bits/syslog.h index aadcd42000..e6f7938a3f 100644 --- a/misc/bits/syslog.h +++ b/misc/bits/syslog.h @@ -31,11 +31,13 @@ redirections, e.g. long double asm redirections. */ #ifdef __va_arg_pack +# ifndef __glibc_nofortify_syslog __fortify_function void syslog (int __pri, const char *__fmt, ...) { __syslog_chk (__pri, __USE_FORTIFY_LEVEL - 1, __fmt, __va_arg_pack ()); } +# endif #elif !defined __cplusplus # define syslog(pri, ...) \ __syslog_chk (pri, __USE_FORTIFY_LEVEL - 1, __VA_ARGS__) @@ -43,9 +45,11 @@ syslog (int __pri, const char *__fmt, ...) #ifdef __USE_MISC +# ifndef __glibc_nofortify_vsyslog __fortify_function void vsyslog (int __pri, const char *__fmt, __gnuc_va_list __ap) { __vsyslog_chk (__pri, __USE_FORTIFY_LEVEL - 1, __fmt, __ap); } +# endif #endif diff --git a/misc/syslog.c b/misc/syslog.c index 4af87f54fd..68ee3aef5f 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -27,6 +27,9 @@ * SUCH DAMAGE. */ +#define __glibc_nofortify_syslog +#define __glibc_nofortify_vsyslog + #if defined(LIBC_SCCS) && !defined(lint) static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; #endif /* LIBC_SCCS and not lint */ From patchwork Fri Feb 9 15:25:32 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85531 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 4FD9E3858011 for ; Fri, 9 Feb 2024 15:26:58 +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 548DD3858292 for ; Fri, 9 Feb 2024 15:25:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 548DD3858292 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 548DD3858292 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=1707492339; cv=none; b=K/MNHLSO+Cp78wYHF8QEoYQ/MB3xJCFeCA+bfkC39xCFK6IXjiNdeBZXRq3NeemZyrPYqYE0zC0pXJ6QfplwMO3PHVJcW3wpXRdk0gzmj3WJ6WKLnTpgFfQDPRvFSmqvXg1CB2FsSdfQDBkJFKOKVtvAzLspHQIF320Lw/+sNsU= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492339; c=relaxed/simple; bh=1qhPUFg2iig3XQA3d7IWjNgLnk97RgXy39PQ3VQ4KiU=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=u+kq5jAmu6WOXndcy1eriZer59at2BEgwRH9YYbRc/BW9HPpEdaAONudw53g2NbtFkGgSGcg4InnzwmF4qulcJOhEwWgsYlFYNwLSxTBl071iiUPrciNrxmdgEmgyIKK3CjL+HSR9/XLfLvx21QZmQtXuXzXTKMOyqUIac1pgOU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492336; 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=t/NoYXUrkb4feYtxyno0FEE9yQA0mxg7BNX5uaM4+Dw=; b=JpQDHNl2KSVnEb1M6aK84fmvEcRnPcO64eK+rIaM6PxKa7kAIQtTsV7r6YdKLYdZtaNuoY hnm+Xz0wFeEFzI7y0G07lbpwvWrNwTAprlaJNX01scigiFbb7NazHiB3gloSCexBLA84Js R7c1Um6j0nDFQs55i7gKqpg0TMNFChs= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-650-C9PN-VKeN_ykFVjhJpI5lw-1; Fri, 09 Feb 2024 10:25:34 -0500 X-MC-Unique: C9PN-VKeN_ykFVjhJpI5lw-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 7BE1385A58C for ; Fri, 9 Feb 2024 15:25:34 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B44FB1103A for ; Fri, 9 Feb 2024 15:25:33 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 06/11] stdio: Rename __printf_buffer to __vfprintf_buffer In-Reply-To: Message-ID: References: X-From-Line: c23bed2f8fed8be96f0b56f3b81430515e6ccf24 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:25:32 +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.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 And __wprintf_buffer to __vwprintf_buffer. These functions take a va_list argument, so they are closer to the v-variant functions in . Also move the declaration to , which is arguably a better fit than (which contains lots of printf internals). Reviewed-by: Adhemerval Zanella --- include/printf.h | 9 +-------- include/printf_buffer.h | 9 +++++++++ libio/iovdprintf.c | 2 +- libio/iovsprintf.c | 2 +- libio/obprintf.c | 2 +- libio/vasprintf.c | 2 +- libio/vsnprintf.c | 2 +- libio/vswprintf.c | 2 +- stdio-common/vfprintf-internal.c | 10 ++++++---- 9 files changed, 22 insertions(+), 18 deletions(-) diff --git a/include/printf.h b/include/printf.h index 2c998059d4..78aac4dcad 100644 --- a/include/printf.h +++ b/include/printf.h @@ -65,18 +65,11 @@ int __translated_number_width (locale_t loc, const char *first, const char *last) attribute_hidden; - -struct __printf_buffer; -void __printf_buffer (struct __printf_buffer *buf, const char *format, - va_list ap, unsigned int mode_flags); -struct __wprintf_buffer; -void __wprintf_buffer (struct __wprintf_buffer *buf, const wchar_t *format, - va_list ap, unsigned int mode_flags); - extern int __printf_fp (FILE *, const struct printf_info *, const void *const *); libc_hidden_proto (__printf_fp) +struct __printf_buffer; void __printf_fphex_l_buffer (struct __printf_buffer *, locale_t, const struct printf_info *, const void *const *) attribute_hidden; diff --git a/include/printf_buffer.h b/include/printf_buffer.h index eff8e3413a..fb0b42178e 100644 --- a/include/printf_buffer.h +++ b/include/printf_buffer.h @@ -200,6 +200,12 @@ int __printf_buffer_done (struct __printf_buffer *buf) attribute_hidden; known, and the flush implementation can be called directly. */ bool __printf_buffer_flush (struct __printf_buffer *buf) attribute_hidden; +/* Building block for printf-style functions. Write argument list AP + to BUF according to FORMAT. MODE_FLAGS contains PRINTF_* flags + defined in . */ +void __vprintf_buffer (struct __printf_buffer *buf, const char *format, + va_list ap, unsigned int mode_flags) attribute_hidden; + /* Wide version of struct __printf_buffer follows. */ enum __wprintf_buffer_mode @@ -274,6 +280,9 @@ int __wprintf_buffer_done (struct __wprintf_buffer *buf) attribute_hidden; bool __wprintf_buffer_flush (struct __wprintf_buffer *buf) attribute_hidden; +void __vwprintf_buffer (struct __wprintf_buffer *buf, const wchar_t *format, + va_list ap, unsigned int mode_flags) attribute_hidden; + /* Type-generic convenience macros. They are useful if printf_buffer-char.h or printf_buffer-wchar_t.h is included as well. */ diff --git a/libio/iovdprintf.c b/libio/iovdprintf.c index 6894fa8347..47d9d0ba8f 100644 --- a/libio/iovdprintf.c +++ b/libio/iovdprintf.c @@ -66,7 +66,7 @@ __vdprintf_internal (int d, const char *format, va_list arg, __printf_buffer_init (&buf.base, buf.buf, array_length (buf.buf), __printf_buffer_mode_dprintf); buf.fd = d; - __printf_buffer (&buf.base, format, arg, mode_flags); + __vprintf_buffer (&buf.base, format, arg, mode_flags); if (__printf_buffer_has_failed (&buf.base)) return -1; __printf_buffer_flush_dprintf (&buf); diff --git a/libio/iovsprintf.c b/libio/iovsprintf.c index 2d81ee3507..7fdc06dcd4 100644 --- a/libio/iovsprintf.c +++ b/libio/iovsprintf.c @@ -59,7 +59,7 @@ __vsprintf_internal (char *string, size_t maxlen, __printf_buffer_init_end (&buf, string, (char *) ~(uintptr_t) 0, __printf_buffer_mode_sprintf); - __printf_buffer (&buf, format, args, mode_flags); + __vprintf_buffer (&buf, format, args, mode_flags); /* Write the NUL terminator if there is room. Do not use the putc operation to avoid overflowing the character write count. */ diff --git a/libio/obprintf.c b/libio/obprintf.c index 491d14fbae..24d1edf260 100644 --- a/libio/obprintf.c +++ b/libio/obprintf.c @@ -92,7 +92,7 @@ __obstack_vprintf_internal (struct obstack *obstack, const char *format, /* Now allocate the rest of the current chunk. */ obstack_blank_fast (obstack, room); - __printf_buffer (&buf.base, format, args, mode_flags); + __vprintf_buffer (&buf.base, format, args, mode_flags); if (buf.base.write_ptr == &buf.ch + 1) /* buf.ch is in use. Put it into the obstack. */ diff --git a/libio/vasprintf.c b/libio/vasprintf.c index 999ae526f4..edcfab2323 100644 --- a/libio/vasprintf.c +++ b/libio/vasprintf.c @@ -99,7 +99,7 @@ __vasprintf_internal (char **result_ptr, const char *format, va_list args, __printf_buffer_init (&buf.base, buf.direct, array_length (buf.direct), __printf_buffer_mode_asprintf); - __printf_buffer (&buf.base, format, args, mode_flags); + __vprintf_buffer (&buf.base, format, args, mode_flags); int done = __printf_buffer_done (&buf.base); if (done < 0) { diff --git a/libio/vsnprintf.c b/libio/vsnprintf.c index dd7a585cbe..7077fcd9f2 100644 --- a/libio/vsnprintf.c +++ b/libio/vsnprintf.c @@ -93,7 +93,7 @@ __vsnprintf_internal (char *string, size_t maxlen, const char *format, { struct __printf_buffer_snprintf buf; __printf_buffer_snprintf_init (&buf, string, maxlen); - __printf_buffer (&buf.base, format, args, mode_flags); + __vprintf_buffer (&buf.base, format, args, mode_flags); return __printf_buffer_snprintf_done (&buf); } diff --git a/libio/vswprintf.c b/libio/vswprintf.c index 5990bd2eff..d73246ba41 100644 --- a/libio/vswprintf.c +++ b/libio/vswprintf.c @@ -41,7 +41,7 @@ __vswprintf_internal (wchar_t *string, size_t maxlen, const wchar_t *format, struct __wprintf_buffer buf; __wprintf_buffer_init (&buf, string, maxlen, __wprintf_buffer_mode_swprintf); - __wprintf_buffer (&buf, format, args, mode_flags); + __vwprintf_buffer (&buf, format, args, mode_flags); if (buf.write_ptr == buf.write_end) { diff --git a/stdio-common/vfprintf-internal.c b/stdio-common/vfprintf-internal.c index 771beca9bf..23754232f2 100644 --- a/stdio-common/vfprintf-internal.c +++ b/stdio-common/vfprintf-internal.c @@ -122,6 +122,7 @@ #ifndef COMPILE_WPRINTF # include "printf_buffer-char.h" +# define VFPRINTF_BUFFER __vprintf_buffer # define vfprintf __vfprintf_internal # define OTHER_CHAR_T wchar_t # define UCHAR_T unsigned char @@ -136,6 +137,7 @@ typedef const char *THOUSANDS_SEP_T; # define CONVERT_FROM_OTHER_STRING __wcsrtombs #else # include "printf_buffer-wchar_t.h" +# define VFPRINTF_BUFFER __vwprintf_buffer # define vfprintf __vfwprintf_internal # define OTHER_CHAR_T char /* This is a hack!!! There should be a type uwchar_t. */ @@ -595,8 +597,8 @@ static void group_number (struct Xprintf_buffer *buf, /* The buffer-based function itself. */ void -Xprintf_buffer (struct Xprintf_buffer *buf, const CHAR_T *format, - va_list ap, unsigned int mode_flags) +VFPRINTF_BUFFER (struct Xprintf_buffer *buf, const CHAR_T *format, + va_list ap, unsigned int mode_flags) { /* The character used as thousands separator. */ THOUSANDS_SEP_T thousands_sep = 0; @@ -1541,7 +1543,7 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap, unsigned int mode_flags) { struct Xprintf (buffer_to_file) wrap; Xprintf (buffer_to_file_init) (&wrap, s); - Xprintf_buffer (&wrap.base, format, ap, mode_flags); + VFPRINTF_BUFFER (&wrap.base, format, ap, mode_flags); return Xprintf (buffer_to_file_done) (&wrap); } @@ -1556,7 +1558,7 @@ vfprintf (FILE *s, const CHAR_T *format, va_list ap, unsigned int mode_flags) Xprintf (buffer_to_file_init) (&wrap, s); /* Perform the printing operation on the buffer. */ - Xprintf_buffer (&wrap.base, format, ap, mode_flags); + VFPRINTF_BUFFER (&wrap.base, format, ap, mode_flags); done = Xprintf (buffer_to_file_done) (&wrap); /* Unlock the stream. */ From patchwork Fri Feb 9 15:25:38 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85532 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 5E461385802E for ; Fri, 9 Feb 2024 15:27:07 +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.129.124]) by sourceware.org (Postfix) with ESMTPS id 4EC9D385829E for ; Fri, 9 Feb 2024 15:25:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4EC9D385829E 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 4EC9D385829E Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=170.10.129.124 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492345; cv=none; b=vadn76mSfPoINLRoO13DCrY0gvsWYYeEpxEeue99i/H/hFzTBINyaT7xcK7ziEVseJX9djlkJ9nFW5qCkcQipzDyPP4YayunpTWJZRpJyD2kiK7JQPbrV6jvPA7BmXO3B0fgkqvFkEvYm2/fW0kyq0Nffz5ivkyoRqJFuiv/LuI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492345; c=relaxed/simple; bh=nFLvjHXYiS8ceIj0M1yUMgFIosQ78kkjB6Q5KAAmWGY=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=aAdpAn7WGgCWSvfXjJnyC4tUguSpLTj3Wf9F/0wPCIGsRFP3c8O3+WiKObKGdT8EqcFTeK5W69VNc/BES7OV3RlEI5fCKI/sxhUZ+mhXtSx7+hcpbVTaH4PcJ19rw2As0jHsXum0Gj0fvSP7K8XY2YgCw7H/8awmYrHPf8sVHSs= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492342; 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=AKOfd7Roz9dgnDRT4eNNSjdepnfuGDPoh6MHXAjgMK0=; b=GQGFcHvTnj/x4I8d/0rRK4j7F0hvPrJOmgDJVP1+7Gp2kq6MN+7ZAEymM6RbFKEnb7FimA IVyLTkf5kpQhElHPbsCDy1O2ttK2zkN4dBaIFvZInQnETY0AJ+DcXclPO5Jesj/tXN4z1P z5oS7JreiIb+jdUoI519Mg05j4PzZrg= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-265-G3UYzYEGPa6rlwS-RvKatA-1; Fri, 09 Feb 2024 10:25:40 -0500 X-MC-Unique: G3UYzYEGPa6rlwS-RvKatA-1 Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.rdu2.redhat.com [10.11.54.5]) (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 6CD4F85A58C for ; Fri, 9 Feb 2024 15:25:40 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id DDBAD1103A for ; Fri, 9 Feb 2024 15:25:39 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 07/11] libio: Extract __printf_buffer_asprintf_init from asprintf implementation In-Reply-To: Message-ID: <345c3f94450a38be9408f158a211c82a657034b1.1707491940.git.fweimer@redhat.com> References: X-From-Line: 345c3f94450a38be9408f158a211c82a657034b1 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:25:38 +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.5 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, 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 And __printf_buffer_asprintf_free as well. This will allow to reuse the asprintf implementation for more general buffer handling, similar to open_memstream, but without the mandatory heap allocation. Reviewed-by: Adhemerval Zanella --- include/printf_buffer.h | 30 +++++++++++++++++++++++++----- libio/vasprintf.c | 40 +++++++++++++++++----------------------- 2 files changed, 42 insertions(+), 28 deletions(-) diff --git a/include/printf_buffer.h b/include/printf_buffer.h index fb0b42178e..cee5afb581 100644 --- a/include/printf_buffer.h +++ b/include/printf_buffer.h @@ -317,6 +317,31 @@ void __printf_buffer_snprintf_init (struct __printf_buffer_snprintf *, int __printf_buffer_snprintf_done (struct __printf_buffer_snprintf *) attribute_hidden; +/* Size of the initial on-stack buffer for asprintf. It should be + large enough to copy almost all asprintf usages with just a single + (final, correctly sized) heap allocation. */ +#define PRINTF_BUFFER_SIZE_ASPRINTF 200 + +struct __printf_buffer_asprintf +{ + /* base.write_base points either to a heap-allocated buffer, or to + the direct array below. */ + struct __printf_buffer base; + + /* Initial allocation. */ + char direct[PRINTF_BUFFER_SIZE_ASPRINTF]; +}; + +/* Sets up BUF for writing via __printf_buffer. */ +void __printf_buffer_asprintf_init (struct __printf_buffer_asprintf *buf) + attribute_hidden; + +/* Deallocates any allocated memory in *BUF. (This is not the usual + done routine for asprintf because that has to preserve allocation.) + Always returns -1 to indicate an error. */ +int __printf_buffer_asprintf_free (struct __printf_buffer_asprintf *buf) + attribute_hidden; + /* Flush function implementations follow. They are called from __printf_buffer_flush. Generic code should not call these flush functions directly. Some modes have inline implementations. */ @@ -363,11 +388,6 @@ void __wprintf_buffer_flush_to_file (struct __wprintf_buffer_to_file *) /* Temporary buffer used during floating point digit translation. */ #define PRINTF_BUFFER_SIZE_DIGITS 64 -/* Size of the initial on-stack buffer for asprintf. It should be - large enough to copy almost all asprintf usages with just a single - (final, correctly sized) heap allocation. */ -#define PRINTF_BUFFER_SIZE_ASPRINTF 200 - /* This should cover most of the packet-oriented file descriptors, where boundaries between writes could be visible to readers. But it is still small enough not to cause too many stack overflow issues. */ diff --git a/libio/vasprintf.c b/libio/vasprintf.c index edcfab2323..f8413eedfe 100644 --- a/libio/vasprintf.c +++ b/libio/vasprintf.c @@ -34,18 +34,6 @@ #include #include -struct __printf_buffer_asprintf -{ - /* base.write_base points either to a heap-allocated buffer, or to - the direct array below. */ - struct __printf_buffer base; - - /* Initial allocation. 200 should be large enough to copy almost - all asprintf usages with just a single (final, correctly sized) - heap allocation. */ - char direct[PRINTF_BUFFER_SIZE_ASPRINTF]; -}; - void __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *buf) { @@ -90,23 +78,32 @@ __printf_buffer_flush_asprintf (struct __printf_buffer_asprintf *buf) buf->base.write_end = new_buffer + new_size; } +void +__printf_buffer_asprintf_init (struct __printf_buffer_asprintf *buf) +{ + __printf_buffer_init (&buf->base, buf->direct, array_length (buf->direct), + __printf_buffer_mode_asprintf); +} + +int +__printf_buffer_asprintf_free (struct __printf_buffer_asprintf *buf) +{ + if (buf->base.write_base != buf->direct) + free (buf->base.write_base); + return -1; +} int __vasprintf_internal (char **result_ptr, const char *format, va_list args, unsigned int mode_flags) { struct __printf_buffer_asprintf buf; - __printf_buffer_init (&buf.base, buf.direct, array_length (buf.direct), - __printf_buffer_mode_asprintf); + __printf_buffer_asprintf_init (&buf); __vprintf_buffer (&buf.base, format, args, mode_flags); int done = __printf_buffer_done (&buf.base); if (done < 0) - { - if (buf.base.write_base != buf.direct) - free (buf.base.write_base); - return done; - } + return __printf_buffer_asprintf_free (&buf); /* Transfer to the final buffer. */ char *result; @@ -122,10 +119,7 @@ __vasprintf_internal (char **result_ptr, const char *format, va_list args, { result = realloc (buf.base.write_base, size + 1); if (result == NULL) - { - free (buf.base.write_base); - return -1; - } + return __printf_buffer_asprintf_free (&buf); } /* Add NUL termination. */ 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 From patchwork Fri Feb 9 15:25:51 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85528 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 C9F9D385800E for ; Fri, 9 Feb 2024 15:26:38 +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 86B2B385843B for ; Fri, 9 Feb 2024 15:25:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 86B2B385843B 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 86B2B385843B 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=1707492357; cv=none; b=TpO7Y1XxEMzJxtasz9sQ+gWB/J4gjQoOeqJOi+6vcEuWqKpiqn1QoVN/U8DGLA7nTi8cHjTz0mlrEVL9EC+6S9Gkdwc3sG1hq3zeccFS3P6/EjjlXUq62+urmGslMVndX55DAfe/tWWqp76n2lQEZiQCbz+9BUM03NZkY7UzcUM= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492357; c=relaxed/simple; bh=5ujDP55VMDAPYtfyFGavHOxt8YgflKUCg43euVTxtH4=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=lCdw8oojkDiV90KVp7ex4sACuB6aCMLgxIEv+Ac0UNTOSkHfFw2t4fgU2Md5AWdIftPso0i4EsUxo2T17wwCrog42gMYKvZcvGhahFKOChX7pyj6gxFR0EZYPEIrzRGDnFvi8CGl5E+cjN5e7unnxOAu7hg8NjDZaMxfaKvbLFc= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492355; 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=gEeT5PZ1acAiIpLF6pnZkj91Z2E+TKJY5xATbCxyFBQ=; b=FF8eULSDTdoeqjRzzh53i2lPI/OK6ituLsKykaeZbdX1XJjpwyxEwgWfbXzSRRpZR9DHvp QjBax+GAC3Mori7bXqttAJzAAcO6qZNX/7oX4ihXOiyXu1/409pLchrAQfaYgORZh+96FP 6qIMLSySa1kCFiusthR4k65sFH3XU3c= Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-248-LlvDX5FQM-OcqbaEST9cxA-1; Fri, 09 Feb 2024 10:25:54 -0500 X-MC-Unique: LlvDX5FQM-OcqbaEST9cxA-1 Received: from smtp.corp.redhat.com (int-mx08.intmail.prod.int.rdu2.redhat.com [10.11.54.8]) (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 A06E185A58B for ; Fri, 9 Feb 2024 15:25:53 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1D761C08EF7 for ; Fri, 9 Feb 2024 15:25:52 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 09/11] stdio-common: Allow skipping initial bytes in __printf_buffer for %n In-Reply-To: Message-ID: References: X-From-Line: fa1f6a475bd87ff4615ad71c5253257c21cc0816 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:25:51 +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.8 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-10.7 required=5.0 tests=BAYES_00, DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 Making the written field signed allows subtraction of a prefix count in case multiple writes happen to the same buffer. Reviewed-by: Adhemerval Zanella --- include/printf_buffer.h | 8 +++++--- stdio-common/tst-printf_buffer.c | 18 ++++++++++++++++++ 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/include/printf_buffer.h b/include/printf_buffer.h index a5483e8a97..2400371b94 100644 --- a/include/printf_buffer.h +++ b/include/printf_buffer.h @@ -92,8 +92,10 @@ struct __printf_buffer buffer). Potentially updated on flush. The actual number of written bytes also includes the unflushed-but-written buffer part, write_ptr - write_base. A 64-bit value is used to avoid - the need for overflow checks. */ - uint64_t written; + the need for intermediate overflow checks. Negative values can + be used to ignore leading parts of the buffer for %n computations + (for repeated __printf_buffer calls to the same buffer). */ + int64_t written; /* Identifies the flush callback. */ enum __printf_buffer_mode mode; @@ -225,7 +227,7 @@ struct __wprintf_buffer wchar_t *write_base; wchar_t *write_ptr; wchar_t *write_end; - uint64_t written; + int64_t written; enum __wprintf_buffer_mode mode; }; diff --git a/stdio-common/tst-printf_buffer.c b/stdio-common/tst-printf_buffer.c index d12da8c939..2a3ee511d0 100644 --- a/stdio-common/tst-printf_buffer.c +++ b/stdio-common/tst-printf_buffer.c @@ -132,6 +132,24 @@ do_test (void) TEST_VERIFY (!__printf_buffer_has_failed (&buf.base)); TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); } + + /* Test %n with initial skip. */ + { + struct __printf_buffer_asprintf buf; + __printf_buffer_asprintf_init (&buf); + __printf_buffer (&buf.base, "abc"); + buf.base.written = -2; + int n = -1; + __printf_buffer (&buf.base, "D%nEF", &n); + TEST_COMPARE (n, 2); /* Two characters were skipped. */ + 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_done (&buf.base), 4); /* Two skipped. */ + TEST_COMPARE (__printf_buffer_asprintf_free (&buf), -1); + } + return 0; } From patchwork Fri Feb 9 15:25:55 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85530 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 96B1D385800B for ; Fri, 9 Feb 2024 15:26:48 +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 64045385840E for ; Fri, 9 Feb 2024 15:26:00 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 64045385840E 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 64045385840E 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=1707492362; cv=none; b=s64cr6fOwPYCSGNeZpVtGKwPb1dEVk22LWYy9UuU+LPjjJWECSqwpPfm0gvBoMXxt5vjZK58UIBOx39n3lBqHVfXSEQLolTGWUNgOsdyEICHmIjYotvAA4mec9UOKH1ZOPUtkoXfnNcfBiO0h4gd/ouqIaGQ4nojsT/juo5EHSI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492362; c=relaxed/simple; bh=7s+dOT5f+NbpWsjKyFwoLuF0ADKqiYTSTTiEEmkeWqM=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=MGeXn00QWVN9Gcbw4qRZvjZq0nLYg9joWAIU0NEtDU53FjCJJ9FlhT/ZzXzO8xv8Sy8iEUpDnClENZiEMhQKOu4yjpbiNeX67nVLxQMzNNy+TJlY1ovtGWSnxK60ISDbctKFKhgNqqVqXwDoJC0LD5NtPHcZ/X/m7VjcgvKhYwU= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492360; 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=r83C3hQL7sdsTCWEBNG6C7vDS62UqREJfsPHEp6fvSI=; b=DW4FxgIHpej9Hpd1/srjvsQHoieujMrhFOAC0/iWSJDtGNMdBOGOBPfOFmbZxsR8B8d0nU KSGje5rB6za+bJKIxuhR938QrAwh956Netp7DbOUm9/KW4nQMEXJL9sWugXMV7trzLLE9P TGTYCBjrwOmmR1RiZZBmggreQkggLhc= 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-512-JCGqwgKzOlmEHDL7tY0d6g-1; Fri, 09 Feb 2024 10:25:58 -0500 X-MC-Unique: JCGqwgKzOlmEHDL7tY0d6g-1 Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.rdu2.redhat.com [10.11.54.2]) (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 A42603C1E9D0 for ; Fri, 9 Feb 2024 15:25:58 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 10B9140C9444 for ; Fri, 9 Feb 2024 15:25:57 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 10/11] stdio-common: Support large offsets with %lln In-Reply-To: Message-ID: References: X-From-Line: d25f0a580fde5721d594492cbb8a37d38b53c702 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:25:55 +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.2 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 Use of Xprintf_buffer_done is unnecessary because it performs overflow detection against int and sets errno. --- stdio-common/Makefile | 1 + stdio-common/tst-printf-large-n.c | 81 +++++++++++++++++++++++++++++ stdio-common/vfprintf-process-arg.c | 2 +- 3 files changed, 83 insertions(+), 1 deletion(-) create mode 100644 stdio-common/tst-printf-large-n.c diff --git a/stdio-common/Makefile b/stdio-common/Makefile index d610ed67e6..482d841f96 100644 --- a/stdio-common/Makefile +++ b/stdio-common/Makefile @@ -234,6 +234,7 @@ tests := \ tst-printf-fp-free \ tst-printf-fp-leak \ tst-printf-intn \ + tst-printf-large-n \ tst-printf-oct \ tst-printf-round \ tst-printfsz \ diff --git a/stdio-common/tst-printf-large-n.c b/stdio-common/tst-printf-large-n.c new file mode 100644 index 0000000000..4504a693d2 --- /dev/null +++ b/stdio-common/tst-printf-large-n.c @@ -0,0 +1,81 @@ +/* Test that %n can report values larger than INT_MAX. + 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 + +#include +#include +#include + +static int +do_test (void) +{ + enum { string_length = 1 << 28 }; + + FILE *fp = xfopen ("/dev/null", "w"); + + /* Byte-oriented streams. */ + { + struct support_blob_repeat repeat + = support_blob_repeat_allocate ("a", 1, string_length + 1); + char *string = repeat.start; + if (string == NULL) + FAIL_UNSUPPORTED ("cannot allocate large string"); + string[string_length] = '\0'; + + long long int n; + TEST_COMPARE (fprintf (fp, "%s %s %s %s %s %s %s %s %lln", + string, string, string, string, string, string, + string, string, &n), -1); + TEST_COMPARE (errno, EOVERFLOW); + TEST_COMPARE (n, 8 * (string_length + 1LL)); + TEST_VERIFY (n > INT_MAX); + + support_blob_repeat_free (&repeat); + } + + /* Wide-oriented streams. */ + { + struct support_blob_repeat repeat + = support_blob_repeat_allocate (L"a", sizeof (wchar_t), + string_length + 1); + wchar_t *string = repeat.start; + if (string == NULL) + FAIL_UNSUPPORTED ("cannot allocate large wide string"); + string[string_length] = '\0'; + + long long int n; + TEST_COMPARE (fwprintf (fp, L"%Ls %Ls %Ls %Ls %Ls %Ls %Ls %Ls %lln", + string, string, string, string, string, string, + string, string, &n), -1); + TEST_COMPARE (errno, EOVERFLOW); + TEST_COMPARE (n, 8 * (string_length + 1LL)); + TEST_VERIFY (n > INT_MAX); + + support_blob_repeat_free (&repeat); + } + + xfclose (fp); + + return 0; +} + +#include diff --git a/stdio-common/vfprintf-process-arg.c b/stdio-common/vfprintf-process-arg.c index af6b570ea9..dbc78d4942 100644 --- a/stdio-common/vfprintf-process-arg.c +++ b/stdio-common/vfprintf-process-arg.c @@ -337,7 +337,7 @@ LABEL (form_number): } /* Answer the count of characters written. */ void *ptrptr = process_arg_pointer (); - unsigned int written = Xprintf_buffer_done (buf); + uint64_t written = buf->written + (buf->write_ptr - buf->write_base); if (is_longlong) *(long long int *) ptrptr = written; else if (is_long_num) From patchwork Fri Feb 9 15:26:01 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Florian Weimer X-Patchwork-Id: 85533 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 1601B38582B9 for ; Fri, 9 Feb 2024 15:27:48 +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 2AA3638582A3 for ; Fri, 9 Feb 2024 15:26:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 2AA3638582A3 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 2AA3638582A3 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=1707492369; cv=none; b=oD/GWxO3yY/ZT96VaqbE4dDFdJ84/82JnFXfYzAbDUivtbJmNbJF/gdTBUzBwqRtsm2fGRq2NqXnbSZrzgrlXl5ZRB2n0Yfd9UHS+zJGj0mJfV8qTnj6RYsGl5Q0Tyn09kAXgXfyymxOGuqLKStyAYZdH3SVnwj8nbwe0gicRNE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707492369; c=relaxed/simple; bh=Z21CqlVFxRC2yZpaWXikI1+LZg8FkpQfQZCWsqbVPlI=; h=DKIM-Signature:From:To:Subject:Message-ID:Date:MIME-Version; b=xyLdAmJwY6SfrFpns2TvhgP+R9AhHIrTc+3YR3a6puNsQVMX+j1JgSmCmJHM73HGDxUGw6/Bf5ud/+rBaYYSTbOmC283p7P05QSz6jvs/rFkULUAyFng0qb7sr6AepzUTGkY05JVTyuAfRUtR5XqrlK/3yjbzfmgGyPTiervskg= ARC-Authentication-Results: i=1; server2.sourceware.org DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1707492366; 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=RnsV1jBuQmpN+F4yvTFeZy1CVilvDazUE3/mSf8AI5A=; b=DGmFVHGprpk3GTHq2xHlzFa2ANTLwV5aBtwQQUdy0xIUYda3/nbOz7BpKpFdWLJ0eUiMec nYQ5xVVES6O3OG3nGr0leKRNAW2TjrbJRdCPDoCgNwvV98YDsIF1FKiyaM+xZmS62MSL4W IjxQvo1zaWlVy12goBLzQmoozUVk55A= 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-692-Q-_xw3UAMLKBg21Kj3RZKg-1; Fri, 09 Feb 2024 10:26:04 -0500 X-MC-Unique: Q-_xw3UAMLKBg21Kj3RZKg-1 Received: from smtp.corp.redhat.com (int-mx09.intmail.prod.int.rdu2.redhat.com [10.11.54.9]) (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 4BF5428B6A2C for ; Fri, 9 Feb 2024 15:26:04 +0000 (UTC) Received: from oldenburg.str.redhat.com (unknown [10.39.192.58]) by smtp.corp.redhat.com (Postfix) with ESMTPS id AB52F492BC6 for ; Fri, 9 Feb 2024 15:26:02 +0000 (UTC) From: Florian Weimer To: libc-alpha@sourceware.org Subject: [PATCH 11/11] syslog: Use a printf buffer directly to construct the entire packet In-Reply-To: Message-ID: References: X-From-Line: a709cb1c36c0e92954ebb036adabc350428ee7d5 Mon Sep 17 00:00:00 2001 Date: Fri, 09 Feb 2024 16:26:01 +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.9 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, 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 This defers buffer management largely to the asprintf implementation. It is quite close to the original implementation around open_memstream, except that an on-stack buffer is used for shorter messages, and that strftime no longer writes directly into the buffer. The new version no longer uses the (slow) %n format specifier. It also fixes an issue in the localtime_r failure path, where the message is prefixed with ": " due to an incorrect placement of the %n specifier. --- misc/syslog.c | 184 ++++++++++++++++---------------------------------- 1 file changed, 58 insertions(+), 126 deletions(-) diff --git a/misc/syslog.c b/misc/syslog.c index 68ee3aef5f..50cb252f47 100644 --- a/misc/syslog.c +++ b/misc/syslog.c @@ -38,13 +38,11 @@ static char sccsid[] = "@(#)syslog.c 8.4 (Berkeley) 3/18/94"; #include #include #include -#include -#include #include -#include #include #include #include +#include static int LogType = SOCK_DGRAM; /* type of socket connection */ static int LogFile = -1; /* fd for log */ @@ -60,27 +58,23 @@ __libc_lock_define_initialized (static, syslog_lock) static void openlog_internal (const char *, int, int); static void closelog_internal (void); -struct cleanup_arg +/* Unlock and deallocate the buffer. */ +static void +cancel_handler_buf (void *ptr) { - void *buf; - struct sigaction *oldaction; -}; + __libc_lock_unlock (syslog_lock); + + struct __printf_buffer_asprintf *buf = ptr; + __printf_buffer_asprintf_free (buf); +} +/* Only unlock. */ static void cancel_handler (void *ptr) { - /* Restore the old signal handler. */ - struct cleanup_arg *clarg = (struct cleanup_arg *) ptr; - - if (clarg != NULL) - /* Free the memstream buffer, */ - free (clarg->buf); - - /* Free the lock. */ __libc_lock_unlock (syslog_lock); } - /* * syslog, vsyslog -- * print message on log file; output is intended for syslogd(8). @@ -122,16 +116,21 @@ __vsyslog_chk (int pri, int flag, const char *fmt, va_list ap) __vsyslog_internal (pri, fmt, ap, (flag > 0) ? PRINTF_FORTIFY : 0); } +/* Send the buffer contents over the syslog socket. */ +static ssize_t +__syslog_send (struct __printf_buffer *buf) +{ + /* Send the trailing NUL byte for LogType == SOCK_STREAM only. */ + return __send (LogFile, buf->write_base, + buf->write_ptr - buf->write_base - (LogType != SOCK_STREAM), + MSG_NOSIGNAL); +} + void __vsyslog_internal (int pri, const char *fmt, va_list ap, unsigned int mode_flags) { - /* Try to use a static buffer as an optimization. */ - char bufs[1024]; - char *buf = bufs; - size_t bufsize; - - int msgoff; + ptrdiff_t msgoff; int saved_errno = errno; #define INTERNALLOG LOG_ERR|LOG_CONS|LOG_PERROR|LOG_PID @@ -144,8 +143,9 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, /* Prepare for multiple users. We have to take care: most syscalls we are using are cancellation points. */ - struct cleanup_arg clarg = { NULL, NULL }; - __libc_cleanup_push (cancel_handler, &clarg); + struct __printf_buffer_asprintf buf; + __printf_buffer_asprintf_init (&buf); + __libc_cleanup_push (cancel_handler_buf, &buf); __libc_lock_lock (syslog_lock); /* Check priority against setlogmask values. */ @@ -173,122 +173,56 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, __strftime_l (timestamp, sizeof timestamp, "%h %e %T ", now_tmp, _nl_C_locobj_ptr); -#define SYSLOG_HEADER(__pri, __timestamp, __msgoff, pid) \ - "<%d>%s%n%s%s%.0d%s: ", \ - __pri, __timestamp, __msgoff, \ - LogTag == NULL ? __progname : LogTag, \ - "[" + (pid == 0), pid, "]" + (pid == 0) + __printf_buffer (&buf.base, "<%d>", pri); -#define SYSLOG_HEADER_WITHOUT_TS(__pri, __msgoff) \ - "<%d>: %n", __pri, __msgoff - - int l, vl; if (has_ts) - l = __snprintf (bufs, sizeof bufs, - SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); - else - l = __snprintf (bufs, sizeof bufs, - SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff)); - if (l < 0) - goto out; - - char *pos; - size_t len; - - if (l < sizeof bufs) { - /* At this point, there is still a chance that we can print the - remaining part of the log into bufs and use that. */ - pos = bufs + l; - len = sizeof (bufs) - l; + __printf_buffer_puts (&buf.base, timestamp); + msgoff = buf.base.write_ptr - buf.base.write_base; + __printf_buffer_puts (&buf.base, LogTag == NULL ? __progname : LogTag); + if (pid != 0) + __printf_buffer (&buf.base, "[%d]", pid); + __printf_buffer_putc (&buf.base, ':'); + __printf_buffer_putc (&buf.base, ' '); } else { - buf = NULL; - /* We already know that bufs is too small to use for this log message. - The next vsnprintf into bufs is used only to calculate the total - required buffer length. We will discard bufs contents and allocate - an appropriately sized buffer later instead. */ - pos = bufs; - len = sizeof (bufs); + msgoff = buf.base.write_ptr - buf.base.write_base; + __printf_buffer_putc (&buf.base, ':'); + __printf_buffer_putc (&buf.base, ' '); } - { - va_list apc; - va_copy (apc, ap); - - /* Restore errno for %m format. */ - __set_errno (saved_errno); - - vl = __vsnprintf_internal (pos, len, fmt, apc, mode_flags); - va_end (apc); + /* Do not count the bytes written to the buffer so far. This value + is negative and accounts for the existing buffer contents, which + is not upposed to be visible to the caller. */ + buf.base.written = buf.base.write_base - buf.base.write_ptr; - if (vl < 0 || vl >= INT_MAX - l) - goto out; + errno = saved_errno; + __vprintf_buffer (&buf.base, fmt, ap, mode_flags); - if (vl >= len) - buf = NULL; - - bufsize = l + vl; - } - - if (buf == NULL) - { - buf = malloc ((bufsize + 1) * sizeof (char)); - if (buf != NULL) - { - /* Tell the cancellation handler to free this buffer. */ - clarg.buf = buf; + /* We may need the terminator if we are sending over a SOCK_STREAM + connection. Add this unconditionally to simplify error handling. */ + __printf_buffer_putc (&buf.base, '\0'); - int cl; - if (has_ts) - cl = __snprintf (buf, l + 1, - SYSLOG_HEADER (pri, timestamp, &msgoff, pid)); - else - cl = __snprintf (buf, l + 1, - SYSLOG_HEADER_WITHOUT_TS (pri, &msgoff)); - if (cl != l) - goto out; - - va_list apc; - va_copy (apc, ap); - cl = __vsnprintf_internal (buf + l, bufsize - l + 1, fmt, apc, - mode_flags); - va_end (apc); - - if (cl != vl) - goto out; - } - else - { - int bl; - /* Nothing much to do but emit an error message. */ - bl = __snprintf (bufs, sizeof bufs, - "out of memory[%d]", __getpid ()); - if (bl < 0 || bl >= sizeof bufs) - goto out; - - bufsize = bl; - buf = bufs; - msgoff = 0; - } - } + if (__printf_buffer_has_failed (&buf.base)) + goto out; /* Output to stderr if requested. */ if (LogStat & LOG_PERROR) - __dprintf (STDERR_FILENO, "%s%s", buf + msgoff, - "\n" + (buf[bufsize - 1] == '\n')); + { + /* The buffer always starts with '<' and a digit, so there are + at least two bytes, and -2 is valid offset from the next + to-be-written byte. -2 accounts for the NUL byte. */ + int has_nl = buf.base.write_ptr[-2] == '\n'; + __dprintf (STDERR_FILENO, "%s%s", buf.base.write_base + msgoff, + "\n" + has_nl); + } /* Get connected, output the message to the local logger. */ if (!connected) openlog_internal (NULL, LogStat | LOG_NDELAY, LogFacility); - /* If we have a SOCK_STREAM connection, also send ASCII NUL as a record - terminator. */ - if (LogType == SOCK_STREAM) - ++bufsize; - - if (!connected || __send (LogFile, buf, bufsize, MSG_NOSIGNAL) < 0) + if (!connected || __syslog_send (&buf.base) < 0) { if (connected) { @@ -297,7 +231,7 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, openlog_internal (NULL, LogStat | LOG_NDELAY, LogFacility); } - if (!connected || __send (LogFile, buf, bufsize, MSG_NOSIGNAL) < 0) + if (!connected || __syslog_send (&buf.base) < 0) { closelog_internal (); /* attempt re-open next time */ /* @@ -311,7 +245,7 @@ __vsyslog_internal (int pri, const char *fmt, va_list ap, (fd = __open (_PATH_CONSOLE, O_WRONLY | O_NOCTTY | O_CLOEXEC, 0)) >= 0) { - __dprintf (fd, "%s\r\n", buf + msgoff); + __dprintf (fd, "%s\r\n", buf.base.write_base + msgoff); __close (fd); } } @@ -321,9 +255,7 @@ out: /* End of critical section. */ __libc_cleanup_pop (0); __libc_lock_unlock (syslog_lock); - - if (buf != bufs) - free (buf); + __printf_buffer_asprintf_free (&buf); } /* AF_UNIX address of local logger */