From patchwork Mon Jun 12 15:18:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 70912 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 0013B3857359 for ; Mon, 12 Jun 2023 15:19:41 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0013B3857359 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686583182; bh=NcQePaHCSQBwYbrd7aj4qe9vyheROikSoXNrs7mAaQM=; 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=luYjhKikuLSKNwU4nzNNrC5gNlm44WzWgw9jmOTBPqME0js2lO7jB8RxMccphwl7P spdKl4qefvTW/RKHBqdeXNK90/jvuUgXVUj56K3YKdmg2A0WdVpjuzfKhXHHSZh5+/ hjW9Cnit+tRKxNL9epoBe5pnLL17EJ2ZYn5qDhC0= 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 4A9E53858422 for ; Mon, 12 Jun 2023 15:18:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4A9E53858422 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-567-MEjg4GTWPK-9x2nbxnnedQ-1; Mon, 12 Jun 2023 11:18:31 -0400 X-MC-Unique: MEjg4GTWPK-9x2nbxnnedQ-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 C96402A5957D; Mon, 12 Jun 2023 15:18:30 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.192.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2DCAE40C20F4; Mon, 12 Jun 2023 15:18:30 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v7 1/4] tests: replace read by xread Date: Mon, 12 Jun 2023 17:18:18 +0200 Message-Id: <20230612151821.199003-2-fberat@redhat.com> In-Reply-To: <20230612151821.199003-1-fberat@redhat.com> References: <20230612151821.199003-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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" With fortification enabled, read calls return result needs to be checked, has it gets the __wur macro enabled. Note on read call removal from sysdeps/pthread/tst-cancel20.c and sysdeps/pthread/tst-cancel21.c: It is assumed that this second read call was there to overcome the race condition between pipe closure and thread cancellation that could happen in the original code. Since this race condition got fixed by d0e3ffb7a58854248f1d5e737610d50cd0a60f46 the second call seems superfluous. Hence, instead of checking for the return value of read, it looks reasonable to simply remove it. --- Changes since v6: - Add note for read call removal - Fixed support/Makefile ordering - Revert faulty change in behavior for first read call in tst-cancel{20,21}.c 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 | 2 -- sysdeps/pthread/tst-cancel21.c | 2 -- sysdeps/pthread/tst-fini1mod.c | 4 +++- 11 files changed, 54 insertions(+), 10 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..5b14abdd28 100644 --- a/support/Makefile +++ b/support/Makefile @@ -195,6 +195,7 @@ libsupport-routines = \ xpthread_spin_lock \ xpthread_spin_unlock \ xraise \ + xread \ xreadlink \ xrealloc \ xrecvfrom \ 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..19f76fcfeb --- /dev/null +++ b/support/xread.c @@ -0,0 +1,36 @@ +/* read with error checking and retries. + 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 + +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..93f287aa4f 100644 --- a/sysdeps/pthread/tst-cancel20.c +++ b/sysdeps/pthread/tst-cancel20.c @@ -84,8 +84,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..ec8bf0dba3 100644 --- a/sysdeps/pthread/tst-cancel21.c +++ b/sysdeps/pthread/tst-cancel21.c @@ -85,8 +85,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 Mon Jun 12 15:18:19 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 70910 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 EBA123857009 for ; Mon, 12 Jun 2023 15:18:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org EBA123857009 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686583138; bh=E7MTqyUxf7FcU3r3bRHbkScvyxjh0LXsmfSn6xQdyVk=; 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=jVGy24AuAc2OCqBxTypg5fMv2QHjz+Akn8oQQUoUdtxYseXftV14Yab8ufvThjTPV Q6dqwUzv/Guhw0X2kB7MXMwzXehY7oDK3eiRnVbMXYPCIBO6ixgD6TpvnlnL9Fs1Cc n/iRm4YpZ4XcDmj8/F+KFY3xkTfKFlK4h1NXY/nI= 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 7D3773858C33 for ; Mon, 12 Jun 2023 15:18:33 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7D3773858C33 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-196-C5zIp8z6NAyki0gfeEkWRg-1; Mon, 12 Jun 2023 11:18:32 -0400 X-MC-Unique: C5zIp8z6NAyki0gfeEkWRg-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 AB3211C0E3C6; Mon, 12 Jun 2023 15:18:31 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.192.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1080840C20F4; Mon, 12 Jun 2023 15:18:30 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v7 2/4] tests: replace system by xsystem Date: Mon, 12 Jun 2023 17:18:19 +0200 Message-Id: <20230612151821.199003-3-fberat@redhat.com> In-Reply-To: <20230612151821.199003-1-fberat@redhat.com> References: <20230612151821.199003-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, 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.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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" With fortification enabled, system calls return result needs to be checked, has it gets the __wur macro enabled. Reviewed-by: Siddhesh Poyarekar --- Changes since v6: - Fixed support/Makefile ordering 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 5b14abdd28..8ee3344c9a 100644 --- a/support/Makefile +++ b/support/Makefile @@ -210,6 +210,7 @@ libsupport-routines = \ xstrndup \ xsymlink \ xsysconf \ + xsystem \ xunlink \ xuselocale \ xwaitpid \ 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 Mon Jun 12 15:18:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 70913 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 906F7385800A for ; Mon, 12 Jun 2023 15:20:24 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 906F7385800A DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686583224; bh=OIJUpFB1zFZxwTxYpCeI/t2dqaYdmf93D6DEkDdPWxc=; 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=cCHwuqCbjiWPFBNoJYvoB0h/J7EwyKBPUR9VbV8C+fMJWqWFDJx7I9neMw/aHYnaL G8Qs8rdZKFh+X8xjZsFti8ll9G1f4sBBwJdDg7r03j0HcZiUAxcknhEVxFRNqdq249 5n3wRuFD7GOwQd4yWcDTsnwAPOVX9IpmGiUWJ+xE= 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 C1D133858C78 for ; Mon, 12 Jun 2023 15:18:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org C1D133858C78 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-394-y9LBvVohPqaTT60nTXghUg-1; Mon, 12 Jun 2023 11:18:33 -0400 X-MC-Unique: y9LBvVohPqaTT60nTXghUg-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 AB28B381D1F4; Mon, 12 Jun 2023 15:18:32 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.192.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E701B40C20F4; Mon, 12 Jun 2023 15:18:31 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v7 3/4] tests: replace fread by xfread Date: Mon, 12 Jun 2023 17:18:20 +0200 Message-Id: <20230612151821.199003-4-fberat@redhat.com> In-Reply-To: <20230612151821.199003-1-fberat@redhat.com> References: <20230612151821.199003-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, 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.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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" With fortification enabled, fread calls return result needs to be checked, has it gets the __wur macro enabled. Reviewed-by: Siddhesh Poyarekar --- Changes since v6: - Fixed support/Makefile ordering - Fixed header copyright date 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 8ee3344c9a..994639a915 100644 --- a/support/Makefile +++ b/support/Makefile @@ -125,6 +125,7 @@ libsupport-routines = \ xfclose \ xfopen \ xfork \ + xfread \ xfreopen \ xftruncate \ xgetline \ diff --git a/support/xfread.c b/support/xfread.c new file mode 100644 index 0000000000..948f0c8111 --- /dev/null +++ b/support/xfread.c @@ -0,0 +1,39 @@ +/* fread with error checking. + 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 +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 Mon Jun 12 15:18:21 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Frederic Berat X-Patchwork-Id: 70911 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 BAA143857714 for ; Mon, 12 Jun 2023 15:19:26 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org BAA143857714 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1686583166; bh=LTYFzkZ2K5YglqxzFoMpMP638PmpqoHW6ifHe+QVCho=; 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=KAAfnXNlDthS71eS2CtufzwRdFEDrORZyq8EJXtF/pfFZSRmCIaL5mrIi0YcjeIxB HmQQwAWkSc/xcuXfLXvYgX/iYfpWPgPivHLSwxldwm3SUOJMCBMGLasZcAro0ygmIT xXNn8JzwTDGe9ScJX2i9CH6JQEOmeEWduuU4OTUk= 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 803633858423 for ; Mon, 12 Jun 2023 15:18:36 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 803633858423 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-460-5Lps2N3LMLySLkT8U1JrjQ-1; Mon, 12 Jun 2023 11:18:34 -0400 X-MC-Unique: 5Lps2N3LMLySLkT8U1JrjQ-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 8E800801585; Mon, 12 Jun 2023 15:18:33 +0000 (UTC) Received: from Nymeria-redhat.redhat.com (unknown [10.39.192.6]) by smtp.corp.redhat.com (Postfix) with ESMTPS id E797940C20F4; Mon, 12 Jun 2023 15:18:32 +0000 (UTC) To: libc-alpha@sourceware.org Cc: siddhesh@gotplt.org, fberat@redhat.com Subject: [PATCH v7 4/4] tests: replace fgets by xfgets Date: Mon, 12 Jun 2023 17:18:21 +0200 Message-Id: <20230612151821.199003-5-fberat@redhat.com> In-Reply-To: <20230612151821.199003-1-fberat@redhat.com> References: <20230612151821.199003-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.4 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.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+patchwork=sourceware.org@sourceware.org Sender: "Libc-alpha" With fortification enabled, fgets calls return result needs to be checked, has it gets the __wur macro enabled. Reviewed-by: Siddhesh Poyarekar --- Changes since v6: - Fixed support/Makefile ordering - Fixed header copyright date 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 994639a915..c81e3c928c 100644 --- a/support/Makefile +++ b/support/Makefile @@ -123,6 +123,7 @@ libsupport-routines = \ xdup2 \ xfchmod \ xfclose \ + xfgets \ xfopen \ xfork \ xfread \ diff --git a/support/xfgets.c b/support/xfgets.c new file mode 100644 index 0000000000..14f98dee1b --- /dev/null +++ b/support/xfgets.c @@ -0,0 +1,32 @@ +/* fgets with error checking. + 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 + +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; }