From patchwork Fri Jun 2 15:28:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 1789738 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=f8UMX7wH; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QXn2v24jZz20Tj for ; Sat, 3 Jun 2023 01:28:39 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3E1BB38558BE for ; Fri, 2 Jun 2023 15:28:37 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3E1BB38558BE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685719717; bh=LQxWXJ0XKXiVkL9P0+j+v3GiKKtxH1t/X7nvLxiUgp4=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=f8UMX7wHDUH1/MQgNbmQhE5sPkXEXHfGrbW4QSYUS5Baxjj6d10ToBSKZiV2Mp7Bf Cuvxy6H/nwktiiu3+9D1Eo7L/QP4wH2L1y8lIU7TVj3ZbuCbpAGtaW0FNfvojf5kdH dY1A7p8CP8Za39eZJeKyHwYGxxNmsfoY/CCE9ACE= 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 280773858D3C for ; Fri, 2 Jun 2023 15:28:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 280773858D3C Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-388-KCbPqv44MLqaFYhjwqoCPQ-1; Fri, 02 Jun 2023 11:28:17 -0400 X-MC-Unique: KCbPqv44MLqaFYhjwqoCPQ-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 14647801585; Fri, 2 Jun 2023 15:28:17 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.193.180]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 51F3940CFD45; Fri, 2 Jun 2023 15:28:16 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v6 1/7] tests: fix warn unused result on asprintf calls Date: Fri, 2 Jun 2023 17:28:06 +0200 Message-Id: <20230602152812.108497-2-fberat@redhat.com> In-Reply-To: <20230602152812.108497-1-fberat@redhat.com> References: <20230602152812.108497-1-fberat@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.2 required=5.0 tests=BAYES_00, DKIM_INVALID, DKIM_SIGNED, GIT_PATCH_0, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Fr=C3=A9d=C3=A9ric_B=C3=A9rat_via_Libc-alpha?= From: Frederic Berat Reply-To: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" When enabling _FORTIFY_SOURCE, some functions now lead to warnings when their result is not checked. Reviewed-by: Siddhesh Poyarekar --- Changes since v5: - Removed unnecessary check after xasprintf call argp/argp-test.c | 8 +++++--- posix/tst-execl2.c | 8 ++------ posix/tst-execle2.c | 8 ++------ posix/tst-execlp2.c | 17 ++++------------- posix/tst-execv2.c | 8 ++------ posix/tst-execve2.c | 8 ++------ posix/tst-execvp2.c | 17 ++++------------- stdio-common/bug5.c | 4 +++- stdio-common/test-fwrite.c | 4 +++- stdio-common/tst-fseek.c | 5 ++--- 10 files changed, 29 insertions(+), 58 deletions(-) diff --git a/argp/argp-test.c b/argp/argp-test.c index c7e20f6235..cd69c81b1a 100644 --- a/argp/argp-test.c +++ b/argp/argp-test.c @@ -25,6 +25,8 @@ #include #include +#include + const char *argp_program_version = "argp-test 1.0"; struct argp_option sub_options[] = @@ -178,12 +180,12 @@ help_filter (int key, const char *text, void *input) if (key == ARGP_KEY_HELP_POST_DOC && text) { time_t now = time (0); - asprintf (&new_text, text, ctime (&now)); + new_text = xasprintf (text, ctime (&now)); } else if (key == 'f') /* Show the default for the --foonly option. */ - asprintf (&new_text, "%s (ZOT defaults to %x)", - text, params->foonly_default); + new_text = xasprintf ("%s (ZOT defaults to %x)", + text, params->foonly_default); else new_text = (char *)text; diff --git a/posix/tst-execl2.c b/posix/tst-execl2.c index 5b74959ef8..d2f4453ad8 100644 --- a/posix/tst-execl2.c +++ b/posix/tst-execl2.c @@ -18,12 +18,8 @@ prepare (int argc, char *argv[]) { char *buf; int off; - asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]); - if (buf == NULL) - { - puts ("asprintf failed"); - exit (1); - } + + buf = xasprintf ("cp %s %n%s-copy", argv[0], &off, argv[0]); if (system (buf) != 0) { puts ("system failed"); diff --git a/posix/tst-execle2.c b/posix/tst-execle2.c index 0430b7b573..8e3ad9acb6 100644 --- a/posix/tst-execle2.c +++ b/posix/tst-execle2.c @@ -18,12 +18,8 @@ prepare (int argc, char *argv[]) { char *buf; int off; - asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]); - if (buf == NULL) - { - puts ("asprintf failed"); - exit (1); - } + + buf = xasprintf ("cp %s %n%s-copy", argv[0], &off, argv[0]); if (system (buf) != 0) { puts ("system failed"); diff --git a/posix/tst-execlp2.c b/posix/tst-execlp2.c index 81a723dda4..4c3afb8539 100644 --- a/posix/tst-execlp2.c +++ b/posix/tst-execlp2.c @@ -22,12 +22,8 @@ prepare (int argc, char *argv[]) { char *buf; int off; - asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]); - if (buf == NULL) - { - puts ("asprintf failed"); - exit (1); - } + + buf = xasprintf ("cp %s %n%s-copy", argv[0], &off, argv[0]); if (system (buf) != 0) { puts ("system failed"); @@ -58,13 +54,8 @@ do_test (void) puts ("canonicalize_file_name failed"); return 1; } - char *path; - asprintf (&path, "%s:../libio:../elf", bindir); - if (path == NULL) - { - puts ("asprintf failed"); - return 1; - } + + char *path = xasprintf ("%s:../libio:../elf", bindir); setenv ("PATH", path, 1); diff --git a/posix/tst-execv2.c b/posix/tst-execv2.c index a5168a269c..5fd6c46c1f 100644 --- a/posix/tst-execv2.c +++ b/posix/tst-execv2.c @@ -18,12 +18,8 @@ prepare (int argc, char *argv[]) { char *buf; int off; - asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]); - if (buf == NULL) - { - puts ("asprintf failed"); - exit (1); - } + + buf = xasprintf ("cp %s %n%s-copy", argv[0], &off, argv[0]); if (system (buf) != 0) { puts ("system failed"); diff --git a/posix/tst-execve2.c b/posix/tst-execve2.c index 1a804e94fd..e0a7c84346 100644 --- a/posix/tst-execve2.c +++ b/posix/tst-execve2.c @@ -18,12 +18,8 @@ prepare (int argc, char *argv[]) { char *buf; int off; - asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]); - if (buf == NULL) - { - puts ("asprintf failed"); - exit (1); - } + + buf = xasprintf ("cp %s %n%s-copy", argv[0], &off, argv[0]); if (system (buf) != 0) { puts ("system failed"); diff --git a/posix/tst-execvp2.c b/posix/tst-execvp2.c index 440dfab438..f6c0cb4d98 100644 --- a/posix/tst-execvp2.c +++ b/posix/tst-execvp2.c @@ -25,12 +25,8 @@ prepare (int argc, char *argv[]) { char *buf; int off; - asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]); - if (buf == NULL) - { - puts ("asprintf failed"); - exit (1); - } + + buf = xasprintf ("cp %s %n%s-copy", argv[0], &off, argv[0]); if (system (buf) != 0) { puts ("system failed"); @@ -61,13 +57,8 @@ do_test (void) puts ("canonicalize_file_name failed"); return 1; } - char *path; - asprintf (&path, "%s:../libio:../elf", bindir); - if (path == NULL) - { - puts ("asprintf failed"); - return 1; - } + + char *path = xasprintf ("%s:../libio:../elf", bindir); setenv ("PATH", path, 1); diff --git a/stdio-common/bug5.c b/stdio-common/bug5.c index 7bfe9b2b8d..dfa19aed55 100644 --- a/stdio-common/bug5.c +++ b/stdio-common/bug5.c @@ -7,6 +7,8 @@ #include #include +#include + static char buf[8192]; int @@ -60,7 +62,7 @@ main (void) the perhaps incompatible new shared libraries. */ unsetenv ("LD_LIBRARY_PATH"); - asprintf (&printbuf, "cmp %s %s", inname, outname); + printbuf = xasprintf ("cmp %s %s", inname, outname); result = system (printbuf); remove (inname); remove (outname); diff --git a/stdio-common/test-fwrite.c b/stdio-common/test-fwrite.c index 5677c6da80..7f383921ca 100644 --- a/stdio-common/test-fwrite.c +++ b/stdio-common/test-fwrite.c @@ -1,6 +1,8 @@ #include #include +#include + static int do_test (void) { @@ -57,7 +59,7 @@ do_test (void) return 1; } - asprintf (&line, "\ + line = xasprintf ("\ GDB is free software and you are welcome to distribute copies of it\n\ under certain conditions; type \"show copying\" to see the conditions.\n\ There is absolutely no warranty for GDB; type \"show warranty\" for details.\n\ diff --git a/stdio-common/tst-fseek.c b/stdio-common/tst-fseek.c index c4ac17cdf4..386dd1dd51 100644 --- a/stdio-common/tst-fseek.c +++ b/stdio-common/tst-fseek.c @@ -25,6 +25,7 @@ #include #include +#include static int do_test (void) @@ -44,9 +45,7 @@ do_test (void) if (tmpdir == NULL || tmpdir[0] == '\0') tmpdir = "/tmp"; - asprintf (&fname, "%s/tst-fseek.XXXXXX", tmpdir); - if (fname == NULL) - error (EXIT_FAILURE, errno, "cannot generate name for temporary file"); + fname = xasprintf ("%s/tst-fseek.XXXXXX", tmpdir); /* Create a temporary file. */ fd = mkstemp (fname); From patchwork Fri Jun 2 15:28:07 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 1789742 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=gKevJCtp; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QXn3m6zSBz20Tj for ; Sat, 3 Jun 2023 01:29:24 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id EE46138532DE for ; Fri, 2 Jun 2023 15:29:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EE46138532DE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685719763; bh=DxvM60A3lTB0CEd2KmaRHlSkgwF1VtN5BKI3sdA7TPI=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=gKevJCtpRvFXIeLl0RzhlAm/lngKZT/jy5rC/jGUxgAEoeBWiLKGmrMAix0TR5if2 +3xSA89AOqP+8o0TSfU8Qy8mzISTOOMOA5M/VTVQ6fZU/35kidR03aGaE7uM2EONPy lsGYhyxjkZJ6un3C3xorNPemR5Yaepj6s66MFHPg= 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 8C37D3858C66 for ; Fri, 2 Jun 2023 15:28:19 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 8C37D3858C66 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-364-glDQpMf7PkqtfrVhivX0DA-1; Fri, 02 Jun 2023 11:28:18 -0400 X-MC-Unique: glDQpMf7PkqtfrVhivX0DA-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id E7EDB3C13A0E; Fri, 2 Jun 2023 15:28:17 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.193.180]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4F66D40CFD45; Fri, 2 Jun 2023 15:28:17 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v6 2/7] tests: replace read by xread Date: Fri, 2 Jun 2023 17:28:07 +0200 Message-Id: <20230602152812.108497-3-fberat@redhat.com> In-Reply-To: <20230602152812.108497-1-fberat@redhat.com> References: <20230602152812.108497-1-fberat@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Fr=C3=A9d=C3=A9ric_B=C3=A9rat_via_Libc-alpha?= From: Frederic Berat Reply-To: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" With fortification enabled, read calls return result needs to be checked, has it gets the __wur macro enabled. --- Changes since v4/v5: - Rebased dirent/tst-fdopendir.c | 3 ++- nptl/tst-cleanup4.c | 4 +++- support/Makefile | 1 + support/test-container.c | 3 ++- support/xread.c | 36 ++++++++++++++++++++++++++++++++++ support/xunistd.h | 3 +++ sysdeps/pthread/Makefile | 2 +- sysdeps/pthread/tst-cancel11.c | 4 +++- sysdeps/pthread/tst-cancel20.c | 10 +++------- sysdeps/pthread/tst-cancel21.c | 9 ++------- sysdeps/pthread/tst-fini1mod.c | 4 +++- 11 files changed, 59 insertions(+), 20 deletions(-) create mode 100644 support/xread.c diff --git a/dirent/tst-fdopendir.c b/dirent/tst-fdopendir.c index 2c9520574d..d6a24f47db 100644 --- a/dirent/tst-fdopendir.c +++ b/dirent/tst-fdopendir.c @@ -45,7 +45,8 @@ do_test (void) } char buf[5]; - read(fd, buf, sizeof (buf)); + xread(fd, buf, sizeof (buf)); + close(fd); struct stat64 st2; diff --git a/nptl/tst-cleanup4.c b/nptl/tst-cleanup4.c index 1d3d53fb5f..f2e9f263e5 100644 --- a/nptl/tst-cleanup4.c +++ b/nptl/tst-cleanup4.c @@ -21,6 +21,8 @@ #include #include +#include + /* LinuxThreads pthread_cleanup_{push,pop} helpers. */ extern void _pthread_cleanup_push (struct _pthread_cleanup_buffer *__buffer, void (*__routine) (void *), @@ -64,7 +66,7 @@ fn_read (void) } char c; - read (fds[0], &c, 1); + xread (fds[0], &c, 1); } diff --git a/support/Makefile b/support/Makefile index 130de4a985..e39001ef24 100644 --- a/support/Makefile +++ b/support/Makefile @@ -196,6 +196,7 @@ libsupport-routines = \ xpthread_spin_unlock \ xraise \ xreadlink \ + xread \ xrealloc \ xrecvfrom \ xsendto \ diff --git a/support/test-container.c b/support/test-container.c index 20ea19af37..788b091ea0 100644 --- a/support/test-container.c +++ b/support/test-container.c @@ -1217,7 +1217,8 @@ main (int argc, char **argv) /* Get our "outside" pid from our parent. We use this to help with debugging from outside the container. */ - read (pipes[0], &child, sizeof(child)); + xread (pipes[0], &child, sizeof(child)); + close (pipes[0]); close (pipes[1]); sprintf (pid_buf, "%lu", (long unsigned)child); diff --git a/support/xread.c b/support/xread.c new file mode 100644 index 0000000000..215f9b4f00 --- /dev/null +++ b/support/xread.c @@ -0,0 +1,36 @@ +/* read with error checking and retries. + Copyright (C) 2016-2023 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 + +void +xread (int fd, void *buffer, size_t length) +{ + char *p = buffer; + char *end = p + length; + while (p < end) + { + ssize_t ret = read (fd, p, end - p); + if (ret < 0) + FAIL_EXIT1 ("read of %zu bytes failed after %td: %m", + length, p - (char *) buffer); + p += ret; + } +} diff --git a/support/xunistd.h b/support/xunistd.h index 43a1e69fcb..0aa2638a8d 100644 --- a/support/xunistd.h +++ b/support/xunistd.h @@ -77,6 +77,9 @@ void xclose (int); /* Write the buffer. Retry on short writes. */ void xwrite (int, const void *, size_t); +/* Read to buffer. Retry on short reads. */ +void xread (int, void *, size_t); + /* Invoke mmap with a zero file offset. */ void *xmmap (void *addr, size_t length, int prot, int flags, int fd); void xmprotect (void *addr, size_t length, int prot); diff --git a/sysdeps/pthread/Makefile b/sysdeps/pthread/Makefile index 5df1109dd3..32cf4eb119 100644 --- a/sysdeps/pthread/Makefile +++ b/sysdeps/pthread/Makefile @@ -464,7 +464,7 @@ $(objpfx)tst-cancel28: $(librt) $(objpfx)tst-fini1: $(shared-thread-library) $(objpfx)tst-fini1mod.so -$(objpfx)tst-fini1mod.so: $(shared-thread-library) +$(objpfx)tst-fini1mod.so: $(libsupport) $(shared-thread-library) $(objpfx)tst-_res1mod2.so: $(objpfx)tst-_res1mod1.so LDFLAGS-tst-_res1mod1.so = -Wl,-soname,tst-_res1mod1.so diff --git a/sysdeps/pthread/tst-cancel11.c b/sysdeps/pthread/tst-cancel11.c index 4dd84d6673..449f3b9b63 100644 --- a/sysdeps/pthread/tst-cancel11.c +++ b/sysdeps/pthread/tst-cancel11.c @@ -22,6 +22,8 @@ #include #include +#include + static pthread_barrier_t bar; static int fd[2]; @@ -56,7 +58,7 @@ tf (void *arg) /* This call should block and be cancelable. */ char buf[20]; - read (fd[0], buf, sizeof (buf)); + xread (fd[0], buf, sizeof (buf)); pthread_cleanup_pop (0); diff --git a/sysdeps/pthread/tst-cancel20.c b/sysdeps/pthread/tst-cancel20.c index 1d5c53049b..0f1ada3742 100644 --- a/sysdeps/pthread/tst-cancel20.c +++ b/sysdeps/pthread/tst-cancel20.c @@ -22,6 +22,8 @@ #include #include +#include + static int fd[4]; static pthread_barrier_t b; @@ -43,11 +45,7 @@ sh_body (void) pthread_cleanup_push (cl, (void *) 1L); in_sh_body = 1; - if (read (fd[2], &c, 1) == 1) - { - puts ("read succeeded"); - exit (1); - } + xread (fd[2], &c, 1); pthread_cleanup_pop (0); } @@ -84,8 +82,6 @@ tf_body (void) exit (1); } - read (fd[0], &c, 1); - pthread_cleanup_pop (0); } diff --git a/sysdeps/pthread/tst-cancel21.c b/sysdeps/pthread/tst-cancel21.c index bc4ff308f9..c14ed37d14 100644 --- a/sysdeps/pthread/tst-cancel21.c +++ b/sysdeps/pthread/tst-cancel21.c @@ -23,6 +23,7 @@ #include #include +#include static int fd[4]; static pthread_barrier_t b; @@ -44,11 +45,7 @@ sh_body (void) pthread_cleanup_push (cl, (void *) 1L); in_sh_body = 1; - if (read (fd[2], &c, 1) == 1) - { - puts ("read succeeded"); - exit (1); - } + xread (fd[2], &c, 1); pthread_cleanup_pop (0); } @@ -85,8 +82,6 @@ tf_body (void) exit (1); } - read (fd[0], &c, 1); - pthread_cleanup_pop (0); } diff --git a/sysdeps/pthread/tst-fini1mod.c b/sysdeps/pthread/tst-fini1mod.c index cdadf034cd..0a45f6c5f2 100644 --- a/sysdeps/pthread/tst-fini1mod.c +++ b/sysdeps/pthread/tst-fini1mod.c @@ -20,6 +20,8 @@ #include #include +#include + static void * tf (void *arg) @@ -32,7 +34,7 @@ tf (void *arg) } char buf[10]; - read (fds[0], buf, sizeof (buf)); + xread (fds[0], buf, sizeof (buf)); puts ("read returned"); exit (1); From patchwork Fri Jun 2 15:28:08 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 1789743 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=pLqA+3uY; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QXn3q2nVGz20Tj for ; Sat, 3 Jun 2023 01:29:27 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 4E68A38532DE for ; Fri, 2 Jun 2023 15:29:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 4E68A38532DE DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685719765; bh=opEFG/RAJ8n+quNiElDLit785VlJ1W6PA6O97zsXMik=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=pLqA+3uY10UA72gPc9B2RwJb7lZ0pQMPLkxaBvZfF4qZwcOclF/L1/sG843dpeqdS yUJMr4nsq+YFsZS7VMCGtKhrx6jQWzqfcXFOD/5Wr+fOii7+1MabAaUhEI+HdNXct+ BDPKiu8/6VTZrqI/XOug21dSQX43TslJFS9r7oD0= 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 065613858415 for ; Fri, 2 Jun 2023 15:28:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 065613858415 Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-257-RjoxipgRMHmUmPY83GogQw-1; Fri, 02 Jun 2023 11:28:19 -0400 X-MC-Unique: RjoxipgRMHmUmPY83GogQw-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id C76663C13A0E; Fri, 2 Jun 2023 15:28:18 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.193.180]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2EE9640CFD45; Fri, 2 Jun 2023 15:28:18 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v6 3/7] tests: replace system by xsystem Date: Fri, 2 Jun 2023 17:28:08 +0200 Message-Id: <20230602152812.108497-4-fberat@redhat.com> In-Reply-To: <20230602152812.108497-1-fberat@redhat.com> References: <20230602152812.108497-1-fberat@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.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, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Fr=C3=A9d=C3=A9ric_B=C3=A9rat_via_Libc-alpha?= From: Frederic Berat Reply-To: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" With fortification enabled, system calls return result needs to be checked, has it gets the __wur macro enabled. --- Changes since v5: - Fixed missing headers in {nptl,elf}/tst-stackguard1.c - Fixed date in copyrights elf/tst-stackguard1.c | 5 ++++- libio/bug-mmap-fflush.c | 7 +++++-- nptl/tst-cancel7.c | 3 ++- nptl/tst-stackguard1.c | 4 +++- nss/tst-nss-db-endpwent.c | 3 ++- support/Makefile | 1 + support/xstdlib.h | 31 +++++++++++++++++++++++++++++++ support/xsystem.c | 37 +++++++++++++++++++++++++++++++++++++ 8 files changed, 85 insertions(+), 6 deletions(-) create mode 100644 support/xstdlib.h create mode 100644 support/xsystem.c diff --git a/elf/tst-stackguard1.c b/elf/tst-stackguard1.c index 2e65e36078..e03b242e7e 100644 --- a/elf/tst-stackguard1.c +++ b/elf/tst-stackguard1.c @@ -26,6 +26,8 @@ #include #include +#include + static const char *command; static bool child; static uintptr_t stack_chk_guard_copy; @@ -108,7 +110,8 @@ do_test (void) dup2 (fds[1], 2); close (fds[1]); - system (command); + xsystem (command); + exit (0); } diff --git a/libio/bug-mmap-fflush.c b/libio/bug-mmap-fflush.c index d8aa58985a..3f99222eef 100644 --- a/libio/bug-mmap-fflush.c +++ b/libio/bug-mmap-fflush.c @@ -4,6 +4,7 @@ #include #include +#include static char *fname; @@ -35,14 +36,16 @@ do_test (void) char buffer[1024]; snprintf (buffer, sizeof (buffer), "echo 'From foo@bar.com' > %s", fname); - system (buffer); + xsystem (buffer); + f = fopen (fname, "r"); fseek (f, 0, SEEK_END); o = ftello (f); fseek (f, 0, SEEK_SET); fflush (f); snprintf (buffer, sizeof (buffer), "echo 'From bar@baz.edu' >> %s", fname); - system (buffer); + xsystem (buffer); + fseek (f, o, SEEK_SET); if (fgets (buffer, 1024, f) == NULL) exit (1); diff --git a/nptl/tst-cancel7.c b/nptl/tst-cancel7.c index 2835613a9b..0ecb7d025f 100644 --- a/nptl/tst-cancel7.c +++ b/nptl/tst-cancel7.c @@ -27,6 +27,7 @@ #include #include #include +#include #include #include @@ -43,7 +44,7 @@ tf (void *arg) { char *cmd = xasprintf ("%s --direct --sem %s --pidfile %s", command, semfilename, pidfilename); - system (cmd); + xsystem (cmd); /* This call should never return. */ return NULL; } diff --git a/nptl/tst-stackguard1.c b/nptl/tst-stackguard1.c index 4ac57157e9..7308b9d37a 100644 --- a/nptl/tst-stackguard1.c +++ b/nptl/tst-stackguard1.c @@ -28,6 +28,7 @@ #include #include +#include static const char *command; static bool child; @@ -140,7 +141,8 @@ do_test (void) dup2 (fds[1], 2); close (fds[1]); - system (command); + xsystem (command); + exit (0); } diff --git a/nss/tst-nss-db-endpwent.c b/nss/tst-nss-db-endpwent.c index 2b0fc1b064..4dba3fada6 100644 --- a/nss/tst-nss-db-endpwent.c +++ b/nss/tst-nss-db-endpwent.c @@ -23,6 +23,7 @@ #include #include +#include /* It is entirely allowed to start with a getpwent call without resetting the state of the service via a call to setpwent. @@ -55,7 +56,7 @@ do_test (void) cmd = xasprintf ("%s/makedb -o /var/db/passwd.db /var/db/passwd.in", support_bindir_prefix); - system (cmd); + xsystem (cmd); free (cmd); try_it (); diff --git a/support/Makefile b/support/Makefile index e39001ef24..fbbc305423 100644 --- a/support/Makefile +++ b/support/Makefile @@ -209,6 +209,7 @@ libsupport-routines = \ xstrdup \ xstrndup \ xsymlink \ + xsystem \ xsysconf \ xunlink \ xuselocale \ diff --git a/support/xstdlib.h b/support/xstdlib.h new file mode 100644 index 0000000000..db5a5b9d4f --- /dev/null +++ b/support/xstdlib.h @@ -0,0 +1,31 @@ +/* Error-checking wrappers for stdlib functions. + Copyright (C) 2023 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 + . */ + +#ifndef SUPPORT_XSTDLIB_H +#define SUPPORT_XSTDLIB_H + +#include +#include + +__BEGIN_DECLS + +void xsystem (const char *cmd); + +__END_DECLS + +#endif /* SUPPORT_XSTDLIB_H */ diff --git a/support/xsystem.c b/support/xsystem.c new file mode 100644 index 0000000000..1f558953bc --- /dev/null +++ b/support/xsystem.c @@ -0,0 +1,37 @@ +/* Error-checking replacement for "system". + Copyright (C) 2023 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 +xsystem (const char *cmd) +{ + int ret = system (cmd); + + if (ret == 0 && cmd == NULL) + FAIL_EXIT1 ("Unable to spawn a shell for NULL command"); + + if (ret == 127) + FAIL_EXIT1 ("Child terminated with status 127"); + + if (ret < 0) + FAIL_EXIT1 ("system (\"%s\")", cmd); +} From patchwork Fri Jun 2 15:28:09 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 1789744 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=aHvG37ra; dkim-atps=neutral Received: from sourceware.org (ip-8-43-85-97.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QXn4b206dz20Py for ; Sat, 3 Jun 2023 01:30:07 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 1C6193850857 for ; Fri, 2 Jun 2023 15:30:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 1C6193850857 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685719805; bh=I3bV7wtLlHrA0wGYSfY+x6NEZWz9hq1/xaC2G2nNHyw=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=aHvG37raxmklinGFHzYUY232+DvB+cXFjaG95qaap2BcJNb44NUcbxOUwaRPVdvH+ 95erlrGYzdMWTYdlUaijO+F24PrZ8uUtdukAXGuiF8MJHydmBV0vfIyfbpqRvj8r94 186r44vUdjqC3OJL5iaHC47fVD4mmvC4ZNhcg6ts= 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 D1DB73858439 for ; Fri, 2 Jun 2023 15:28:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D1DB73858439 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-508-gE_l8Hm2PLyHJsq3LX48Cg-1; Fri, 02 Jun 2023 11:28:20 -0400 X-MC-Unique: gE_l8Hm2PLyHJsq3LX48Cg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id A65618002BF; Fri, 2 Jun 2023 15:28:19 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.193.180]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0E02340CFD46; Fri, 2 Jun 2023 15:28:18 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v6 4/7] tests: replace fread by xfread Date: Fri, 2 Jun 2023 17:28:09 +0200 Message-Id: <20230602152812.108497-5-fberat@redhat.com> In-Reply-To: <20230602152812.108497-1-fberat@redhat.com> References: <20230602152812.108497-1-fberat@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Fr=C3=A9d=C3=A9ric_B=C3=A9rat_via_Libc-alpha?= From: Frederic Berat Reply-To: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" With fortification enabled, fread calls return result needs to be checked, has it gets the __wur macro enabled. --- Changes since v4/v5: - Rebased libio/bug-fseek.c | 7 ++++--- stdio-common/bug12.c | 12 ++++++----- stdio-common/bug3.c | 4 +++- stdio-common/bug4.c | 4 +++- stdio-common/tst-cookie.c | 5 ++++- stdio-common/tst-fmemopen3.c | 4 +++- support/Makefile | 1 + support/xfread.c | 39 ++++++++++++++++++++++++++++++++++++ support/xstdio.h | 1 + 9 files changed, 65 insertions(+), 12 deletions(-) create mode 100644 support/xfread.c diff --git a/libio/bug-fseek.c b/libio/bug-fseek.c index 1b60580b53..19d5e2429e 100644 --- a/libio/bug-fseek.c +++ b/libio/bug-fseek.c @@ -3,6 +3,7 @@ #include #include +#include static char *fname; @@ -48,7 +49,7 @@ do_test (void) perror ("fopen(\"r\")"); } - fread (buf, 3, 1, f); + xfread (buf, 3, 1, f); errno = 0; if (fseek (f, -10, SEEK_CUR) == 0) { @@ -72,7 +73,7 @@ Got %d instead\n", perror ("fopen(\"r+\")"); } - fread (buf, 3, 1, f); + xfread (buf, 3, 1, f); errno = 0; if (fseek (f, -10, SEEK_CUR) == 0) { @@ -96,7 +97,7 @@ Got %d instead\n", perror ("fopen(\"r+\")"); } - fread (buf, 3, 1, f); + xfread (buf, 3, 1, f); if (ftell (f) != 3) { puts ("ftell failed"); diff --git a/stdio-common/bug12.c b/stdio-common/bug12.c index 48610c0e78..1ba296deb4 100644 --- a/stdio-common/bug12.c +++ b/stdio-common/bug12.c @@ -1,6 +1,8 @@ #include #include +#include + char x[4096], z[4096], b[21], m[4096 * 4]; int @@ -20,24 +22,24 @@ main (void) } rewind (f); - fread (m, 4096 * 4 - 10, 1, f); - fread (b, 20, 1, f); + xfread (m, 4096 * 4 - 10, 1, f); + xfread (b, 20, 1, f); printf ("got %s (should be %s)\n", b, "zzzzzzzzzzxxxxxxxxxx"); if (strcmp (b, "zzzzzzzzzzxxxxxxxxxx")) failed = 1; fseek (f, -40, SEEK_CUR); - fread (b, 20, 1, f); + xfread (b, 20, 1, f); printf ("got %s (should be %s)\n", b, "zzzzzzzzzzzzzzzzzzzz"); if (strcmp (b, "zzzzzzzzzzzzzzzzzzzz")) failed = 1; - fread (b, 20, 1, f); + xfread (b, 20, 1, f); printf ("got %s (should be %s)\n", b, "zzzzzzzzzzxxxxxxxxxx"); if (strcmp (b, "zzzzzzzzzzxxxxxxxxxx")) failed = 1; - fread (b, 20, 1, f); + xfread (b, 20, 1, f); printf ("got %s (should be %s)\n", b, "xxxxxxxxxxxxxxxxxxxx"); if (strcmp (b, "xxxxxxxxxxxxxxxxxxxx")) failed = 1; diff --git a/stdio-common/bug3.c b/stdio-common/bug3.c index 62a6cab330..deabd00572 100644 --- a/stdio-common/bug3.c +++ b/stdio-common/bug3.c @@ -1,6 +1,8 @@ #include #include +#include + int main (void) { @@ -32,7 +34,7 @@ main (void) char buf[25]; buf[0] = j; - fread (buf + 1, 1, 23, f); + xfread (buf + 1, 1, 23, f); buf[24] = '\0'; if (strcmp (buf, "Where does this text go?") != 0) { diff --git a/stdio-common/bug4.c b/stdio-common/bug4.c index cf7fe116eb..4059ff75b3 100644 --- a/stdio-common/bug4.c +++ b/stdio-common/bug4.c @@ -2,6 +2,8 @@ #include #include +#include + int stdio_block_read = 1, stdio_block_write = 1; int @@ -30,7 +32,7 @@ main (int argc, char *argv[]) fseek (f, 8180L, 0); fwrite ("Where does this text come from?", 1, 31, f); fseek (f, 8180L, 0); - fread (buffer, 1, 31, f); + xfread (buffer, 1, 31, f); fwrite (buffer, 1, 31, stdout); fclose (f); remove (filename); diff --git a/stdio-common/tst-cookie.c b/stdio-common/tst-cookie.c index 030e684562..90ebc8e58c 100644 --- a/stdio-common/tst-cookie.c +++ b/stdio-common/tst-cookie.c @@ -5,6 +5,8 @@ #include +#include + #define THE_COOKIE ((void *) 0xdeadbeeful) @@ -77,7 +79,8 @@ do_test (void) f = fopencookie (THE_COOKIE, "r+", fcts); - fread (buf, 1, 1, f); + xfread (buf, 1, 1, f); + fwrite (buf, 1, 1, f); fseek (f, 0, SEEK_CUR); fclose (f); diff --git a/stdio-common/tst-fmemopen3.c b/stdio-common/tst-fmemopen3.c index bef87b712a..1627f17f59 100644 --- a/stdio-common/tst-fmemopen3.c +++ b/stdio-common/tst-fmemopen3.c @@ -21,6 +21,8 @@ #include #include +#include + static void print_buffer (const char *s, size_t n) { @@ -153,7 +155,7 @@ do_test_read_seek_neg (const char *mode, const char *expected) FILE *fp = fmemopen (buf, sizeof (buf), mode); fseek (fp, offset, SEEK_END); - fread (tmp, tmps, 1, fp); + xfread (tmp, tmps, 1, fp); if (memcmp (tmp, expected, tmps) != 0) { diff --git a/support/Makefile b/support/Makefile index fbbc305423..3eed2deb1a 100644 --- a/support/Makefile +++ b/support/Makefile @@ -126,6 +126,7 @@ libsupport-routines = \ xfopen \ xfork \ xfreopen \ + xfread \ xftruncate \ xgetline \ xgetsockname \ diff --git a/support/xfread.c b/support/xfread.c new file mode 100644 index 0000000000..c21187d476 --- /dev/null +++ b/support/xfread.c @@ -0,0 +1,39 @@ +/* fread with error checking. + Copyright (C) 2016-2023 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 +xfread (void *ptr, size_t size, size_t nmemb, FILE *stream) +{ + size_t count = 0; + char *p = ptr; + + while (count < nmemb) + { + size_t ret = fread (p, size, nmemb - count, stream); + if (ret <= 0 && ferror(stream)) + FAIL_EXIT1 ("read of %zu bytes failed after %td: %m", + size * nmemb, p - (char *) ptr); + count += ret; + p += size * ret; + } +} diff --git a/support/xstdio.h b/support/xstdio.h index 5410d42579..633c342c82 100644 --- a/support/xstdio.h +++ b/support/xstdio.h @@ -27,6 +27,7 @@ __BEGIN_DECLS FILE *xfopen (const char *path, const char *mode); void xfclose (FILE *); FILE *xfreopen (const char *path, const char *mode, FILE *stream); +void xfread (void *ptr, size_t size, size_t nmemb, FILE *stream); /* Read a line from FP, using getline. *BUFFER must be NULL, or a heap-allocated pointer of *LENGTH bytes. Return the number of From patchwork Fri Jun 2 15:28:10 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 1789741 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=bM1ebugC; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QXn3B19Fnz20Tj for ; Sat, 3 Jun 2023 01:28:54 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id E45C33854839 for ; Fri, 2 Jun 2023 15:28:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org E45C33854839 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685719731; bh=eG/G5D67LI5tgTIciwMtRmJnu2/GMKyDXku5amjDD14=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=bM1ebugCwLcbySgVOoQMoRL3DIhBXREKRb748VaXFKHx7HkAcVeTVXcSVnwHBXVoa wRNGvy9qo/W3jHbIQHw1R0oRW+Xj9HJnRXQw5xVgG/RtVfYgZAeqC4m6MckiETB7ku WX5UU58Ma7XYrZk5l1QI4yGj7T2+t7ehsy435lbg= 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 4E3CD385770C for ; Fri, 2 Jun 2023 15:28:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4E3CD385770C Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-401-_c58uqBwMRaLcpTN2tEr8w-1; Fri, 02 Jun 2023 11:28:20 -0400 X-MC-Unique: _c58uqBwMRaLcpTN2tEr8w-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 83FBF85A5AA; Fri, 2 Jun 2023 15:28:20 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.193.180]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E021740CFD45; Fri, 2 Jun 2023 15:28:19 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v6 5/7] tests: replace fgets by xfgets Date: Fri, 2 Jun 2023 17:28:10 +0200 Message-Id: <20230602152812.108497-6-fberat@redhat.com> In-Reply-To: <20230602152812.108497-1-fberat@redhat.com> References: <20230602152812.108497-1-fberat@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Fr=C3=A9d=C3=A9ric_B=C3=A9rat_via_Libc-alpha?= From: Frederic Berat Reply-To: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" With fortification enabled, fgets calls return result needs to be checked, has it gets the __wur macro enabled. --- Changes since v4/v5: - Rebased assert/test-assert-perr.c | 8 +++++--- assert/test-assert.c | 8 +++++--- stdio-common/test_rdwr.c | 11 ++++------- support/Makefile | 1 + support/xfgets.c | 32 ++++++++++++++++++++++++++++++++ support/xstdio.h | 1 + sysdeps/pthread/tst-cancel6.c | 3 ++- 7 files changed, 50 insertions(+), 14 deletions(-) create mode 100644 support/xfgets.c diff --git a/assert/test-assert-perr.c b/assert/test-assert-perr.c index 8496db6ffd..09a4fcb6ef 100644 --- a/assert/test-assert-perr.c +++ b/assert/test-assert-perr.c @@ -11,6 +11,8 @@ #include #include +#include + jmp_buf rec; char buf[160]; @@ -70,15 +72,15 @@ main(void) failed = 1; /* should not happen */ rewind (stderr); - fgets (buf, 160, stderr); + xfgets (buf, 160, stderr); if (!strstr(buf, strerror (1))) failed = 1; - fgets (buf, 160, stderr); + xfgets (buf, 160, stderr); if (strstr (buf, strerror (0))) failed = 1; - fgets (buf, 160, stderr); + xfgets (buf, 160, stderr); if (strstr (buf, strerror (2))) failed = 1; diff --git a/assert/test-assert.c b/assert/test-assert.c index 26b58d4dd3..25e264543b 100644 --- a/assert/test-assert.c +++ b/assert/test-assert.c @@ -11,6 +11,8 @@ #include #include +#include + jmp_buf rec; char buf[160]; @@ -72,15 +74,15 @@ main (void) failed = 1; /* should not happen */ rewind (stderr); - fgets (buf, 160, stderr); + xfgets (buf, 160, stderr); if (!strstr (buf, "1 == 2")) failed = 1; - fgets (buf, 160, stderr); + xfgets (buf, 160, stderr); if (strstr (buf, "1 == 1")) failed = 1; - fgets (buf, 160, stderr); + xfgets (buf, 160, stderr); if (strstr (buf, "2 == 3")) failed = 1; diff --git a/stdio-common/test_rdwr.c b/stdio-common/test_rdwr.c index 7825ca9358..0544916eb1 100644 --- a/stdio-common/test_rdwr.c +++ b/stdio-common/test_rdwr.c @@ -20,6 +20,7 @@ #include #include +#include int main (int argc, char **argv) @@ -49,7 +50,7 @@ main (int argc, char **argv) (void) fputs (hello, f); rewind (f); - (void) fgets (buf, sizeof (buf), f); + xfgets (buf, sizeof (buf), f); rewind (f); (void) fputs (buf, f); rewind (f); @@ -104,12 +105,8 @@ main (int argc, char **argv) if (!lose) { rewind (f); - if (fgets (buf, sizeof (buf), f) == NULL) - { - printf ("fgets got %s.\n", strerror(errno)); - lose = 1; - } - else if (strcmp (buf, replace)) + xfgets (buf, sizeof (buf), f); + if (strcmp (buf, replace)) { printf ("Read \"%s\" instead of \"%s\".\n", buf, replace); lose = 1; diff --git a/support/Makefile b/support/Makefile index 3eed2deb1a..bbd3465d04 100644 --- a/support/Makefile +++ b/support/Makefile @@ -123,6 +123,7 @@ libsupport-routines = \ xdup2 \ xfchmod \ xfclose \ + xfgets \ xfopen \ xfork \ xfreopen \ diff --git a/support/xfgets.c b/support/xfgets.c new file mode 100644 index 0000000000..e2db19cf53 --- /dev/null +++ b/support/xfgets.c @@ -0,0 +1,32 @@ +/* fgets with error checking. + Copyright (C) 2016-2023 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 + +char * +xfgets (char *s, int size, FILE *stream) +{ + char *ret = fgets (s, size, stream); + if (!ret && ferror(stream)) + FAIL_EXIT1 ("fgets failed: %m"); + + return ret; +} diff --git a/support/xstdio.h b/support/xstdio.h index 633c342c82..f30bee6a20 100644 --- a/support/xstdio.h +++ b/support/xstdio.h @@ -28,6 +28,7 @@ FILE *xfopen (const char *path, const char *mode); void xfclose (FILE *); FILE *xfreopen (const char *path, const char *mode, FILE *stream); void xfread (void *ptr, size_t size, size_t nmemb, FILE *stream); +char *xfgets (char *s, int size, FILE *stream); /* Read a line from FP, using getline. *BUFFER must be NULL, or a heap-allocated pointer of *LENGTH bytes. Return the number of diff --git a/sysdeps/pthread/tst-cancel6.c b/sysdeps/pthread/tst-cancel6.c index 63e6d49707..49b7399353 100644 --- a/sysdeps/pthread/tst-cancel6.c +++ b/sysdeps/pthread/tst-cancel6.c @@ -20,12 +20,13 @@ #include #include +#include static void * tf (void *arg) { char buf[100]; - fgets (buf, sizeof (buf), arg); + xfgets (buf, sizeof (buf), arg); /* This call should never return. */ return NULL; } From patchwork Fri Jun 2 15:28:11 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 1789746 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=O07tbT5r; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QXn5T4M2Sz20QH for ; Sat, 3 Jun 2023 01:30:53 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 9A6B9385661C for ; Fri, 2 Jun 2023 15:30:51 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9A6B9385661C DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685719851; bh=zX+ZyLtUhHhgCkhv6Tn/Fk5LtlqKK/Y5q/HlGk0I30c=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=O07tbT5rdRq5Wn5QSfIT4FERAo9ASugtQsdpcG3qtYB2lKVh1M0J1ctBLeGq7JB0/ c1Tl9PF9J70AqhzPUpMYOdZtr/Ju9JiZn8vrgoJB/3bDkHsS69C9FmIrnRZ+kMi6tz XpfXm8zeL26dJfkbVNpd70xcO4ERErydRgJqv9eQ= 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 50FDE3856616 for ; Fri, 2 Jun 2023 15:28:26 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 50FDE3856616 Received: from mimecast-mx02.redhat.com (mimecast-mx02.redhat.com [66.187.233.88]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-204-Veei5tiBNBqzcC6L1svc2Q-1; Fri, 02 Jun 2023 11:28:22 -0400 X-MC-Unique: Veei5tiBNBqzcC6L1svc2Q-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 63AAF8002BF; Fri, 2 Jun 2023 15:28:21 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.193.180]) by smtp.corp.redhat.com (Postfix) with ESMTPS id BECB140CFD45; Fri, 2 Jun 2023 15:28:20 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v6 6/7] tests: Replace various function calls with their x variant Date: Fri, 2 Jun 2023 17:28:11 +0200 Message-Id: <20230602152812.108497-7-fberat@redhat.com> In-Reply-To: <20230602152812.108497-1-fberat@redhat.com> References: <20230602152812.108497-1-fberat@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.5 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, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Fr=C3=A9d=C3=A9ric_B=C3=A9rat_via_Libc-alpha?= From: Frederic Berat Reply-To: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" With fortification enabled, few function calls return result need to be checked, has they get the __wur macro enabled. Reviewed-by: Siddhesh Poyarekar --- Changes since v5: - Actually replace functions with their x variant as described misc/tst-error1.c | 4 +++- nss/tst-reload2.c | 2 +- posix/tst-chmod.c | 7 +++++-- posix/wordexp-test.c | 2 +- stdlib/test-canon.c | 4 +++- 5 files changed, 13 insertions(+), 6 deletions(-) diff --git a/misc/tst-error1.c b/misc/tst-error1.c index 9c4a62fbd0..75d4edf476 100644 --- a/misc/tst-error1.c +++ b/misc/tst-error1.c @@ -5,11 +5,13 @@ #include #include +#include + static int do_test (int argc, char *argv[]) { mtrace (); - (void) freopen (argc == 1 ? "/dev/stdout" : argv[1], "a", stderr); + xfreopen (argc == 1 ? "/dev/stdout" : argv[1], "a", stderr); /* Orient the stream. */ fwprintf (stderr, L"hello world\n"); char buf[20000]; diff --git a/nss/tst-reload2.c b/nss/tst-reload2.c index ba9b5b7687..89cc9ea242 100644 --- a/nss/tst-reload2.c +++ b/nss/tst-reload2.c @@ -121,7 +121,7 @@ do_test (void) /* Change the root dir. */ TEST_VERIFY (chroot ("/subdir") == 0); - chdir ("/"); + xchdir ("/"); /* Check we're NOT using the "inner" nsswitch.conf. */ diff --git a/posix/tst-chmod.c b/posix/tst-chmod.c index b98a05a265..470a696fe5 100644 --- a/posix/tst-chmod.c +++ b/posix/tst-chmod.c @@ -27,6 +27,8 @@ #include #include +#include + #define OUT_OF_MEMORY \ do { \ @@ -229,7 +231,8 @@ do_test (int argc, char *argv[]) close (fd); snprintf (buf, buflen, "%s/..", testdir); - chdir (buf); + xchdir (buf); + /* We are now in the directory above the one we create the test directory in. */ @@ -349,7 +352,7 @@ do_test (int argc, char *argv[]) } fail: - chdir (startdir); + xchdir (startdir); /* Remove all the files. */ chmod (testdir, 0700); diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c index 524597d96b..28d1d1509b 100644 --- a/posix/wordexp-test.c +++ b/posix/wordexp-test.c @@ -336,7 +336,7 @@ do_test (int argc, char *argv[]) if (cwd == NULL) cwd = ".."; - chdir (cwd); + xchdir (cwd); rmdir (tmpdir); return 0; diff --git a/stdlib/test-canon.c b/stdlib/test-canon.c index bf19b1f1b1..2874258722 100644 --- a/stdlib/test-canon.c +++ b/stdlib/test-canon.c @@ -27,6 +27,8 @@ #include #include +#include + /* Prototype for our test function. */ extern int do_test (int argc, char *argv[]); #include @@ -159,7 +161,7 @@ do_test (int argc, char ** argv) } for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i) - symlink (symlinks[i].value, symlinks[i].name); + xsymlink (symlinks[i].value, symlinks[i].name); int has_dir = mkdir ("doesExist", 0777) == 0; From patchwork Fri Jun 2 15:28:12 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 1789745 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=sourceware.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=sourceware.org; envelope-from=libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org; receiver=) Authentication-Results: legolas.ozlabs.org; dkim=pass (1024-bit key; secure) header.d=sourceware.org header.i=@sourceware.org header.a=rsa-sha256 header.s=default header.b=O1Ns3fGB; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-384) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4QXn4d5Q8Lz20QH for ; Sat, 3 Jun 2023 01:30:09 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A24FB384FDD6 for ; Fri, 2 Jun 2023 15:30:07 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A24FB384FDD6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1685719807; bh=PWw9F3GZxsa1uy2cgkI6nDQ5eKlXa4BP6iHxISVLcus=; h=To:Cc:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=O1Ns3fGBDr+CAWf0W5w11JEUCxRKe5I0RuredqWX/AZE9l1o5vJRs7wtQrESCm1hX ojquPwwEsleIfyIU6E/IOAz5mvaE1Mp535e85jzPkRgcTul6K3kPYYsCcD9gQYUDhe Kcr53ZcSAubZCSfhQMC0sfqa9I4YpLff3wHs3InQ= 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 E6139385802F for ; Fri, 2 Jun 2023 15:28:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E6139385802F Received: from mimecast-mx02.redhat.com (mx3-rdu2.redhat.com [66.187.233.73]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-274-3s0R_Q6lPTKGR8kQp-mdsg-1; Fri, 02 Jun 2023 11:28:22 -0400 X-MC-Unique: 3s0R_Q6lPTKGR8kQp-mdsg-1 Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.rdu2.redhat.com [10.11.54.1]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 4337C3C13A19; Fri, 2 Jun 2023 15:28:22 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.193.180]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 9EC9140CFD45; Fri, 2 Jun 2023 15:28:21 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v6 7/7] Move {read,write}_all functions to a dedicated header Date: Fri, 2 Jun 2023 17:28:12 +0200 Message-Id: <20230602152812.108497-8-fberat@redhat.com> In-Reply-To: <20230602152812.108497-1-fberat@redhat.com> References: <20230602152812.108497-1-fberat@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.1 on 10.11.54.1 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com X-Spam-Status: No, score=-12.8 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, 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.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: =?utf-8?q?Fr=C3=A9d=C3=A9ric_B=C3=A9rat_via_Libc-alpha?= From: Frederic Berat Reply-To: =?utf-8?b?RnLDqWTDqXJpYyBCw6lyYXQ=?= Errors-To: libc-alpha-bounces+incoming=patchwork.ozlabs.org@sourceware.org Sender: "Libc-alpha" Since these functions are used in both catgets/gencat.c and malloc/memusage{,stat}.c, it make sense to move them into a dedicated header where they can be inlined. Reviewed-by: Siddhesh Poyarekar --- Changes since v5: - Adding copyright to unistd_ext.h - Don't remove included headers in memusagestat.c catgets/gencat.c | 22 +-------------- include/unistd_ext.h | 66 +++++++++++++++++++++++++++++++++++++++++++ malloc/memusage.c | 22 +-------------- malloc/memusagestat.c | 40 +------------------------- 4 files changed, 69 insertions(+), 81 deletions(-) create mode 100644 include/unistd_ext.h diff --git a/catgets/gencat.c b/catgets/gencat.c index 826596c2e4..9cacc801b5 100644 --- a/catgets/gencat.c +++ b/catgets/gencat.c @@ -37,6 +37,7 @@ #include #include #include +#include #include #include "version.h" @@ -838,27 +839,6 @@ invalid character: message ignored")); return current; } -static void -write_all (int fd, const void *buffer, size_t length) -{ - const char *p = buffer; - const char *end = p + length; - while (p < end) - { - ssize_t ret = write (fd, p, end - p); - if (ret < 0) - error (EXIT_FAILURE, errno, - gettext ("write of %zu bytes failed after %td: %m"), - length, p - (const char *) buffer); - - if (ret == 0) - error (EXIT_FAILURE, 0, - gettext ("write returned 0 after writing %td bytes of %zu"), - p - (const char *) buffer, length); - p += ret; - } -} - static void write_out (struct catalog *catalog, const char *output_name, const char *header_name) diff --git a/include/unistd_ext.h b/include/unistd_ext.h new file mode 100644 index 0000000000..8c93bde88b --- /dev/null +++ b/include/unistd_ext.h @@ -0,0 +1,66 @@ +/* unistd.h related helpers. + Copyright (C) 2023 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 + . */ + +#ifndef _UNISTD_EXT_H +#define _UNISTD_EXT_H + +#include +#include +#include +#include + +/* Helpers used in catgets/gencat.c and malloc/memusage*.c */ +static inline void +write_all (int fd, const void *buffer, size_t length) +{ + const char *p = buffer; + const char *end = p + length; + while (p < end) + { + ssize_t ret = write (fd, p, end - p); + if (ret < 0) + error (EXIT_FAILURE, errno, + gettext ("write of %zu bytes failed after %td: %m"), + length, p - (const char *) buffer); + + if (ret == 0) + error (EXIT_FAILURE, 0, + gettext ("write returned 0 after writing %td bytes of %zu"), + p - (const char *) buffer, length); + p += ret; + } +} + +static inline void +read_all (int fd, void *buffer, size_t length) +{ + char *p = buffer; + char *end = p + length; + while (p < end) + { + ssize_t ret = read (fd, p, end - p); + if (ret < 0) + error (EXIT_FAILURE, errno, + gettext ("read of %zu bytes failed after %td: %m"), + length, p - (char *) buffer); + + p += ret; + } +} + +#endif diff --git a/malloc/memusage.c b/malloc/memusage.c index 53a3630651..cd675e16a6 100644 --- a/malloc/memusage.c +++ b/malloc/memusage.c @@ -30,6 +30,7 @@ #include #include #include +#include #include #include @@ -145,27 +146,6 @@ peak_atomic_max (_Atomic size_t *peak, size_t val) while (! atomic_compare_exchange_weak (peak, &v, val)); } -static void -write_all (int fd, const void *buffer, size_t length) -{ - const char *p = buffer; - const char *end = p + length; - while (p < end) - { - ssize_t ret = write (fd, p, end - p); - if (ret < 0) - error (EXIT_FAILURE, errno, - gettext ("write of %zu bytes failed after %td: %m"), - length, p - (const char *) buffer); - - if (ret == 0) - error (EXIT_FAILURE, 0, - gettext ("write returned 0 after writing %td bytes of %zu"), - p - (const char *) buffer, length); - p += ret; - } -} - /* Update the global data after a successful function call. */ static void update_data (struct header *result, size_t len, size_t old_len) diff --git a/malloc/memusagestat.c b/malloc/memusagestat.c index cc066d4af9..837b613c2b 100644 --- a/malloc/memusagestat.c +++ b/malloc/memusagestat.c @@ -29,6 +29,7 @@ #include #include #include +#include #include #include #include @@ -114,45 +115,6 @@ static int time_based; static int also_total = 0; -static void -read_all (int fd, void *buffer, size_t length) -{ - char *p = buffer; - char *end = p + length; - while (p < end) - { - ssize_t ret = read (fd, p, end - p); - if (ret < 0) - error (EXIT_FAILURE, errno, - gettext ("read of %zu bytes failed after %td: %m"), - length, p - (char *) buffer); - - p += ret; - } -} - -static void -write_all (int fd, const void *buffer, size_t length) -{ - const char *p = buffer; - const char *end = p + length; - while (p < end) - { - ssize_t ret = write (fd, p, end - p); - if (ret < 0) - error (EXIT_FAILURE, errno, - gettext ("write of %zu bytes failed after %td: %m"), - length, p - (const char *) buffer); - - if (ret == 0) - error (EXIT_FAILURE, 0, - gettext ("write returned 0 after writing %td bytes of %zu"), - p - (const char *) buffer, length); - p += ret; - } -} - - int main (int argc, char *argv[]) {