public inbox for libc-alpha@sourceware.org
 help / color / mirror / Atom feed
* [PATCH 0/8] Fix warn unused result
@ 2023-04-18 12:11 Frédéric Bérat
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
                   ` (10 more replies)
  0 siblings, 11 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-18 12:11 UTC (permalink / raw)
  To: libc-alpha; +Cc: sipoyare

Hello,

Enabling _FORTIFY_SOURCE on Glibc itself implies that some function get the
__wur macro enabed.
This leads to build failures when -Werror is set.

I went for multiple patches for these fixes, a big one that cover the tests,
and then separate one for the other ones, assuming they may need to get a
closer look at.

While most of the changes look trivial, decision I made on how to fix them may
be open to debate, e.g. the additional checks may lead to failures while they
weren't earlier, some other have no impact while it may be useful to have one.

Feedback is therefore highly appreciated.

Fred.

---
Frédéric Bérat (8):
  tests: fix warn unused result
  catgets/gencat.c: fix warn unused result
  inet/rcmd.c: fix warn unused result
  locale/programs/locarchive.c: fix warn unused result
  malloc/{memusage.c,memusagestat.c}: fix warn unused result
  nptl_db/thread_dbP.h: fix warn unused result
  sunrpc/netname.c: fix warn unused result
  sysdeps/pthread/eintr.c: fix warn unused result

 argp/argp-test.c               | 14 ++++++++++---
 assert/test-assert-perr.c      | 13 ++++++------
 assert/test-assert.c           | 13 ++++++------
 catgets/gencat.c               | 34 ++++++++++++++++++++----------
 crypt/cert.c                   |  6 +++++-
 dirent/tst-fdopendir.c         | 13 ++++++++++--
 elf/tst-stackguard1.c          |  2 +-
 inet/rcmd.c                    |  7 +++++--
 io/tst-copy_file_range.c       |  2 +-
 io/tst-faccessat.c             |  6 +++++-
 io/tst-fchmodat.c              |  6 +++++-
 io/tst-fchownat.c              |  6 +++++-
 io/tst-fstatat.c               |  6 +++++-
 io/tst-futimesat.c             |  6 +++++-
 io/tst-linkat.c                |  6 +++++-
 io/tst-openat.c                |  6 +++++-
 io/tst-renameat.c              |  6 +++++-
 io/tst-symlinkat.c             |  6 +++++-
 io/tst-unlinkat.c              |  6 +++++-
 libio/bug-fseek.c              | 15 +++++++++++---
 libio/bug-mmap-fflush.c        | 12 +++++++++--
 libio/bug-ungetc.c             |  7 ++++++-
 libio/bug-ungetc3.c            |  7 ++++++-
 libio/bug-ungetc4.c            |  7 ++++++-
 libio/bug-wfflush.c            |  6 +++++-
 libio/bug-wsetpos.c            |  7 ++++++-
 locale/programs/locarchive.c   | 18 +++++++++-------
 malloc/memusage.c              | 38 +++++++++++++++++++++++-----------
 malloc/memusagestat.c          | 13 ++++++++----
 misc/tst-efgcvt-template.c     |  4 ++--
 misc/tst-error1.c              |  2 +-
 nptl/tst-cancel7.c             |  2 +-
 nptl/tst-cleanup4.c            |  6 +++++-
 nptl/tst-stackguard1.c         |  8 +++++--
 nptl/tst-tls3mod.c             |  4 ++--
 nptl_db/thread_dbP.h           |  2 +-
 nss/tst-nss-db-endpwent.c      |  6 +++++-
 nss/tst-reload2.c              |  6 +++++-
 posix/tst-chmod.c              |  9 ++++++--
 posix/tst-execl2.c             |  4 ++--
 posix/tst-execle2.c            |  4 ++--
 posix/tst-execlp2.c            |  7 +++----
 posix/tst-execv2.c             |  3 +--
 posix/tst-execve2.c            |  3 +--
 posix/tst-execvp2.c            |  6 ++----
 posix/tst-getopt-cancel.c      |  2 +-
 posix/tst-nice.c               |  3 +--
 posix/wordexp-test.c           | 12 +++++++++--
 rt/tst-cpuclock2.c             |  2 +-
 rt/tst-cputimer1.c             |  2 +-
 rt/tst-cputimer2.c             |  2 +-
 rt/tst-cputimer3.c             |  2 +-
 stdio-common/bug12.c           | 15 +++++++++-----
 stdio-common/bug19.c           |  9 ++++++--
 stdio-common/bug3.c            |  6 +++++-
 stdio-common/bug4.c            |  6 +++++-
 stdio-common/bug5.c            |  6 +++++-
 stdio-common/bug6.c            |  8 +++----
 stdio-common/test-fwrite.c     |  8 +++++--
 stdio-common/test_rdwr.c       |  3 ++-
 stdio-common/tst-cookie.c      |  4 +++-
 stdio-common/tst-fmemopen3.c   |  6 +++++-
 stdio-common/tst-fseek.c       |  3 +--
 stdio-common/tst-perror.c      |  6 +++++-
 stdio-common/tstscanf.c        | 14 +++++++++++--
 stdlib/test-canon.c            | 25 ++++++++++++++++++----
 sunrpc/netname.c               |  3 ++-
 support/test-container.c       | 18 +++++++++-------
 sysdeps/pthread/eintr.c        |  4 ++--
 sysdeps/pthread/tst-cancel11.c |  6 +++++-
 sysdeps/pthread/tst-cancel16.c |  6 +++++-
 sysdeps/pthread/tst-cancel20.c |  2 --
 sysdeps/pthread/tst-cancel21.c |  2 --
 sysdeps/pthread/tst-cancel4.c  |  6 ++++--
 sysdeps/pthread/tst-cancel6.c  |  3 ++-
 sysdeps/pthread/tst-cond18.c   |  2 +-
 sysdeps/pthread/tst-fini1mod.c |  6 +++++-
 sysdeps/pthread/tst-flock1.c   |  6 +++++-
 sysdeps/pthread/tst-flock2.c   |  6 +++++-
 sysdeps/pthread/tst-key1.c     | 10 ++++-----
 sysdeps/pthread/tst-signal1.c  |  6 +++++-
 sysdeps/pthread/tst-signal2.c  |  6 +++++-
 sysdeps/pthread/tst-timer.c    |  2 +-
 time/tst-cpuclock1.c           |  2 +-
 84 files changed, 434 insertions(+), 176 deletions(-)

-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH 1/8] tests: fix warn unused result
  2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
@ 2023-04-18 12:11 ` Frédéric Bérat
  2023-04-18 12:23   ` Siddhesh Poyarekar
                     ` (12 more replies)
  2023-04-18 12:11 ` [PATCH 2/8] catgets/gencat.c: fix warn unused result Frédéric Bérat
                   ` (9 subsequent siblings)
  10 siblings, 13 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-18 12:11 UTC (permalink / raw)
  To: libc-alpha; +Cc: sipoyare

When enabling _FORTIFY_SOURCE, some functions now lead to warnings when
their result is not checked.
---
 argp/argp-test.c               | 14 +++++++++++---
 assert/test-assert-perr.c      | 13 +++++++------
 assert/test-assert.c           | 13 +++++++------
 crypt/cert.c                   |  6 +++++-
 dirent/tst-fdopendir.c         | 13 +++++++++++--
 elf/tst-stackguard1.c          |  2 +-
 io/tst-copy_file_range.c       |  2 +-
 io/tst-faccessat.c             |  6 +++++-
 io/tst-fchmodat.c              |  6 +++++-
 io/tst-fchownat.c              |  6 +++++-
 io/tst-fstatat.c               |  6 +++++-
 io/tst-futimesat.c             |  6 +++++-
 io/tst-linkat.c                |  6 +++++-
 io/tst-openat.c                |  6 +++++-
 io/tst-renameat.c              |  6 +++++-
 io/tst-symlinkat.c             |  6 +++++-
 io/tst-unlinkat.c              |  6 +++++-
 libio/bug-fseek.c              | 15 ++++++++++++---
 libio/bug-mmap-fflush.c        | 12 ++++++++++--
 libio/bug-ungetc.c             |  7 ++++++-
 libio/bug-ungetc3.c            |  7 ++++++-
 libio/bug-ungetc4.c            |  7 ++++++-
 libio/bug-wfflush.c            |  6 +++++-
 libio/bug-wsetpos.c            |  7 ++++++-
 misc/tst-efgcvt-template.c     |  4 ++--
 misc/tst-error1.c              |  2 +-
 nptl/tst-cancel7.c             |  2 +-
 nptl/tst-cleanup4.c            |  6 +++++-
 nptl/tst-stackguard1.c         |  8 ++++++--
 nptl/tst-tls3mod.c             |  4 ++--
 nss/tst-nss-db-endpwent.c      |  6 +++++-
 nss/tst-reload2.c              |  6 +++++-
 posix/tst-chmod.c              |  9 +++++++--
 posix/tst-execl2.c             |  4 ++--
 posix/tst-execle2.c            |  4 ++--
 posix/tst-execlp2.c            |  7 +++----
 posix/tst-execv2.c             |  3 +--
 posix/tst-execve2.c            |  3 +--
 posix/tst-execvp2.c            |  6 ++----
 posix/tst-getopt-cancel.c      |  2 +-
 posix/tst-nice.c               |  3 +--
 posix/wordexp-test.c           | 12 ++++++++++--
 rt/tst-cpuclock2.c             |  2 +-
 rt/tst-cputimer1.c             |  2 +-
 rt/tst-cputimer2.c             |  2 +-
 rt/tst-cputimer3.c             |  2 +-
 stdio-common/bug12.c           | 15 ++++++++++-----
 stdio-common/bug19.c           |  9 +++++++--
 stdio-common/bug3.c            |  6 +++++-
 stdio-common/bug4.c            |  6 +++++-
 stdio-common/bug5.c            |  6 +++++-
 stdio-common/bug6.c            |  8 ++++----
 stdio-common/test-fwrite.c     |  8 ++++++--
 stdio-common/test_rdwr.c       |  3 ++-
 stdio-common/tst-cookie.c      |  4 +++-
 stdio-common/tst-fmemopen3.c   |  6 +++++-
 stdio-common/tst-fseek.c       |  3 +--
 stdio-common/tst-perror.c      |  6 +++++-
 stdio-common/tstscanf.c        | 14 ++++++++++++--
 stdlib/test-canon.c            | 25 +++++++++++++++++++++----
 support/test-container.c       | 18 +++++++++++-------
 sysdeps/pthread/tst-cancel11.c |  6 +++++-
 sysdeps/pthread/tst-cancel16.c |  6 +++++-
 sysdeps/pthread/tst-cancel20.c |  2 --
 sysdeps/pthread/tst-cancel21.c |  2 --
 sysdeps/pthread/tst-cancel4.c  |  6 ++++--
 sysdeps/pthread/tst-cancel6.c  |  3 ++-
 sysdeps/pthread/tst-cond18.c   |  2 +-
 sysdeps/pthread/tst-fini1mod.c |  6 +++++-
 sysdeps/pthread/tst-flock1.c   |  6 +++++-
 sysdeps/pthread/tst-flock2.c   |  6 +++++-
 sysdeps/pthread/tst-key1.c     | 10 +++++-----
 sysdeps/pthread/tst-signal1.c  |  6 +++++-
 sysdeps/pthread/tst-signal2.c  |  6 +++++-
 sysdeps/pthread/tst-timer.c    |  2 +-
 time/tst-cpuclock1.c           |  2 +-
 76 files changed, 356 insertions(+), 135 deletions(-)

diff --git a/argp/argp-test.c b/argp/argp-test.c
index c7e20f6235..fa7f9694d1 100644
--- a/argp/argp-test.c
+++ b/argp/argp-test.c
@@ -178,12 +178,20 @@ 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));
+      if (asprintf (&new_text, text, ctime (&now)) < 0)
+	{
+	  printf("Failed to execute asprintf: %m\n");
+	  exit(1);
+	}
     }
   else if (key == 'f')
     /* Show the default for the --foonly option.  */
-    asprintf (&new_text, "%s (ZOT defaults to %x)",
-	      text, params->foonly_default);
+    if (asprintf (&new_text, "%s (ZOT defaults to %x)",
+	      text, params->foonly_default) < 0)
+	{
+	  printf("Failed to execute asprintf: %m\n");
+	  exit(1);
+	}
   else
     new_text = (char *)text;
 
diff --git a/assert/test-assert-perr.c b/assert/test-assert-perr.c
index 8496db6ffd..ecfd4d14e5 100644
--- a/assert/test-assert-perr.c
+++ b/assert/test-assert-perr.c
@@ -46,6 +46,7 @@ int
 main(void)
 {
   volatile int failed = 1;  /* safety in presence of longjmp() */
+  char *ret;
 
   fclose (stderr);
   stderr = tmpfile ();
@@ -70,16 +71,16 @@ main(void)
     failed = 1; /* should not happen */
 
   rewind (stderr);
-  fgets (buf, 160, stderr);
-  if (!strstr(buf, strerror (1)))
+  ret = fgets (buf, 160, stderr);
+  if ((!ret && ferror(stderr)) || !strstr(buf, strerror (1)))
     failed = 1;
 
-  fgets (buf, 160, stderr);
-  if (strstr (buf, strerror (0)))
+  ret = fgets (buf, 160, stderr);
+  if ((!ret && ferror(stderr)) || strstr (buf, strerror (0)))
     failed = 1;
 
-  fgets (buf, 160, stderr);
-  if (strstr (buf, strerror (2)))
+  ret = fgets (buf, 160, stderr);
+  if ((!ret && ferror(stderr)) || strstr (buf, strerror (2)))
     failed = 1;
 
   return failed;
diff --git a/assert/test-assert.c b/assert/test-assert.c
index 26b58d4dd3..22d250317c 100644
--- a/assert/test-assert.c
+++ b/assert/test-assert.c
@@ -48,6 +48,7 @@ main (void)
 {
 
   volatile int failed = 1;
+  char *ret;
 
   fclose (stderr);
   stderr = tmpfile ();
@@ -72,16 +73,16 @@ main (void)
     failed = 1; /* should not happen */
 
   rewind (stderr);
-  fgets (buf, 160, stderr);
-  if (!strstr (buf, "1 == 2"))
+  ret = fgets (buf, 160, stderr);
+  if ((!ret && ferror(stderr)) || !strstr (buf, "1 == 2"))
     failed = 1;
 
-  fgets (buf, 160, stderr);
-  if (strstr (buf, "1 == 1"))
+  ret = fgets (buf, 160, stderr);
+  if ((!ret && ferror(stderr)) || strstr (buf, "1 == 1"))
     failed = 1;
 
-  fgets (buf, 160, stderr);
-  if (strstr (buf, "2 == 3"))
+  ret = fgets (buf, 160, stderr);
+  if ((!ret && ferror(stderr)) || strstr (buf, "2 == 3"))
     failed = 1;
 
   return failed;
diff --git a/crypt/cert.c b/crypt/cert.c
index 32c4386caf..5b4277f76d 100644
--- a/crypt/cert.c
+++ b/crypt/cert.c
@@ -99,7 +99,11 @@ get8 (char *cp)
 	int i,j,t;
 
 	for(i=0;i<8;i++){
-		scanf("%2x",&t);
+		if (scanf("%2x",&t) < 1)
+		  {
+		    if(ferror(stdin))
+		      totfails++;
+		  }
 		if(feof(stdin))
 		  good_bye();
 		for(j=0; j<8 ; j++) {
diff --git a/dirent/tst-fdopendir.c b/dirent/tst-fdopendir.c
index 6321af1daa..ee645cf9af 100644
--- a/dirent/tst-fdopendir.c
+++ b/dirent/tst-fdopendir.c
@@ -22,7 +22,11 @@ do_test (void)
       return 1;
     }
 
-  write (fd, "hello", 5);
+  if (write (fd, "hello", 5) < 5)
+    {
+      printf ("Failed to write to file: %m\n");
+      return 1;
+    }
   close (fd);
 
   struct stat64 st;
@@ -43,7 +47,12 @@ do_test (void)
     }
 
   char buf[5];
-  read(fd, buf, sizeof (buf));
+  if (read(fd, buf, sizeof (buf)) < 0)
+    {
+      puts ("read failed");
+      return 1;
+    }
+
   close(fd);
 
   struct stat64 st2;
diff --git a/elf/tst-stackguard1.c b/elf/tst-stackguard1.c
index 2e65e36078..1a98a9da52 100644
--- a/elf/tst-stackguard1.c
+++ b/elf/tst-stackguard1.c
@@ -108,7 +108,7 @@ do_test (void)
 	  dup2 (fds[1], 2);
 	  close (fds[1]);
 
-	  system (command);
+	  if (system (command)) {}
 	  exit (0);
 	}
 
diff --git a/io/tst-copy_file_range.c b/io/tst-copy_file_range.c
index d1f3aaa5a9..bd64e9c42b 100644
--- a/io/tst-copy_file_range.c
+++ b/io/tst-copy_file_range.c
@@ -166,7 +166,7 @@ short_copy (void)
           inoff = 3;
           xlseek (infd, shift, SEEK_SET);
         }
-      ftruncate (outfd, 0);
+      xftruncate (outfd, 0);
       xlseek (outfd, 0, SEEK_SET);
       outoff = 0;
 
diff --git a/io/tst-faccessat.c b/io/tst-faccessat.c
index 7bdeed008c..5ef81b1451 100644
--- a/io/tst-faccessat.c
+++ b/io/tst-faccessat.c
@@ -96,7 +96,11 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  if (write (fd, "hello", 5) < 5)
+    {
+      printf ("Failed to write to file: %m\n");
+      return 1;
+    }
   puts ("file created");
 
   /* Before closing the file, try using this file descriptor to open
diff --git a/io/tst-fchmodat.c b/io/tst-fchmodat.c
index 7d4a8717ff..aeed98e0a8 100644
--- a/io/tst-fchmodat.c
+++ b/io/tst-fchmodat.c
@@ -98,7 +98,11 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  if (write (fd, "hello", 5) < 5)
+    {
+      puts ("failed to write to file");
+      return 1;
+    }
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-fchownat.c b/io/tst-fchownat.c
index e8adf6229f..b206c04200 100644
--- a/io/tst-fchownat.c
+++ b/io/tst-fchownat.c
@@ -106,7 +106,11 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  if (write (fd, "hello", 5) < 5)
+    {
+      printf ("Failed to write to file: %m\n");
+      return 1;
+    }
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-fstatat.c b/io/tst-fstatat.c
index 4766bb2e71..712cd5226d 100644
--- a/io/tst-fstatat.c
+++ b/io/tst-fstatat.c
@@ -94,7 +94,11 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  if (write (fd, "hello", 5) < 5)
+    {
+      puts ("failed to write to file");
+      return 1;
+    }
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-futimesat.c b/io/tst-futimesat.c
index 3d41721f42..0e1d104623 100644
--- a/io/tst-futimesat.c
+++ b/io/tst-futimesat.c
@@ -114,7 +114,11 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  if (write (fd, "hello", 5) < 5)
+    {
+      puts ("failed to write to file");
+      return 1;
+    }
   puts ("file created");
 
   struct_stat st1;
diff --git a/io/tst-linkat.c b/io/tst-linkat.c
index 97445b7954..92b1cc65f7 100644
--- a/io/tst-linkat.c
+++ b/io/tst-linkat.c
@@ -94,7 +94,11 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  if (write (fd, "hello", 5) < 5)
+    {
+      puts ("failed to write to file");
+      return 1;
+    }
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-openat.c b/io/tst-openat.c
index 741b8d0ad2..999fdb79ba 100644
--- a/io/tst-openat.c
+++ b/io/tst-openat.c
@@ -94,7 +94,11 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  if (write (fd, "hello", 5) < 5)
+    {
+      puts ("failed to write to file");
+      return 1;
+    }
 
   /* Before closing the file, try using this file descriptor to open
      another file.  This must fail.  */
diff --git a/io/tst-renameat.c b/io/tst-renameat.c
index 435302b52b..33885a4280 100644
--- a/io/tst-renameat.c
+++ b/io/tst-renameat.c
@@ -94,7 +94,11 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  if (write (fd, "hello", 5) < 5)
+    {
+      puts ("failed to write to file");
+      return 1;
+    }
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-symlinkat.c b/io/tst-symlinkat.c
index 214a8e348e..e9aba970c7 100644
--- a/io/tst-symlinkat.c
+++ b/io/tst-symlinkat.c
@@ -94,7 +94,11 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  if (write (fd, "hello", 5) < 5)
+    {
+      puts ("failed to write to file");
+      return 1;
+    }
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-unlinkat.c b/io/tst-unlinkat.c
index e21d56f9f7..027d91ba98 100644
--- a/io/tst-unlinkat.c
+++ b/io/tst-unlinkat.c
@@ -94,7 +94,11 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  if (write (fd, "hello", 5) < 5)
+    {
+      puts ("failed to write to file");
+      return 1;
+    }
   close (fd);
   puts ("file created");
 
diff --git a/libio/bug-fseek.c b/libio/bug-fseek.c
index 1b60580b53..509db8689e 100644
--- a/libio/bug-fseek.c
+++ b/libio/bug-fseek.c
@@ -48,7 +48,10 @@ do_test (void)
       perror ("fopen(\"r\")");
     }
 
-  fread (buf, 3, 1, f);
+  if (fread (buf, 3, 1, f) < 1)
+    {
+      perror ("fread");
+    }
   errno = 0;
   if (fseek (f, -10, SEEK_CUR) == 0)
     {
@@ -72,7 +75,10 @@ Got %d instead\n",
       perror ("fopen(\"r+\")");
     }
 
-  fread (buf, 3, 1, f);
+  if (fread (buf, 3, 1, f) < 1)
+    {
+      perror ("fread");
+    }
   errno = 0;
   if (fseek (f, -10, SEEK_CUR) == 0)
     {
@@ -96,7 +102,10 @@ Got %d instead\n",
       perror ("fopen(\"r+\")");
     }
 
-  fread (buf, 3, 1, f);
+  if (fread (buf, 3, 1, f) < 1)
+    {
+      perror ("fread");
+    }
   if (ftell (f) != 3)
     {
       puts ("ftell failed");
diff --git a/libio/bug-mmap-fflush.c b/libio/bug-mmap-fflush.c
index d8aa58985a..697d39816e 100644
--- a/libio/bug-mmap-fflush.c
+++ b/libio/bug-mmap-fflush.c
@@ -35,14 +35,22 @@ do_test (void)
   char buffer[1024];
 
   snprintf (buffer, sizeof (buffer), "echo 'From foo@bar.com' > %s", fname);
-  system (buffer);
+  if (system (buffer))
+    {
+      perror ("Failed to execute echo command %m\n");
+      exit (1);
+    }
   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);
+  if (system (buffer))
+    {
+      perror ("Failed to execute echo command %m\n");
+      exit (1);
+    }
   fseek (f, o, SEEK_SET);
   if (fgets (buffer, 1024, f) == NULL)
     exit (1);
diff --git a/libio/bug-ungetc.c b/libio/bug-ungetc.c
index 51940b68f5..5db5417170 100644
--- a/libio/bug-ungetc.c
+++ b/libio/bug-ungetc.c
@@ -20,7 +20,12 @@ do_prepare (void)
       printf ("cannot create temporary file: %m\n");
       exit (1);
     }
-  write (fd, pattern, sizeof (pattern));
+
+  if (write (fd, pattern, sizeof (pattern)) < sizeof (pattern))
+    {
+      printf ("cannot write to temporary file: %m\n");
+      exit (1);
+    }
   close (fd);
 }
 
diff --git a/libio/bug-ungetc3.c b/libio/bug-ungetc3.c
index 0c83c1161e..2afe000d2a 100644
--- a/libio/bug-ungetc3.c
+++ b/libio/bug-ungetc3.c
@@ -20,7 +20,12 @@ do_prepare (void)
       printf ("cannot create temporary file: %m\n");
       exit (1);
     }
-  write (fd, pattern, sizeof (pattern));
+  if (write (fd, pattern, sizeof (pattern)) < sizeof (pattern))
+    {
+      printf ("cannot write to temporary file: %m\n");
+      exit (1);
+    }
+
   close (fd);
 }
 
diff --git a/libio/bug-ungetc4.c b/libio/bug-ungetc4.c
index 0bd02a570d..df47bcd263 100644
--- a/libio/bug-ungetc4.c
+++ b/libio/bug-ungetc4.c
@@ -36,7 +36,12 @@ do_prepare (void)
       printf ("cannot create temporary file: %m\n");
       exit (1);
     }
-  write (fd, pattern, sizeof (pattern) - 1);
+
+  if (write (fd, pattern, sizeof (pattern) - 1) < sizeof (pattern) - 1)
+    {
+      printf ("cannot write to temporary file: %m\n");
+      exit (1);
+    }
   close (fd);
 }
 
diff --git a/libio/bug-wfflush.c b/libio/bug-wfflush.c
index a8fd61e997..771c32e17b 100644
--- a/libio/bug-wfflush.c
+++ b/libio/bug-wfflush.c
@@ -20,7 +20,11 @@ do_prepare (void)
       printf ("cannot create temporary file: %m\n");
       exit (1);
     }
-  write (fd, "1!", 2);
+  if (write (fd, "1!", 2) < 2)
+    {
+      printf ("cannot write to temporary file: %m\n");
+      exit (1);
+    }
   close (fd);
 }
 
diff --git a/libio/bug-wsetpos.c b/libio/bug-wsetpos.c
index ccb22a4b62..39180ccee1 100644
--- a/libio/bug-wsetpos.c
+++ b/libio/bug-wsetpos.c
@@ -22,7 +22,12 @@ do_prepare (void)
       printf ("cannot create temporary file: %m\n");
       exit (1);
     }
-  write (fd, pattern, sizeof (pattern));
+  if (write (fd, pattern, sizeof (pattern)) < sizeof (pattern))
+    {
+      printf ("cannot write to temporary file: %m\n");
+      exit (1);
+    }
+
   close (fd);
 }
 
diff --git a/misc/tst-efgcvt-template.c b/misc/tst-efgcvt-template.c
index b924659a3d..87e3ebe4fa 100644
--- a/misc/tst-efgcvt-template.c
+++ b/misc/tst-efgcvt-template.c
@@ -200,8 +200,8 @@ special (void)
     output_error (NAME (ECVT), INFINITY, 10, "inf", 0, 0, p, decpt, sign);
 
   /* Simply make sure these calls with large NDIGITs don't crash.  */
-  (void) ECVT (123.456, 10000, &decpt, &sign);
-  (void) FCVT (123.456, 10000, &decpt, &sign);
+  p = ECVT (123.456, 10000, &decpt, &sign);
+  p = FCVT (123.456, 10000, &decpt, &sign);
 
   /* Some tests for the reentrant functions.  */
   /* Use a too small buffer.  */
diff --git a/misc/tst-error1.c b/misc/tst-error1.c
index 9c4a62fbd0..65e3fd0c0e 100644
--- a/misc/tst-error1.c
+++ b/misc/tst-error1.c
@@ -9,7 +9,7 @@ static int
 do_test (int argc, char *argv[])
 {
   mtrace ();
-  (void) freopen (argc == 1 ? "/dev/stdout" : argv[1], "a", stderr);
+  if (freopen (argc == 1 ? "/dev/stdout" : argv[1], "a", stderr)) {}
   /* Orient the stream.  */
   fwprintf (stderr, L"hello world\n");
   char buf[20000];
diff --git a/nptl/tst-cancel7.c b/nptl/tst-cancel7.c
index 2835613a9b..e4196d9d11 100644
--- a/nptl/tst-cancel7.c
+++ b/nptl/tst-cancel7.c
@@ -43,7 +43,7 @@ tf (void *arg)
 {
   char *cmd = xasprintf ("%s --direct --sem %s --pidfile %s",
 			 command, semfilename, pidfilename);
-  system (cmd);
+  if (system (cmd)) {};
   /* This call should never return.  */
   return NULL;
 }
diff --git a/nptl/tst-cleanup4.c b/nptl/tst-cleanup4.c
index 1d3d53fb5f..64206c4ab6 100644
--- a/nptl/tst-cleanup4.c
+++ b/nptl/tst-cleanup4.c
@@ -64,7 +64,11 @@ fn_read (void)
     }
 
   char c;
-  read (fds[0], &c, 1);
+  if (read (fds[0], &c, 1) < 1)
+    {
+      printf ("%s: read failed\n", __FUNCTION__);
+      exit (1);
+    }
 }
 
 
diff --git a/nptl/tst-stackguard1.c b/nptl/tst-stackguard1.c
index b9cf6844de..96cfcb02f7 100644
--- a/nptl/tst-stackguard1.c
+++ b/nptl/tst-stackguard1.c
@@ -96,7 +96,11 @@ do_test (void)
 	else if (ret != NULL)
 	  return 1;
 
-      write (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy));
+      if (write (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy)) < 0)
+	{
+	  puts("failed to write stack_chk_guard_copy");
+	  return 1;
+	}
       return 0;
     }
 
@@ -138,7 +142,7 @@ do_test (void)
 	  dup2 (fds[1], 2);
 	  close (fds[1]);
 
-	  system (command);
+	  if (system (command)) {}
 	  exit (0);
 	}
 
diff --git a/nptl/tst-tls3mod.c b/nptl/tst-tls3mod.c
index c6e8910b1e..ba9e012204 100644
--- a/nptl/tst-tls3mod.c
+++ b/nptl/tst-tls3mod.c
@@ -43,7 +43,7 @@ handler (int sig)
 {
   if (sig != THE_SIG)
     {
-      write (STDOUT_FILENO, "wrong signal\n", 13);
+      if (write (STDOUT_FILENO, "wrong signal\n", 13) < 13) {}
       _exit (1);
     }
 
@@ -51,7 +51,7 @@ handler (int sig)
 
   if (sem_post (&s) != 0)
     {
-      write (STDOUT_FILENO, "sem_post failed\n", 16);
+      if (write (STDOUT_FILENO, "sem_post failed\n", 16) < 16) {}
       _exit (1);
     }
 }
diff --git a/nss/tst-nss-db-endpwent.c b/nss/tst-nss-db-endpwent.c
index 2b0fc1b064..519b73c41e 100644
--- a/nss/tst-nss-db-endpwent.c
+++ b/nss/tst-nss-db-endpwent.c
@@ -55,7 +55,11 @@ do_test (void)
 
   cmd = xasprintf ("%s/makedb -o /var/db/passwd.db /var/db/passwd.in",
 		   support_bindir_prefix);
-  system (cmd);
+  if (system (cmd) < 0)
+    {
+      printf("failed to prepare the test");
+      return 1;
+    }
   free (cmd);
 
   try_it ();
diff --git a/nss/tst-reload2.c b/nss/tst-reload2.c
index ba9b5b7687..b25e5e3528 100644
--- a/nss/tst-reload2.c
+++ b/nss/tst-reload2.c
@@ -121,7 +121,11 @@ do_test (void)
   /* Change the root dir.  */
 
   TEST_VERIFY (chroot ("/subdir") == 0);
-  chdir ("/");
+  if (chdir ("/") < 0)
+    {
+      printf("Failed to change directory: %m");
+      return 1;
+    }
 
   /* Check we're NOT using the "inner" nsswitch.conf.  */
 
diff --git a/posix/tst-chmod.c b/posix/tst-chmod.c
index b98a05a265..bec2d2b8eb 100644
--- a/posix/tst-chmod.c
+++ b/posix/tst-chmod.c
@@ -229,7 +229,12 @@ do_test (int argc, char *argv[])
   close (fd);
 
   snprintf (buf, buflen, "%s/..", testdir);
-  chdir (buf);
+  if (chdir (buf))
+    {
+      printf ("cannot change directory: %m\n");
+      result = 1;
+      goto fail;
+    }
   /* We are now in the directory above the one we create the test
      directory in.  */
 
@@ -349,7 +354,7 @@ do_test (int argc, char *argv[])
     }
 
  fail:
-  chdir (startdir);
+  if (chdir (startdir)) {}
 
   /* Remove all the files.  */
   chmod (testdir, 0700);
diff --git a/posix/tst-execl2.c b/posix/tst-execl2.c
index 5b74959ef8..3c22ecce5c 100644
--- a/posix/tst-execl2.c
+++ b/posix/tst-execl2.c
@@ -18,8 +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)
+
+  if (asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]) < 0 || buf == NULL)
     {
       puts ("asprintf  failed");
       exit (1);
diff --git a/posix/tst-execle2.c b/posix/tst-execle2.c
index 0430b7b573..28d3071dd9 100644
--- a/posix/tst-execle2.c
+++ b/posix/tst-execle2.c
@@ -18,8 +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)
+
+  if (asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]) < 0 || buf == NULL)
     {
       puts ("asprintf  failed");
       exit (1);
diff --git a/posix/tst-execlp2.c b/posix/tst-execlp2.c
index 81a723dda4..797a320bb5 100644
--- a/posix/tst-execlp2.c
+++ b/posix/tst-execlp2.c
@@ -22,8 +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)
+
+  if (asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]) < 0 || buf == NULL)
     {
       puts ("asprintf  failed");
       exit (1);
@@ -59,8 +59,7 @@ do_test (void)
       return 1;
     }
   char *path;
-  asprintf (&path, "%s:../libio:../elf", bindir);
-  if (path == NULL)
+  if (asprintf (&path, "%s:../libio:../elf", bindir) < 0 || path == NULL)
     {
       puts ("asprintf  failed");
       return 1;
diff --git a/posix/tst-execv2.c b/posix/tst-execv2.c
index a5168a269c..2de3286a6d 100644
--- a/posix/tst-execv2.c
+++ b/posix/tst-execv2.c
@@ -18,8 +18,7 @@ prepare (int argc, char *argv[])
 {
   char *buf;
   int off;
-  asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]);
-  if (buf == NULL)
+  if (asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]) < 0 || buf == NULL)
     {
       puts ("asprintf  failed");
       exit (1);
diff --git a/posix/tst-execve2.c b/posix/tst-execve2.c
index 1a804e94fd..57da680536 100644
--- a/posix/tst-execve2.c
+++ b/posix/tst-execve2.c
@@ -18,8 +18,7 @@ prepare (int argc, char *argv[])
 {
   char *buf;
   int off;
-  asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]);
-  if (buf == NULL)
+  if (asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]) < 0 || buf == NULL)
     {
       puts ("asprintf  failed");
       exit (1);
diff --git a/posix/tst-execvp2.c b/posix/tst-execvp2.c
index 440dfab438..ab6c3ee175 100644
--- a/posix/tst-execvp2.c
+++ b/posix/tst-execvp2.c
@@ -25,8 +25,7 @@ prepare (int argc, char *argv[])
 {
   char *buf;
   int off;
-  asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]);
-  if (buf == NULL)
+  if (asprintf (&buf, "cp %s %n%s-copy", argv[0], &off, argv[0]) < 0 || buf == NULL)
     {
       puts ("asprintf  failed");
       exit (1);
@@ -62,8 +61,7 @@ do_test (void)
       return 1;
     }
   char *path;
-  asprintf (&path, "%s:../libio:../elf", bindir);
-  if (path == NULL)
+  if (asprintf (&path, "%s:../libio:../elf", bindir) < 0 || path == NULL)
     {
       puts ("asprintf  failed");
       return 1;
diff --git a/posix/tst-getopt-cancel.c b/posix/tst-getopt-cancel.c
index 7167d1a914..24771ee82e 100644
--- a/posix/tst-getopt-cancel.c
+++ b/posix/tst-getopt-cancel.c
@@ -48,7 +48,7 @@ check_stderr (bool expect_errmsg, FILE *stderr_trapped)
       fputs (lineptr, stdout);
     }
   rewind (stderr_trapped);
-  ftruncate (fileno (stderr_trapped), 0);
+  if (ftruncate (fileno (stderr_trapped), 0) < 0) {}
   return got_errmsg == expect_errmsg;
 }
 
diff --git a/posix/tst-nice.c b/posix/tst-nice.c
index fe9888b3f6..59cf953e27 100644
--- a/posix/tst-nice.c
+++ b/posix/tst-nice.c
@@ -58,8 +58,7 @@ do_test (void)
 
   /* BZ #18086. Make sure we don't reset errno.  */
   errno = EBADF;
-  nice (0);
-  if (errno != EBADF)
+  if (nice (0) == -1 || errno != EBADF)
     {
       printf ("FAIL: errno = %i, but wanted EBADF (%i)\n", errno, EBADF);
       return 1;
diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c
index bae27d6cee..057c89dd3c 100644
--- a/posix/wordexp-test.c
+++ b/posix/wordexp-test.c
@@ -253,7 +253,11 @@ do_test (int argc, char *argv[])
   cwd = getcwd (NULL, 0);
 
   /* Set up arena for pathname expansion */
-  tmpnam (tmpdir);
+  if (!tmpnam (tmpdir))
+    {
+      printf ("Failed to create a temporary directory with a unique name: %m");
+      return 1;
+    }
   xmkdir (tmpdir, S_IRWXU);
   TEST_VERIFY_EXIT (chdir (tmpdir) == 0);
 
@@ -332,7 +336,11 @@ do_test (int argc, char *argv[])
   if (cwd == NULL)
     cwd = "..";
 
-  chdir (cwd);
+  if (chdir (cwd) < 0)
+    {
+      printf ("failed to change dir: %m");
+      return 1;
+    }
   rmdir (tmpdir);
 
   return 0;
diff --git a/rt/tst-cpuclock2.c b/rt/tst-cpuclock2.c
index e4584d8791..22f20870f8 100644
--- a/rt/tst-cpuclock2.c
+++ b/rt/tst-cpuclock2.c
@@ -55,7 +55,7 @@ chew_cpu (void *arg)
       for (int i = 0; i < 100; ++i)
 	for (size_t j = 0; j < sizeof buf; ++j)
 	  buf[j] = 0xbb;
-      write (nullfd, (char *) buf, sizeof buf);
+      if (write (nullfd, (char *) buf, sizeof buf)) {};
       close (nullfd);
     }
 
diff --git a/rt/tst-cputimer1.c b/rt/tst-cputimer1.c
index 8f5dd76cf2..ec75424a83 100644
--- a/rt/tst-cputimer1.c
+++ b/rt/tst-cputimer1.c
@@ -29,7 +29,7 @@ chew_cpu (void *arg)
       for (int i = 0; i < 100; ++i)
 	for (size_t j = 0; j < sizeof buf; ++j)
 	  buf[j] = 0xbb;
-      write (nullfd, (char *) buf, sizeof buf);
+      if (write (nullfd, (char *) buf, sizeof buf)) {}
       close (nullfd);
     }
 
diff --git a/rt/tst-cputimer2.c b/rt/tst-cputimer2.c
index 397d7998c0..7fb78fe485 100644
--- a/rt/tst-cputimer2.c
+++ b/rt/tst-cputimer2.c
@@ -32,7 +32,7 @@ chew_cpu (void *arg)
       for (int i = 0; i < 100; ++i)
 	for (size_t j = 0; j < sizeof buf; ++j)
 	  buf[j] = 0xbb;
-      write (nullfd, (char *) buf, sizeof buf);
+      if (write (nullfd, (char *) buf, sizeof buf)) {}
       close (nullfd);
     }
 
diff --git a/rt/tst-cputimer3.c b/rt/tst-cputimer3.c
index 056766a377..e8661b9cfc 100644
--- a/rt/tst-cputimer3.c
+++ b/rt/tst-cputimer3.c
@@ -33,7 +33,7 @@ chew_cpu (void)
       for (int i = 0; i < 100; ++i)
 	for (size_t j = 0; j < sizeof buf; ++j)
 	  buf[j] = 0xbb;
-      write (nullfd, (char *) buf, sizeof buf);
+      if (write (nullfd, (char *) buf, sizeof buf)) {}
       close (nullfd);
       if (getppid () == 1)
 	_exit (2);
diff --git a/stdio-common/bug12.c b/stdio-common/bug12.c
index 48610c0e78..820e91f618 100644
--- a/stdio-common/bug12.c
+++ b/stdio-common/bug12.c
@@ -20,24 +20,29 @@ main (void)
     }
   rewind (f);
 
-  fread (m, 4096 * 4 - 10, 1, f);
-  fread (b, 20, 1, f);
+  if (fread (m, 4096 * 4 - 10, 1, f) < 1)
+    failed = 1;
+  if (fread (b, 20, 1, f) < 1)
+    failed = 1;
   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);
+  if (fread (b, 20, 1, f) < 1)
+    failed = 1;
   printf ("got %s (should be %s)\n", b, "zzzzzzzzzzzzzzzzzzzz");
   if (strcmp (b, "zzzzzzzzzzzzzzzzzzzz"))
     failed = 1;
 
-  fread (b, 20, 1, f);
+  if (fread (b, 20, 1, f) < 1)
+    failed = 1;
   printf ("got %s (should be %s)\n", b, "zzzzzzzzzzxxxxxxxxxx");
   if (strcmp (b, "zzzzzzzzzzxxxxxxxxxx"))
     failed = 1;
 
-  fread (b, 20, 1, f);
+  if (fread (b, 20, 1, f) < 1)
+    failed = 1;
   printf ("got %s (should be %s)\n", b, "xxxxxxxxxxxxxxxxxxxx");
   if (strcmp (b, "xxxxxxxxxxxxxxxxxxxx"))
     failed = 1;
diff --git a/stdio-common/bug19.c b/stdio-common/bug19.c
index e083304bda..9a3deac3fc 100644
--- a/stdio-common/bug19.c
+++ b/stdio-common/bug19.c
@@ -29,12 +29,17 @@ do_test (void)
   printf("checking sscanf\n");
 
   int i, j, n;
+  int result = 0;
 
   i = j = n = 0;
-  FSCANF (fp, L(" %i - %i %n"), &i, &j, &n);
+  if (FSCANF (fp, L(" %i - %i %n"), &i, &j, &n) < 2)
+    {
+      printf ("FSCANF couldn't read all parameters %d\n", errno);
+      result = 1;
+    }
+
   printf ("found %i-%i (length=%i)\n", i, j, n);
 
-  int result = 0;
   if (i != 7)
     {
       printf ("i is %d, expected 7\n", i);
diff --git a/stdio-common/bug3.c b/stdio-common/bug3.c
index 62a6cab330..c764a5a442 100644
--- a/stdio-common/bug3.c
+++ b/stdio-common/bug3.c
@@ -32,7 +32,11 @@ main (void)
 	      char buf[25];
 
 	      buf[0] = j;
-	      fread (buf + 1, 1, 23, f);
+	      if (fread (buf + 1, 1, 23, f) < 23)
+		{
+		  printf ("%s\nTest FAILED!\n", buf);
+		  return 1;
+		}
 	      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..d587dfcfd4 100644
--- a/stdio-common/bug4.c
+++ b/stdio-common/bug4.c
@@ -30,7 +30,11 @@ 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);
+  if (fread (buffer, 1, 31, f) < 31)
+    {
+      puts ("\nTest FAILED!");
+      return 1;
+    }
   fwrite (buffer, 1, 31, stdout);
   fclose (f);
   remove (filename);
diff --git a/stdio-common/bug5.c b/stdio-common/bug5.c
index 7bfe9b2b8d..e6ee6ceb41 100644
--- a/stdio-common/bug5.c
+++ b/stdio-common/bug5.c
@@ -60,7 +60,11 @@ main (void)
      the perhaps incompatible new shared libraries.  */
   unsetenv ("LD_LIBRARY_PATH");
 
-  asprintf (&printbuf, "cmp %s %s", inname, outname);
+  if (asprintf (&printbuf, "cmp %s %s", inname, outname) < 0)
+    {
+      perror ("asprintf");
+      return 1;
+    }
   result = system (printbuf);
   remove (inname);
   remove (outname);
diff --git a/stdio-common/bug6.c b/stdio-common/bug6.c
index 0db63a3b44..50098bf3f2 100644
--- a/stdio-common/bug6.c
+++ b/stdio-common/bug6.c
@@ -7,16 +7,16 @@ main (void)
   int i;
   int lost = 0;
 
-  scanf ("%2s", buf);
+  lost = (scanf ("%2s", buf) < 0);
   lost |= (buf[0] != 'X' || buf[1] != 'Y' || buf[2] != '\0');
   if (lost)
     puts ("test of %2s failed.");
-  scanf (" ");
-  scanf ("%d", &i);
+  lost |= (scanf (" ") < 0);
+  lost |= (scanf ("%d", &i) < 0);
   lost |= (i != 1234);
   if (lost)
     puts ("test of %d failed.");
-  scanf ("%c", buf);
+  lost |= (scanf ("%c", buf) < 0);
   lost |= (buf[0] != 'L');
   if (lost)
     puts ("test of %c failed.\n");
diff --git a/stdio-common/test-fwrite.c b/stdio-common/test-fwrite.c
index 5677c6da80..3f39f3fd8b 100644
--- a/stdio-common/test-fwrite.c
+++ b/stdio-common/test-fwrite.c
@@ -57,11 +57,15 @@ do_test (void)
       return 1;
     }
 
-  asprintf (&line, "\
+  if (asprintf (&line, "\
 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\
-");
+") < 0)
+    {
+      perror ("asprintf");
+      return 1;
+    }
 
   puts ("Test succeeded.");
   return 0;
diff --git a/stdio-common/test_rdwr.c b/stdio-common/test_rdwr.c
index 7825ca9358..9d7834c852 100644
--- a/stdio-common/test_rdwr.c
+++ b/stdio-common/test_rdwr.c
@@ -49,7 +49,8 @@ main (int argc, char **argv)
 
   (void) fputs (hello, f);
   rewind (f);
-  (void) fgets (buf, sizeof (buf), f);
+  if (!fgets (buf, sizeof (buf), f) && ferror (f))
+	  lose = 1;
   rewind (f);
   (void) fputs (buf, f);
   rewind (f);
diff --git a/stdio-common/tst-cookie.c b/stdio-common/tst-cookie.c
index 030e684562..76df5b4d13 100644
--- a/stdio-common/tst-cookie.c
+++ b/stdio-common/tst-cookie.c
@@ -77,7 +77,9 @@ do_test (void)
 
   f = fopencookie (THE_COOKIE, "r+", fcts);
 
-  fread (buf, 1, 1, f);
+  if (fread (buf, 1, 1, f) < 1)
+    ++errors;
+
   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 3cc2832edc..971d0bcf5e 100644
--- a/stdio-common/tst-fmemopen3.c
+++ b/stdio-common/tst-fmemopen3.c
@@ -153,7 +153,11 @@ 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);
+  if (fread (tmp, tmps, 1, fp) < 1)
+    {
+      printf ("fread failed");
+      return 1;
+    }
 
   if (memcmp (tmp, expected, tmps) != 0)
     {
diff --git a/stdio-common/tst-fseek.c b/stdio-common/tst-fseek.c
index c4ac17cdf4..bacc390c27 100644
--- a/stdio-common/tst-fseek.c
+++ b/stdio-common/tst-fseek.c
@@ -44,8 +44,7 @@ do_test (void)
   if (tmpdir == NULL || tmpdir[0] == '\0')
     tmpdir = "/tmp";
 
-  asprintf (&fname, "%s/tst-fseek.XXXXXX", tmpdir);
-  if (fname == NULL)
+  if (asprintf (&fname, "%s/tst-fseek.XXXXXX", tmpdir) < 0 || fname == NULL)
     error (EXIT_FAILURE, errno, "cannot generate name for temporary file");
 
   /* Create a temporary file.   */
diff --git a/stdio-common/tst-perror.c b/stdio-common/tst-perror.c
index 57835e0c59..af0bbc261a 100644
--- a/stdio-common/tst-perror.c
+++ b/stdio-common/tst-perror.c
@@ -94,7 +94,11 @@ do_test (void)
     puts ("multibyte test succeeded");
 
   lseek (fd, 0, SEEK_SET);
-  ftruncate (fd, 0);
+  if (ftruncate (fd, 0) < 0)
+    {
+      printf ("cannot truncate file descriptor: %m\n");
+      exit (EXIT_FAILURE);
+    }
 
   if (dup2 (fd, 2) == -1)
     {
diff --git a/stdio-common/tstscanf.c b/stdio-common/tstscanf.c
index 3a4ebf7524..7e92df4720 100644
--- a/stdio-common/tstscanf.c
+++ b/stdio-common/tstscanf.c
@@ -120,7 +120,12 @@ main (int argc, char **argv)
     int i;
     float x;
     char name[50];
-    (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
+    if (fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name) < 3)
+      {
+	fputs ("test failed!\n", stdout);
+	result = 1;
+      }
+
     fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
     if (i != 56 || x != 789.0F || strcmp (name, "56"))
       {
@@ -164,7 +169,12 @@ main (int argc, char **argv)
 	quant = 0.0;
 	units[0] = item[0] = '\0';
 	count = fscanf (in, "%f%20s of %20s", &quant, units, item);
-	(void) fscanf (in, "%*[^\n]");
+	if (fscanf (in, "%*[^\n]") < 0 && ferror (in))
+	  {
+	    fputs ("test failed!\n", stdout);
+	    result = 1;
+	  }
+
 	fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
 		 count, quant, item, units);
 	if (count != ok[rounds-1].count || quant != ok[rounds-1].quant
diff --git a/stdlib/test-canon.c b/stdlib/test-canon.c
index 4edee73dd8..61fe893609 100644
--- a/stdlib/test-canon.c
+++ b/stdlib/test-canon.c
@@ -123,8 +123,15 @@ do_test (int argc, char ** argv)
   int i, errors = 0;
   char buf[PATH_MAX];
 
-  getcwd (cwd, sizeof (buf));
-  cwd_len = strlen (cwd);
+  if (getcwd (cwd, sizeof (buf)))
+    {
+      cwd_len = strlen (cwd);
+    }
+  else
+    {
+      printf ("%s: current working directory couldn't be retrieved\n", argv[0]);
+      ++errors;
+    }
 
   errno = 0;
   if (realpath (NULL, buf) != NULL || errno != EINVAL)
@@ -154,7 +161,12 @@ do_test (int argc, char ** argv)
     }
 
   for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
-    symlink (symlinks[i].value, symlinks[i].name);
+    if (symlink (symlinks[i].value, symlinks[i].name))
+      {
+        printf ("%s: Unable to create symlink for %s -> %s\n",
+		argv[0], symlinks[i].name, symlinks[i].value);
+        ++errors;
+      }
 
   int has_dir = mkdir ("doesExist", 0777) == 0;
 
@@ -205,7 +217,12 @@ do_test (int argc, char ** argv)
       free (result2);
     }
 
-  getcwd (buf, sizeof (buf));
+  if (!getcwd (buf, sizeof (buf)))
+    {
+      printf ("%s: current working directory couldn't be retrieved\n", argv[0]);
+      ++errors;
+    }
+
   if (strcmp (buf, cwd))
     {
       printf ("%s: current working directory changed from %s to %s\n",
diff --git a/support/test-container.c b/support/test-container.c
index e68f16eecf..f8d808f331 100644
--- a/support/test-container.c
+++ b/support/test-container.c
@@ -714,8 +714,8 @@ check_for_unshare_hints (int require_pidns)
         continue;
 
       val = -1; /* Sentinel.  */
-      fscanf (f, "%d", &val);
-      if (val != files[i].bad_value)
+      int cnt = fscanf (f, "%d", &val);
+      if (cnt == 1 && val != files[i].bad_value)
 	continue;
 
       printf ("To enable test-container, please run this as root:\n");
@@ -1186,7 +1186,7 @@ main (int argc, char **argv)
       int status;
 
       /* Send the child's "outside" pid to it.  */
-      write (pipes[1], &child, sizeof(child));
+      xwrite (pipes[1], &child, sizeof(child));
       close (pipes[0]);
       close (pipes[1]);
 
@@ -1217,7 +1217,9 @@ 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));
+  if (read (pipes[0], &child, sizeof(child)) < sizeof(child))
+    FAIL_EXIT1 ("Unable to read PID");
+
   close (pipes[0]);
   close (pipes[1]);
   sprintf (pid_buf, "%lu", (long unsigned)child);
@@ -1255,7 +1257,8 @@ main (int argc, char **argv)
 
       sprintf (tmp, "%lld %lld 1\n",
 	       (long long) (be_su ? 0 : original_uid), (long long) original_uid);
-      write (UMAP, tmp, strlen (tmp));
+      xwrite (UMAP, tmp, strlen (tmp));
+
       xclose (UMAP);
 
       /* We must disable setgroups () before we can map our groups, else we
@@ -1264,7 +1267,7 @@ main (int argc, char **argv)
       if (GMAP >= 0)
 	{
 	  /* We support kernels old enough to not have this.  */
-	  write (GMAP, "deny\n", 5);
+	  xwrite (GMAP, "deny\n", 5);
 	  xclose (GMAP);
 	}
 
@@ -1276,7 +1279,8 @@ main (int argc, char **argv)
 
       sprintf (tmp, "%lld %lld 1\n",
 	       (long long) (be_su ? 0 : original_gid), (long long) original_gid);
-      write (GMAP, tmp, strlen (tmp));
+      xwrite (GMAP, tmp, strlen (tmp));
+
       xclose (GMAP);
     }
 
diff --git a/sysdeps/pthread/tst-cancel11.c b/sysdeps/pthread/tst-cancel11.c
index 4dd84d6673..4e577f97ad 100644
--- a/sysdeps/pthread/tst-cancel11.c
+++ b/sysdeps/pthread/tst-cancel11.c
@@ -56,7 +56,11 @@ tf (void *arg)
 
   /* This call should block and be cancelable.  */
   char buf[20];
-  read (fd[0], buf, sizeof (buf));
+  if (read (fd[0], buf, sizeof (buf)) < sizeof (buf))
+    {
+      puts ("tf: read failed");
+      exit (1);
+    }
 
   pthread_cleanup_pop (0);
 
diff --git a/sysdeps/pthread/tst-cancel16.c b/sysdeps/pthread/tst-cancel16.c
index 511b9e1e91..d47c7c68cb 100644
--- a/sysdeps/pthread/tst-cancel16.c
+++ b/sysdeps/pthread/tst-cancel16.c
@@ -50,7 +50,11 @@ tf (void *arg)
   pthread_cleanup_push (cl, NULL);
 
   /* This call should never return.  */
-  (void) lockf (fd, F_LOCK, 0);
+  if (lockf (fd, F_LOCK, 0))
+    {
+      puts ("child thread: lockf failed");
+      exit (1);
+    }
 
   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-cancel4.c b/sysdeps/pthread/tst-cancel4.c
index 4f5c89314c..4c9e8670ca 100644
--- a/sysdeps/pthread/tst-cancel4.c
+++ b/sysdeps/pthread/tst-cancel4.c
@@ -1009,7 +1009,8 @@ tf_pread (void *arg)
   pthread_cleanup_push (cl, NULL);
 
   char mem[10];
-  pread (tempfd, mem, sizeof (mem), 0);
+  if (pread (tempfd, mem, sizeof (mem), 0) < 0)
+    FAIL_EXIT1 ("pread failed: %m");
 
   pthread_cleanup_pop (0);
 
@@ -1038,7 +1039,8 @@ tf_pwrite (void *arg)
   pthread_cleanup_push (cl, NULL);
 
   char mem[10];
-  pwrite (tempfd, mem, sizeof (mem), 0);
+  if (pwrite (tempfd, mem, sizeof (mem), 0) <0)
+    FAIL_EXIT1 ("pwrite failed: %m");
 
   pthread_cleanup_pop (0);
 
diff --git a/sysdeps/pthread/tst-cancel6.c b/sysdeps/pthread/tst-cancel6.c
index 63e6d49707..11a16cd564 100644
--- a/sysdeps/pthread/tst-cancel6.c
+++ b/sysdeps/pthread/tst-cancel6.c
@@ -25,7 +25,8 @@ static void *
 tf (void *arg)
 {
   char buf[100];
-  fgets (buf, sizeof (buf), arg);
+  if (!fgets (buf, sizeof (buf), arg))
+    puts ("fgets failed");
   /* This call should never return.  */
   return NULL;
 }
diff --git a/sysdeps/pthread/tst-cond18.c b/sysdeps/pthread/tst-cond18.c
index edac4fa4ff..ceeecbd70c 100644
--- a/sysdeps/pthread/tst-cond18.c
+++ b/sysdeps/pthread/tst-cond18.c
@@ -40,7 +40,7 @@ tf (void *id)
       while (!exiting)
 	{
 	  if ((spins++ % 1000) == 0)
-	    write (fd, ".", 1);
+	    if(write (fd, ".", 1)) {}
 	  pthread_mutex_unlock (&lock);
 
 	  pthread_mutex_lock (&lock);
diff --git a/sysdeps/pthread/tst-fini1mod.c b/sysdeps/pthread/tst-fini1mod.c
index cdadf034cd..cee1e3838b 100644
--- a/sysdeps/pthread/tst-fini1mod.c
+++ b/sysdeps/pthread/tst-fini1mod.c
@@ -32,7 +32,11 @@ tf (void *arg)
     }
 
   char buf[10];
-  read (fds[0], buf, sizeof (buf));
+  if (read (fds[0], buf, sizeof (buf)) < sizeof (buf))
+    {
+      puts ("can't read enough");
+      exit (1);
+    }
 
   puts ("read returned");
   exit (1);
diff --git a/sysdeps/pthread/tst-flock1.c b/sysdeps/pthread/tst-flock1.c
index 7eef9070ab..660f3f1e3e 100644
--- a/sysdeps/pthread/tst-flock1.c
+++ b/sysdeps/pthread/tst-flock1.c
@@ -56,7 +56,11 @@ do_test (void)
 
   unlink (tmp);
 
-  write (fd, "foobar xyzzy", 12);
+  if (write (fd, "foobar xyzzy", 12) < 12)
+    {
+      puts ("write failed");
+      exit (1);
+    }
 
   if (flock (fd, LOCK_EX | LOCK_NB) != 0)
     {
diff --git a/sysdeps/pthread/tst-flock2.c b/sysdeps/pthread/tst-flock2.c
index 8762e93b52..122ffe296f 100644
--- a/sysdeps/pthread/tst-flock2.c
+++ b/sysdeps/pthread/tst-flock2.c
@@ -70,7 +70,11 @@ do_test (void)
 
   int i;
   for (i = 0; i < 20; ++i)
-    write (fd, "foobar xyzzy", 12);
+    if (write (fd, "foobar xyzzy", 12) < 12)
+      {
+	puts ("write failed");
+	return 1;
+      }
 
   pthread_barrier_t *b;
   b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
diff --git a/sysdeps/pthread/tst-key1.c b/sysdeps/pthread/tst-key1.c
index 933edafef8..f9560a9bf5 100644
--- a/sysdeps/pthread/tst-key1.c
+++ b/sysdeps/pthread/tst-key1.c
@@ -51,7 +51,7 @@ do_test (void)
 
 	if (pthread_setspecific (keys[i], (const void *) (i + 100l)) != 0)
 	  {
-	    write (2, "setspecific failed\n", 19);
+	    if (write (2, "setspecific failed\n", 19)) {};
 	    _exit (1);
 	  }
       }
@@ -60,13 +60,13 @@ do_test (void)
     {
       if (pthread_getspecific (keys[i]) != (void *) (i + 100l))
 	{
-	  write (2, "getspecific failed\n", 19);
+	  if (write (2, "getspecific failed\n", 19)) {};
 	  _exit (1);
 	}
 
       if (pthread_key_delete (keys[i]) != 0)
 	{
-	  write (2, "key_delete failed\n", 18);
+	  if (write (2, "key_delete failed\n", 18)) {};
 	  _exit (1);
 	}
     }
@@ -74,13 +74,13 @@ do_test (void)
   /* Now it must be once again possible to allocate keys.  */
   if (pthread_key_create (&keys[0], NULL) != 0)
     {
-      write (2, "2nd key_create failed\n", 22);
+      if (write (2, "2nd key_create failed\n", 22)) {};
       _exit (1);
     }
 
   if (pthread_key_delete (keys[0]) != 0)
     {
-      write (2, "2nd key_delete failed\n", 22);
+      if (write (2, "2nd key_delete failed\n", 22)) {};
       _exit (1);
     }
 
diff --git a/sysdeps/pthread/tst-signal1.c b/sysdeps/pthread/tst-signal1.c
index d1073e8459..7459e30031 100644
--- a/sysdeps/pthread/tst-signal1.c
+++ b/sysdeps/pthread/tst-signal1.c
@@ -105,7 +105,11 @@ do_test (void)
 
   int i;
   for (i = 0; i < 20; ++i)
-    write (fd, "foobar xyzzy", 12);
+    if (write (fd, "foobar xyzzy", 12) < 0)
+    {
+      puts ("write failed");
+      exit (1);
+    }
 
   b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
 	    MAP_SHARED, fd, 0);
diff --git a/sysdeps/pthread/tst-signal2.c b/sysdeps/pthread/tst-signal2.c
index dfe7d9f64a..34c5fda7e5 100644
--- a/sysdeps/pthread/tst-signal2.c
+++ b/sysdeps/pthread/tst-signal2.c
@@ -111,7 +111,11 @@ do_test (void)
 
   int i;
   for (i = 0; i < 20; ++i)
-    write (fd, "foobar xyzzy", 12);
+    if (write (fd, "foobar xyzzy", 12) < 0)
+    {
+      puts ("write failed");
+      exit (1);
+    }
 
   b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
 	    MAP_SHARED, fd, 0);
diff --git a/sysdeps/pthread/tst-timer.c b/sysdeps/pthread/tst-timer.c
index 47472ab8e1..0363a13ab2 100644
--- a/sysdeps/pthread/tst-timer.c
+++ b/sysdeps/pthread/tst-timer.c
@@ -44,7 +44,7 @@ signal_func (int sig)
 {
   static const char text[] = "signal_func\n";
   signal (sig, signal_func);
-  write (STDOUT_FILENO, text, sizeof text - 1);
+  if (write (STDOUT_FILENO, text, sizeof text - 1)) {};
 }
 
 static void
diff --git a/time/tst-cpuclock1.c b/time/tst-cpuclock1.c
index 6f2e70a58a..1fb22329e4 100644
--- a/time/tst-cpuclock1.c
+++ b/time/tst-cpuclock1.c
@@ -41,7 +41,7 @@ chew_cpu (void)
       for (int i = 0; i < 100; ++i)
 	for (size_t j = 0; j < sizeof buf; ++j)
 	  buf[j] = 0xbb;
-      write (nullfd, (char *) buf, sizeof buf);
+      if (write (nullfd, (char *) buf, sizeof buf)) {}
       close (nullfd);
       if (getppid () == 1)
 	_exit (2);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH 2/8] catgets/gencat.c: fix warn unused result
  2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
@ 2023-04-18 12:11 ` Frédéric Bérat
  2023-04-18 12:36   ` Siddhesh Poyarekar
                     ` (2 more replies)
  2023-04-18 12:11 ` [PATCH 3/8] inet/rcmd.c: " Frédéric Bérat
                   ` (8 subsequent siblings)
  10 siblings, 3 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-18 12:11 UTC (permalink / raw)
  To: libc-alpha; +Cc: sipoyare

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 catgets/gencat.c | 34 +++++++++++++++++++++++-----------
 1 file changed, 23 insertions(+), 11 deletions(-)

diff --git a/catgets/gencat.c b/catgets/gencat.c
index 61ac797349..ecfdaa6e6d 100644
--- a/catgets/gencat.c
+++ b/catgets/gencat.c
@@ -852,6 +852,7 @@ write_out (struct catalog *catalog, const char *output_name,
   const char *strings;
   size_t strings_size;
   uint32_t *array1, *array2;
+  uint32_t array_size;
   size_t cnt;
   int fd;
 
@@ -927,12 +928,11 @@ write_out (struct catalog *catalog, const char *output_name,
   obj.plane_size = best_size;
   obj.plane_depth = best_depth;
 
+  array_size = best_size * best_depth * sizeof (uint32_t) * 3;
   /* Allocate room for all needed arrays.  */
-  array1 =
-    (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
-  memset (array1, '\0', best_size * best_depth * sizeof (uint32_t) * 3);
-  array2
-    = (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
+  array1 = (uint32_t *) alloca (array_size);
+  memset (array1, '\0', array_size);
+  array2 = (uint32_t *) alloca (array_size);
   obstack_init (&string_pool);
 
   set_run = catalog->all_sets;
@@ -985,22 +985,34 @@ write_out (struct catalog *catalog, const char *output_name,
     }
 
   /* Write out header.  */
-  write (fd, &obj, sizeof (obj));
+  if (write (fd, &obj, sizeof (obj)) < sizeof (obj))
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
 
   /* We always write out the little endian version of the index
      arrays.  */
 #if __BYTE_ORDER == __LITTLE_ENDIAN
-  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
-  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
+  if (write (fd, array1, array_size) < array_size)
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
+  if (write (fd, array2, array_size) < array_size)
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
 #elif __BYTE_ORDER == __BIG_ENDIAN
-  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
-  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
+  if (write (fd, array2, array_size) < array_size)
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
+  if (write (fd, array1, array_size) < array_size)
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
 #else
 # error Cannot handle __BYTE_ORDER byte order
 #endif
 
   /* Finally write the strings.  */
-  write (fd, strings, strings_size);
+  if (write (fd, strings, strings_size) < strings_size)
+	  error (EXIT_FAILURE, errno,
+		 gettext ("cannot write enough bytes to `%s'"), output_name);
 
   if (fd != STDOUT_FILENO)
     close (fd);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH 3/8] inet/rcmd.c: fix warn unused result
  2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
  2023-04-18 12:11 ` [PATCH 2/8] catgets/gencat.c: fix warn unused result Frédéric Bérat
@ 2023-04-18 12:11 ` Frédéric Bérat
  2023-04-18 12:37   ` Siddhesh Poyarekar
  2023-04-18 13:40   ` Andreas Schwab
  2023-04-18 12:11 ` [PATCH 4/8] locale/programs/locarchive.c: " Frédéric Bérat
                   ` (7 subsequent siblings)
  10 siblings, 2 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-18 12:11 UTC (permalink / raw)
  To: libc-alpha; +Cc: sipoyare

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 inet/rcmd.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/inet/rcmd.c b/inet/rcmd.c
index ad8a894907..c1cd9daeb5 100644
--- a/inet/rcmd.c
+++ b/inet/rcmd.c
@@ -561,7 +561,9 @@ ruserok2_sa (struct sockaddr *ra, size_t ralen, int superuser,
 	  reading an NFS mounted file system, can't read files that
 	  are protected read/write owner only.  */
        uid = __geteuid ();
-       seteuid (pwd->pw_uid);
+       if (seteuid (pwd->pw_uid) < 0)
+	       return -1;
+
        hostf = iruserfopen (pbuf, pwd->pw_uid);
 
        if (hostf != NULL)
@@ -570,7 +572,8 @@ ruserok2_sa (struct sockaddr *ra, size_t ralen, int superuser,
 	   fclose (hostf);
 	 }
 
-       seteuid (uid);
+       if (seteuid (uid) < 0)
+	       return -1;
        return isbad;
     }
   return -1;
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH 4/8] locale/programs/locarchive.c: fix warn unused result
  2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
                   ` (2 preceding siblings ...)
  2023-04-18 12:11 ` [PATCH 3/8] inet/rcmd.c: " Frédéric Bérat
@ 2023-04-18 12:11 ` Frédéric Bérat
  2023-04-18 12:43   ` Siddhesh Poyarekar
                     ` (2 more replies)
  2023-04-18 12:11 ` [PATCH 5/8] " Frédéric Bérat
                   ` (6 subsequent siblings)
  10 siblings, 3 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-18 12:11 UTC (permalink / raw)
  To: libc-alpha; +Cc: sipoyare

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 locale/programs/locarchive.c | 18 ++++++++++--------
 1 file changed, 10 insertions(+), 8 deletions(-)

diff --git a/locale/programs/locarchive.c b/locale/programs/locarchive.c
index 87d2d9b1b2..4e13bb9ce9 100644
--- a/locale/programs/locarchive.c
+++ b/locale/programs/locarchive.c
@@ -1154,10 +1154,11 @@ add_locale_to_archive (struct locarhandle *ah, const char *name,
   if (mask & XPG_NORM_CODESET)
     /* This name contains a codeset in unnormalized form.
        We will store it in the archive with a normalized name.  */
-    asprintf (&normalized_name, "%s%s%s.%s%s%s",
-	      language, territory == NULL ? "" : "_", territory ?: "",
-	      (mask & XPG_NORM_CODESET) ? normalized_codeset : codeset,
-	      modifier == NULL ? "" : "@", modifier ?: "");
+    if (asprintf (&normalized_name, "%s%s%s.%s%s%s",
+		  language, territory == NULL ? "" : "_", territory ?: "",
+		  (mask & XPG_NORM_CODESET) ? normalized_codeset : codeset,
+		  modifier == NULL ? "" : "@", modifier ?: "") < 0)
+	    return -1;
 
   /* This call does the main work.  */
   locrec_offset = add_locale (ah, normalized_name ?: name, data, replace);
@@ -1188,10 +1189,11 @@ add_locale_to_archive (struct locarhandle *ah, const char *name,
       normalized_codeset = _nl_normalize_codeset (codeset, strlen (codeset));
       mask |= XPG_NORM_CODESET;
 
-      asprintf (&normalized_codeset_name, "%s%s%s.%s%s%s",
-		language, territory == NULL ? "" : "_", territory ?: "",
-		normalized_codeset,
-		modifier == NULL ? "" : "@", modifier ?: "");
+      if (asprintf (&normalized_codeset_name, "%s%s%s.%s%s%s",
+		    language, territory == NULL ? "" : "_", territory ?: "",
+		    normalized_codeset,
+		    modifier == NULL ? "" : "@", modifier ?: "") < 0)
+	      return -1;
 
       add_alias (ah, normalized_codeset_name, replace,
 		 normalized_name ?: name, &locrec_offset);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH 5/8] malloc/{memusage.c,memusagestat.c}: fix warn unused result
  2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
                   ` (3 preceding siblings ...)
  2023-04-18 12:11 ` [PATCH 4/8] locale/programs/locarchive.c: " Frédéric Bérat
@ 2023-04-18 12:11 ` Frédéric Bérat
  2023-04-18 12:47   ` [PATCH 5/8] malloc/{memusage.c, memusagestat.c}: " Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 03/15] malloc/{memusage.c,memusagestat.c}: " Frédéric Bérat
  2023-04-18 12:11 ` [PATCH 6/8] nptl_db/thread_dbP.h: fix warn unused result Frédéric Bérat
                   ` (5 subsequent siblings)
  10 siblings, 2 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-18 12:11 UTC (permalink / raw)
  To: libc-alpha; +Cc: sipoyare

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 malloc/memusage.c     | 38 ++++++++++++++++++++++++++------------
 malloc/memusagestat.c | 13 +++++++++----
 2 files changed, 35 insertions(+), 16 deletions(-)

diff --git a/malloc/memusage.c b/malloc/memusage.c
index 2a3a508557..6251e039b0 100644
--- a/malloc/memusage.c
+++ b/malloc/memusage.c
@@ -18,6 +18,8 @@
 
 #include <assert.h>
 #include <dlfcn.h>
+#include <errno.h>
+#include <error.h>
 #include <fcntl.h>
 #include <stdatomic.h>
 #include <stdbool.h>
@@ -210,10 +212,12 @@ update_data (struct header *result, size_t len, size_t old_len)
       gettime (&buffer[idx]);
 
       /* Write out buffer if it is full.  */
-      if (idx + 1 == buffer_size)
-        write (fd, buffer, buffer_size * sizeof (struct entry));
-      else if (idx + 1 == 2 * buffer_size)
-        write (fd, &buffer[buffer_size], buffer_size * sizeof (struct entry));
+      if (idx + 1 == buffer_size || idx + 1 == 2 * buffer_size)
+        {
+          uint32_t write_size = buffer_size * sizeof (buffer[0]);
+          if (write (fd, &buffer[idx + 1 - buffer_size], write_size) < write_size)
+              error(EXIT_FAILURE, errno, "cannot write buffer");
+        }
     }
 }
 
@@ -299,8 +303,10 @@ me (void)
               first.stack = 0;
               gettime (&first);
               /* Write it two times since we need the starting and end time. */
-              write (fd, &first, sizeof (first));
-              write (fd, &first, sizeof (first));
+              if (write (fd, &first, sizeof (first)) < sizeof (first))
+                error(EXIT_FAILURE, errno, "cannot write entry");
+              if (write (fd, &first, sizeof (first)) < sizeof (first))
+                error(EXIT_FAILURE, errno, "cannot write entry");
 
               /* Determine the buffer size.  We use the default if the
                  environment variable is not present.  */
@@ -850,24 +856,32 @@ dest (void)
   if (fd != -1)
     {
       /* Write the partially filled buffer.  */
+      struct entry *start = buffer;
+      uint32_t write_cnt = buffer_cnt;
+
       if (buffer_cnt > buffer_size)
-        write (fd, buffer + buffer_size,
-               (buffer_cnt - buffer_size) * sizeof (struct entry));
-      else
-        write (fd, buffer, buffer_cnt * sizeof (struct entry));
+        {
+          start = buffer + buffer_size;
+          write_cnt = buffer_cnt - buffer_size;
+        }
+
+      if (write(fd, start, write_cnt * sizeof (buffer[0])) < sizeof (buffer[0]))
+        error(EXIT_FAILURE, errno, "cannot write buffer");
 
       /* Go back to the beginning of the file.  We allocated two records
          here when we opened the file.  */
       lseek (fd, 0, SEEK_SET);
       /* Write out a record containing the total size.  */
       first.stack = peak_total;
-      write (fd, &first, sizeof (struct entry));
+      if (write (fd, &first, sizeof (first)) < sizeof (first))
+        error(EXIT_FAILURE, errno, "cannot write first");
       /* Write out another record containing the maximum for heap and
          stack.  */
       first.heap = peak_heap;
       first.stack = peak_stack;
       gettime (&first);
-      write (fd, &first, sizeof (struct entry));
+      if (write (fd, &first, sizeof (first)) < sizeof (first))
+        error(EXIT_FAILURE, errno, "cannot write first");
 
       /* Close the file.  */
       close (fd);
diff --git a/malloc/memusagestat.c b/malloc/memusagestat.c
index 67c5131f79..3853f00b9b 100644
--- a/malloc/memusagestat.c
+++ b/malloc/memusagestat.c
@@ -188,7 +188,8 @@ main (int argc, char *argv[])
   total = st.st_size / sizeof (struct entry) - 2;
 
   /* Read the administrative information.  */
-  read (fd, headent, sizeof (headent));
+  if (read (fd, headent, sizeof (headent)) < sizeof (headent))
+    error(EXIT_FAILURE, errno, "cannot read entry header");
   maxsize_heap = headent[1].heap;
   maxsize_stack = headent[1].stack;
   maxsize_total = headent[0].stack;
@@ -220,7 +221,9 @@ main (int argc, char *argv[])
 
       /* Write the computed values in the file.  */
       lseek (fd, 0, SEEK_SET);
-      write (fd, headent, 2 * sizeof (struct entry));
+      if (write (fd, headent, sizeof (headent)) < sizeof (headent))
+        error(EXIT_FAILURE, errno, "cannot write entries");
+
     }
 
   if (also_total)
@@ -372,7 +375,8 @@ main (int argc, char *argv[])
           size_t new[2];
           uint64_t now;
 
-          read (fd, &entry, sizeof (entry));
+          if (read (fd, &entry, sizeof (entry)) < sizeof (entry))
+            error(EXIT_FAILURE, errno, "cannot read entry");
 
           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
 
@@ -455,7 +459,8 @@ main (int argc, char *argv[])
           size_t xpos;
           uint64_t now;
 
-          read (fd, &entry, sizeof (entry));
+          if (read (fd, &entry, sizeof (entry)) < sizeof (entry))
+            error(EXIT_FAILURE, errno, "cannot read entry");
 
           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
           xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH 6/8] nptl_db/thread_dbP.h: fix warn unused result
  2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
                   ` (4 preceding siblings ...)
  2023-04-18 12:11 ` [PATCH 5/8] " Frédéric Bérat
@ 2023-04-18 12:11 ` Frédéric Bérat
  2023-04-18 12:49   ` Siddhesh Poyarekar
                     ` (2 more replies)
  2023-04-18 12:11 ` [PATCH 7/8] sunrpc/netname.c: " Frédéric Bérat
                   ` (4 subsequent siblings)
  10 siblings, 3 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-18 12:11 UTC (permalink / raw)
  To: libc-alpha; +Cc: sipoyare

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 nptl_db/thread_dbP.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/nptl_db/thread_dbP.h b/nptl_db/thread_dbP.h
index 8845722109..ad0c197f6f 100644
--- a/nptl_db/thread_dbP.h
+++ b/nptl_db/thread_dbP.h
@@ -61,7 +61,7 @@ enum
 
 /* Comment out the following for less verbose output.  */
 #ifndef NDEBUG
-# define LOG(c) if (__td_debug) write (2, c "\n", strlen (c "\n"))
+# define LOG(c) if (__td_debug) assert(write (2, c "\n", strlen (c "\n")) == strlen (c "\n"))
 extern int __td_debug attribute_hidden;
 #else
 # define LOG(c)
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH 7/8] sunrpc/netname.c: fix warn unused result
  2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
                   ` (5 preceding siblings ...)
  2023-04-18 12:11 ` [PATCH 6/8] nptl_db/thread_dbP.h: fix warn unused result Frédéric Bérat
@ 2023-04-18 12:11 ` Frédéric Bérat
  2023-04-18 12:51   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 05/15] " Frédéric Bérat
  2023-04-18 12:11 ` [PATCH 8/8] sysdeps/pthread/eintr.c: " Frédéric Bérat
                   ` (3 subsequent siblings)
  10 siblings, 2 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-18 12:11 UTC (permalink / raw)
  To: libc-alpha; +Cc: sipoyare

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 sunrpc/netname.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/sunrpc/netname.c b/sunrpc/netname.c
index dd2cb1b441..949954959a 100644
--- a/sunrpc/netname.c
+++ b/sunrpc/netname.c
@@ -96,7 +96,8 @@ host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
       else
 	{
 	  domainname[0] = 0;
-	  getdomainname (domainname, MAXHOSTNAMELEN);
+	  if(getdomainname (domainname, MAXHOSTNAMELEN))
+		  return 0;
 	}
     }
   else
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH 8/8] sysdeps/pthread/eintr.c: fix warn unused result
  2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
                   ` (6 preceding siblings ...)
  2023-04-18 12:11 ` [PATCH 7/8] sunrpc/netname.c: " Frédéric Bérat
@ 2023-04-18 12:11 ` Frédéric Bérat
  2023-04-18 12:54   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 06/15] " Frédéric Bérat
  2023-04-28 12:21 ` [PATCH v4 00/15] Fix " Frédéric Bérat
                   ` (2 subsequent siblings)
  10 siblings, 2 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-18 12:11 UTC (permalink / raw)
  To: libc-alpha; +Cc: sipoyare

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 sysdeps/pthread/eintr.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/sysdeps/pthread/eintr.c b/sysdeps/pthread/eintr.c
index 000649d24e..8441df0c77 100644
--- a/sysdeps/pthread/eintr.c
+++ b/sysdeps/pthread/eintr.c
@@ -31,10 +31,10 @@ eintr_handler (int sig)
 {
   if (sig != the_sig)
     {
-      write (STDOUT_FILENO, "eintr_handler: signal number wrong\n", 35);
+      if (write (STDOUT_FILENO, "eintr_handler: signal number wrong\n", 35)) {};
       _exit (1);
     }
-  write (STDOUT_FILENO, ".", 1);
+  if (write (STDOUT_FILENO, ".", 1)) {};
 }
 
 
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 1/8] tests: fix warn unused result
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
@ 2023-04-18 12:23   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 07/15] tests: fix warn unused result on asprintf calls Frédéric Bérat
                     ` (11 subsequent siblings)
  12 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-04-18 12:23 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha

On 2023-04-18 08:11, Frédéric Bérat via Libc-alpha wrote:
> When enabling _FORTIFY_SOURCE, some functions now lead to warnings when
> their result is not checked.

I haven't done a full review of this patch yet, but the first straight 
issue I can see is that you could use the x* functions in support/ in 
the tests to avoid repeating the error checking blocks in tests.  For 
example:

> ---
>   argp/argp-test.c               | 14 +++++++++++---
>   assert/test-assert-perr.c      | 13 +++++++------
>   assert/test-assert.c           | 13 +++++++------
>   crypt/cert.c                   |  6 +++++-
>   dirent/tst-fdopendir.c         | 13 +++++++++++--
>   elf/tst-stackguard1.c          |  2 +-
>   io/tst-copy_file_range.c       |  2 +-
>   io/tst-faccessat.c             |  6 +++++-
>   io/tst-fchmodat.c              |  6 +++++-
>   io/tst-fchownat.c              |  6 +++++-
>   io/tst-fstatat.c               |  6 +++++-
>   io/tst-futimesat.c             |  6 +++++-
>   io/tst-linkat.c                |  6 +++++-
>   io/tst-openat.c                |  6 +++++-
>   io/tst-renameat.c              |  6 +++++-
>   io/tst-symlinkat.c             |  6 +++++-
>   io/tst-unlinkat.c              |  6 +++++-
>   libio/bug-fseek.c              | 15 ++++++++++++---
>   libio/bug-mmap-fflush.c        | 12 ++++++++++--
>   libio/bug-ungetc.c             |  7 ++++++-
>   libio/bug-ungetc3.c            |  7 ++++++-
>   libio/bug-ungetc4.c            |  7 ++++++-
>   libio/bug-wfflush.c            |  6 +++++-
>   libio/bug-wsetpos.c            |  7 ++++++-
>   misc/tst-efgcvt-template.c     |  4 ++--
>   misc/tst-error1.c              |  2 +-
>   nptl/tst-cancel7.c             |  2 +-
>   nptl/tst-cleanup4.c            |  6 +++++-
>   nptl/tst-stackguard1.c         |  8 ++++++--
>   nptl/tst-tls3mod.c             |  4 ++--
>   nss/tst-nss-db-endpwent.c      |  6 +++++-
>   nss/tst-reload2.c              |  6 +++++-
>   posix/tst-chmod.c              |  9 +++++++--
>   posix/tst-execl2.c             |  4 ++--
>   posix/tst-execle2.c            |  4 ++--
>   posix/tst-execlp2.c            |  7 +++----
>   posix/tst-execv2.c             |  3 +--
>   posix/tst-execve2.c            |  3 +--
>   posix/tst-execvp2.c            |  6 ++----
>   posix/tst-getopt-cancel.c      |  2 +-
>   posix/tst-nice.c               |  3 +--
>   posix/wordexp-test.c           | 12 ++++++++++--
>   rt/tst-cpuclock2.c             |  2 +-
>   rt/tst-cputimer1.c             |  2 +-
>   rt/tst-cputimer2.c             |  2 +-
>   rt/tst-cputimer3.c             |  2 +-
>   stdio-common/bug12.c           | 15 ++++++++++-----
>   stdio-common/bug19.c           |  9 +++++++--
>   stdio-common/bug3.c            |  6 +++++-
>   stdio-common/bug4.c            |  6 +++++-
>   stdio-common/bug5.c            |  6 +++++-
>   stdio-common/bug6.c            |  8 ++++----
>   stdio-common/test-fwrite.c     |  8 ++++++--
>   stdio-common/test_rdwr.c       |  3 ++-
>   stdio-common/tst-cookie.c      |  4 +++-
>   stdio-common/tst-fmemopen3.c   |  6 +++++-
>   stdio-common/tst-fseek.c       |  3 +--
>   stdio-common/tst-perror.c      |  6 +++++-
>   stdio-common/tstscanf.c        | 14 ++++++++++++--
>   stdlib/test-canon.c            | 25 +++++++++++++++++++++----
>   support/test-container.c       | 18 +++++++++++-------
>   sysdeps/pthread/tst-cancel11.c |  6 +++++-
>   sysdeps/pthread/tst-cancel16.c |  6 +++++-
>   sysdeps/pthread/tst-cancel20.c |  2 --
>   sysdeps/pthread/tst-cancel21.c |  2 --
>   sysdeps/pthread/tst-cancel4.c  |  6 ++++--
>   sysdeps/pthread/tst-cancel6.c  |  3 ++-
>   sysdeps/pthread/tst-cond18.c   |  2 +-
>   sysdeps/pthread/tst-fini1mod.c |  6 +++++-
>   sysdeps/pthread/tst-flock1.c   |  6 +++++-
>   sysdeps/pthread/tst-flock2.c   |  6 +++++-
>   sysdeps/pthread/tst-key1.c     | 10 +++++-----
>   sysdeps/pthread/tst-signal1.c  |  6 +++++-
>   sysdeps/pthread/tst-signal2.c  |  6 +++++-
>   sysdeps/pthread/tst-timer.c    |  2 +-
>   time/tst-cpuclock1.c           |  2 +-
>   76 files changed, 356 insertions(+), 135 deletions(-)
> 
> diff --git a/argp/argp-test.c b/argp/argp-test.c
> index c7e20f6235..fa7f9694d1 100644
> --- a/argp/argp-test.c
> +++ b/argp/argp-test.c
> @@ -178,12 +178,20 @@ 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));
> +      if (asprintf (&new_text, text, ctime (&now)) < 0)

... this could be xasprintf,

> +	{
> +	  printf("Failed to execute asprintf: %m\n");
> +	  exit(1);
> +	}
>       }
>     else if (key == 'f')
>       /* Show the default for the --foonly option.  */
> -    asprintf (&new_text, "%s (ZOT defaults to %x)",
> -	      text, params->foonly_default);
> +    if (asprintf (&new_text, "%s (ZOT defaults to %x)",
> +	      text, params->foonly_default) < 0)
> +	{
> +	  printf("Failed to execute asprintf: %m\n");
> +	  exit(1);
> +	}
>     else
>       new_text = (char *)text;
>   
> diff --git a/assert/test-assert-perr.c b/assert/test-assert-perr.c
> index 8496db6ffd..ecfd4d14e5 100644
> --- a/assert/test-assert-perr.c
> +++ b/assert/test-assert-perr.c
> @@ -46,6 +46,7 @@ int
>   main(void)
>   {
>     volatile int failed = 1;  /* safety in presence of longjmp() */
> +  char *ret;
>   
>     fclose (stderr);
>     stderr = tmpfile ();
> @@ -70,16 +71,16 @@ main(void)
>       failed = 1; /* should not happen */
>   
>     rewind (stderr);
> -  fgets (buf, 160, stderr);
> -  if (!strstr(buf, strerror (1)))
> +  ret = fgets (buf, 160, stderr);
> +  if ((!ret && ferror(stderr)) || !strstr(buf, strerror (1)))

This could be a new xfgets, and so on.

I'd suggest making these test fixes into their own series, grouped by 
the x* functions, i.e. one patch for xasprintf, one for xfgets, etc.

Thanks,
Sid

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 2/8] catgets/gencat.c: fix warn unused result
  2023-04-18 12:11 ` [PATCH 2/8] catgets/gencat.c: fix warn unused result Frédéric Bérat
@ 2023-04-18 12:36   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 01/15] " Frédéric Bérat
  2023-06-01 14:27   ` [PATCH v5 01/12] " Frédéric Bérat
  2 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-04-18 12:36 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha

On 2023-04-18 08:11, Frédéric Bérat via Libc-alpha wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   catgets/gencat.c | 34 +++++++++++++++++++++++-----------
>   1 file changed, 23 insertions(+), 11 deletions(-)
> 
> diff --git a/catgets/gencat.c b/catgets/gencat.c
> index 61ac797349..ecfdaa6e6d 100644
> --- a/catgets/gencat.c
> +++ b/catgets/gencat.c
> @@ -852,6 +852,7 @@ write_out (struct catalog *catalog, const char *output_name,
>     const char *strings;
>     size_t strings_size;
>     uint32_t *array1, *array2;
> +  uint32_t array_size;
>     size_t cnt;
>     int fd;
>   
> @@ -927,12 +928,11 @@ write_out (struct catalog *catalog, const char *output_name,
>     obj.plane_size = best_size;
>     obj.plane_depth = best_depth;
>   
> +  array_size = best_size * best_depth * sizeof (uint32_t) * 3;

We use c99 by default, so use declare and define here, no need for 
declarations at the top.

>     /* Allocate room for all needed arrays.  */
> -  array1 =
> -    (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
> -  memset (array1, '\0', best_size * best_depth * sizeof (uint32_t) * 3);
> -  array2
> -    = (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
> +  array1 = (uint32_t *) alloca (array_size);
> +  memset (array1, '\0', array_size);
> +  array2 = (uint32_t *) alloca (array_size);

That's a good cleanup.

>     obstack_init (&string_pool);
>   
>     set_run = catalog->all_sets;
> @@ -985,22 +985,34 @@ write_out (struct catalog *catalog, const char *output_name,
>       }
>   
>     /* Write out header.  */
> -  write (fd, &obj, sizeof (obj));
> +  if (write (fd, &obj, sizeof (obj)) < sizeof (obj))
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);

I wonder if this should use xwrite-like functionality, i.e. write 
iteratively until there's an error.

>   
>     /* We always write out the little endian version of the index
>        arrays.  */
>   #if __BYTE_ORDER == __LITTLE_ENDIAN
> -  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
> -  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
> +  if (write (fd, array1, array_size) < array_size)
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);
> +  if (write (fd, array2, array_size) < array_size)
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);
>   #elif __BYTE_ORDER == __BIG_ENDIAN
> -  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
> -  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
> +  if (write (fd, array2, array_size) < array_size)
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);
> +  if (write (fd, array1, array_size) < array_size)
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);
>   #else
>   # error Cannot handle __BYTE_ORDER byte order
>   #endif
>   
>     /* Finally write the strings.  */
> -  write (fd, strings, strings_size);
> +  if (write (fd, strings, strings_size) < strings_size)
> +	  error (EXIT_FAILURE, errno,
> +		 gettext ("cannot write enough bytes to `%s'"), output_name);
>   
>     if (fd != STDOUT_FILENO)
>       close (fd);

Likewise for all of these calls.  It probably makes sense to implement a 
static int write_all() in this file that is similar to support/xwrite.c 
and then use it in place of all of these write calls.

If this pattern is more common across glibc, perhaps putting it into 
include/unistd.h may make sense too.

Thanks,
Sid

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 3/8] inet/rcmd.c: fix warn unused result
  2023-04-18 12:11 ` [PATCH 3/8] inet/rcmd.c: " Frédéric Bérat
@ 2023-04-18 12:37   ` Siddhesh Poyarekar
  2023-04-18 13:40   ` Andreas Schwab
  1 sibling, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-04-18 12:37 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha; +Cc: sipoyare

On 2023-04-18 08:11, Frédéric Bérat via Libc-alpha wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   inet/rcmd.c | 7 +++++--
>   1 file changed, 5 insertions(+), 2 deletions(-)

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
> diff --git a/inet/rcmd.c b/inet/rcmd.c
> index ad8a894907..c1cd9daeb5 100644
> --- a/inet/rcmd.c
> +++ b/inet/rcmd.c
> @@ -561,7 +561,9 @@ ruserok2_sa (struct sockaddr *ra, size_t ralen, int superuser,
>   	  reading an NFS mounted file system, can't read files that
>   	  are protected read/write owner only.  */
>          uid = __geteuid ();
> -       seteuid (pwd->pw_uid);
> +       if (seteuid (pwd->pw_uid) < 0)
> +	       return -1;
> +
>          hostf = iruserfopen (pbuf, pwd->pw_uid);
>   
>          if (hostf != NULL)
> @@ -570,7 +572,8 @@ ruserok2_sa (struct sockaddr *ra, size_t ralen, int superuser,
>   	   fclose (hostf);
>   	 }
>   
> -       seteuid (uid);
> +       if (seteuid (uid) < 0)
> +	       return -1;
>          return isbad;
>       }
>     return -1;

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 4/8] locale/programs/locarchive.c: fix warn unused result
  2023-04-18 12:11 ` [PATCH 4/8] locale/programs/locarchive.c: " Frédéric Bérat
@ 2023-04-18 12:43   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 02/15] " Frédéric Bérat
  2023-06-01 14:27   ` [PATCH v5 02/12] malloc/{memusage.c,memusagestat.c}: " Frédéric Bérat
  2 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-04-18 12:43 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha; +Cc: sipoyare

On 2023-04-18 08:11, Frédéric Bérat via Libc-alpha wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   locale/programs/locarchive.c | 18 ++++++++++--------
>   1 file changed, 10 insertions(+), 8 deletions(-)
> 
> diff --git a/locale/programs/locarchive.c b/locale/programs/locarchive.c
> index 87d2d9b1b2..4e13bb9ce9 100644
> --- a/locale/programs/locarchive.c
> +++ b/locale/programs/locarchive.c
> @@ -1154,10 +1154,11 @@ add_locale_to_archive (struct locarhandle *ah, const char *name,
>     if (mask & XPG_NORM_CODESET)
>       /* This name contains a codeset in unnormalized form.
>          We will store it in the archive with a normalized name.  */
> -    asprintf (&normalized_name, "%s%s%s.%s%s%s",
> -	      language, territory == NULL ? "" : "_", territory ?: "",
> -	      (mask & XPG_NORM_CODESET) ? normalized_codeset : codeset,
> -	      modifier == NULL ? "" : "@", modifier ?: "");
> +    if (asprintf (&normalized_name, "%s%s%s.%s%s%s",
> +		  language, territory == NULL ? "" : "_", territory ?: "",
> +		  (mask & XPG_NORM_CODESET) ? normalized_codeset : codeset,
> +		  modifier == NULL ? "" : "@", modifier ?: "") < 0)
> +	    return -1;

Wouldn't this leak normalized_codeset?

Also, this is within if (mask & XPG_NORM_CODESET), so the:

(mask & XPG_NORM_CODESET) ? normalized_codeset : codeset

looks unnecessary, doesn't it?  I know that's how the original code is, 
but maybe it's worth cleaning up while you're there.  In a separate 
patch though please.

>   
>     /* This call does the main work.  */
>     locrec_offset = add_locale (ah, normalized_name ?: name, data, replace);
> @@ -1188,10 +1189,11 @@ add_locale_to_archive (struct locarhandle *ah, const char *name,
>         normalized_codeset = _nl_normalize_codeset (codeset, strlen (codeset));
>         mask |= XPG_NORM_CODESET;
>   
> -      asprintf (&normalized_codeset_name, "%s%s%s.%s%s%s",
> -		language, territory == NULL ? "" : "_", territory ?: "",
> -		normalized_codeset,
> -		modifier == NULL ? "" : "@", modifier ?: "");
> +      if (asprintf (&normalized_codeset_name, "%s%s%s.%s%s%s",
> +		    language, territory == NULL ? "" : "_", territory ?: "",
> +		    normalized_codeset,
> +		    modifier == NULL ? "" : "@", modifier ?: "") < 0)
> +	      return -1;

Likewise.

>   
>         add_alias (ah, normalized_codeset_name, replace,
>   		 normalized_name ?: name, &locrec_offset);

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 5/8] malloc/{memusage.c, memusagestat.c}: fix warn unused result
  2023-04-18 12:11 ` [PATCH 5/8] " Frédéric Bérat
@ 2023-04-18 12:47   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 03/15] malloc/{memusage.c,memusagestat.c}: " Frédéric Bérat
  1 sibling, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-04-18 12:47 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha

On 2023-04-18 08:11, Frédéric Bérat via Libc-alpha wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   malloc/memusage.c     | 38 ++++++++++++++++++++++++++------------
>   malloc/memusagestat.c | 13 +++++++++----
>   2 files changed, 35 insertions(+), 16 deletions(-)
> 
> diff --git a/malloc/memusage.c b/malloc/memusage.c
> index 2a3a508557..6251e039b0 100644
> --- a/malloc/memusage.c
> +++ b/malloc/memusage.c
> @@ -18,6 +18,8 @@
>   
>   #include <assert.h>
>   #include <dlfcn.h>
> +#include <errno.h>
> +#include <error.h>
>   #include <fcntl.h>
>   #include <stdatomic.h>
>   #include <stdbool.h>
> @@ -210,10 +212,12 @@ update_data (struct header *result, size_t len, size_t old_len)
>         gettime (&buffer[idx]);
>   
>         /* Write out buffer if it is full.  */
> -      if (idx + 1 == buffer_size)
> -        write (fd, buffer, buffer_size * sizeof (struct entry));
> -      else if (idx + 1 == 2 * buffer_size)
> -        write (fd, &buffer[buffer_size], buffer_size * sizeof (struct entry));
> +      if (idx + 1 == buffer_size || idx + 1 == 2 * buffer_size)
> +        {
> +          uint32_t write_size = buffer_size * sizeof (buffer[0]);
> +          if (write (fd, &buffer[idx + 1 - buffer_size], write_size) < write_size)
> +              error(EXIT_FAILURE, errno, "cannot write buffer");

Same as patch 2/8, maybe it makes sense to make a higher level 
abstraction for read and write to ensure all contents are read/written.

Thanks,
Sid

> +        }
>       }
>   }
>   
> @@ -299,8 +303,10 @@ me (void)
>                 first.stack = 0;
>                 gettime (&first);
>                 /* Write it two times since we need the starting and end time. */
> -              write (fd, &first, sizeof (first));
> -              write (fd, &first, sizeof (first));
> +              if (write (fd, &first, sizeof (first)) < sizeof (first))
> +                error(EXIT_FAILURE, errno, "cannot write entry");
> +              if (write (fd, &first, sizeof (first)) < sizeof (first))
> +                error(EXIT_FAILURE, errno, "cannot write entry");
>   
>                 /* Determine the buffer size.  We use the default if the
>                    environment variable is not present.  */
> @@ -850,24 +856,32 @@ dest (void)
>     if (fd != -1)
>       {
>         /* Write the partially filled buffer.  */
> +      struct entry *start = buffer;
> +      uint32_t write_cnt = buffer_cnt;
> +
>         if (buffer_cnt > buffer_size)
> -        write (fd, buffer + buffer_size,
> -               (buffer_cnt - buffer_size) * sizeof (struct entry));
> -      else
> -        write (fd, buffer, buffer_cnt * sizeof (struct entry));
> +        {
> +          start = buffer + buffer_size;
> +          write_cnt = buffer_cnt - buffer_size;
> +        }
> +
> +      if (write(fd, start, write_cnt * sizeof (buffer[0])) < sizeof (buffer[0]))
> +        error(EXIT_FAILURE, errno, "cannot write buffer");
>   
>         /* Go back to the beginning of the file.  We allocated two records
>            here when we opened the file.  */
>         lseek (fd, 0, SEEK_SET);
>         /* Write out a record containing the total size.  */
>         first.stack = peak_total;
> -      write (fd, &first, sizeof (struct entry));
> +      if (write (fd, &first, sizeof (first)) < sizeof (first))
> +        error(EXIT_FAILURE, errno, "cannot write first");
>         /* Write out another record containing the maximum for heap and
>            stack.  */
>         first.heap = peak_heap;
>         first.stack = peak_stack;
>         gettime (&first);
> -      write (fd, &first, sizeof (struct entry));
> +      if (write (fd, &first, sizeof (first)) < sizeof (first))
> +        error(EXIT_FAILURE, errno, "cannot write first");
>   
>         /* Close the file.  */
>         close (fd);
> diff --git a/malloc/memusagestat.c b/malloc/memusagestat.c
> index 67c5131f79..3853f00b9b 100644
> --- a/malloc/memusagestat.c
> +++ b/malloc/memusagestat.c
> @@ -188,7 +188,8 @@ main (int argc, char *argv[])
>     total = st.st_size / sizeof (struct entry) - 2;
>   
>     /* Read the administrative information.  */
> -  read (fd, headent, sizeof (headent));
> +  if (read (fd, headent, sizeof (headent)) < sizeof (headent))
> +    error(EXIT_FAILURE, errno, "cannot read entry header");
>     maxsize_heap = headent[1].heap;
>     maxsize_stack = headent[1].stack;
>     maxsize_total = headent[0].stack;
> @@ -220,7 +221,9 @@ main (int argc, char *argv[])
>   
>         /* Write the computed values in the file.  */
>         lseek (fd, 0, SEEK_SET);
> -      write (fd, headent, 2 * sizeof (struct entry));
> +      if (write (fd, headent, sizeof (headent)) < sizeof (headent))
> +        error(EXIT_FAILURE, errno, "cannot write entries");
> +
>       }
>   
>     if (also_total)
> @@ -372,7 +375,8 @@ main (int argc, char *argv[])
>             size_t new[2];
>             uint64_t now;
>   
> -          read (fd, &entry, sizeof (entry));
> +          if (read (fd, &entry, sizeof (entry)) < sizeof (entry))
> +            error(EXIT_FAILURE, errno, "cannot read entry");
>   
>             now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
>   
> @@ -455,7 +459,8 @@ main (int argc, char *argv[])
>             size_t xpos;
>             uint64_t now;
>   
> -          read (fd, &entry, sizeof (entry));
> +          if (read (fd, &entry, sizeof (entry)) < sizeof (entry))
> +            error(EXIT_FAILURE, errno, "cannot read entry");
>   
>             now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
>             xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 6/8] nptl_db/thread_dbP.h: fix warn unused result
  2023-04-18 12:11 ` [PATCH 6/8] nptl_db/thread_dbP.h: fix warn unused result Frédéric Bérat
@ 2023-04-18 12:49   ` Siddhesh Poyarekar
  2023-04-18 12:51     ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 04/15] " Frédéric Bérat
  2023-06-01 14:27   ` [PATCH v5 03/12] " Frédéric Bérat
  2 siblings, 1 reply; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-04-18 12:49 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha; +Cc: sipoyare

On 2023-04-18 08:11, Frédéric Bérat via Libc-alpha wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   nptl_db/thread_dbP.h | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
> diff --git a/nptl_db/thread_dbP.h b/nptl_db/thread_dbP.h
> index 8845722109..ad0c197f6f 100644
> --- a/nptl_db/thread_dbP.h
> +++ b/nptl_db/thread_dbP.h
> @@ -61,7 +61,7 @@ enum
>   
>   /* Comment out the following for less verbose output.  */
>   #ifndef NDEBUG
> -# define LOG(c) if (__td_debug) write (2, c "\n", strlen (c "\n"))
> +# define LOG(c) if (__td_debug) assert(write (2, c "\n", strlen (c "\n")) == strlen (c "\n"))
>   extern int __td_debug attribute_hidden;
>   #else
>   # define LOG(c)

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 7/8] sunrpc/netname.c: fix warn unused result
  2023-04-18 12:11 ` [PATCH 7/8] sunrpc/netname.c: " Frédéric Bérat
@ 2023-04-18 12:51   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 05/15] " Frédéric Bérat
  1 sibling, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-04-18 12:51 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha; +Cc: sipoyare

On 2023-04-18 08:11, Frédéric Bérat via Libc-alpha wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   sunrpc/netname.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)
> 
> diff --git a/sunrpc/netname.c b/sunrpc/netname.c
> index dd2cb1b441..949954959a 100644
> --- a/sunrpc/netname.c
> +++ b/sunrpc/netname.c
> @@ -96,7 +96,8 @@ host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
>         else
>   	{
>   	  domainname[0] = 0;
> -	  getdomainname (domainname, MAXHOSTNAMELEN);
> +	  if(getdomainname (domainname, MAXHOSTNAMELEN))

Missing space after if.

> +		  return 0;
>   	}
>       }
>     else

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 6/8] nptl_db/thread_dbP.h: fix warn unused result
  2023-04-18 12:49   ` Siddhesh Poyarekar
@ 2023-04-18 12:51     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-04-18 12:51 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha; +Cc: sipoyare

On 2023-04-18 08:49, Siddhesh Poyarekar wrote:
> On 2023-04-18 08:11, Frédéric Bérat via Libc-alpha wrote:
>> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
>> glibc.
>> ---
>>   nptl_db/thread_dbP.h | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> LGTM.
> 
> Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>
> 
>>
>> diff --git a/nptl_db/thread_dbP.h b/nptl_db/thread_dbP.h
>> index 8845722109..ad0c197f6f 100644
>> --- a/nptl_db/thread_dbP.h
>> +++ b/nptl_db/thread_dbP.h
>> @@ -61,7 +61,7 @@ enum
>>   /* Comment out the following for less verbose output.  */
>>   #ifndef NDEBUG
>> -# define LOG(c) if (__td_debug) write (2, c "\n", strlen (c "\n"))
>> +# define LOG(c) if (__td_debug) assert(write (2, c "\n", strlen (c 
>> "\n")) == strlen (c "\n"))

Ahh wait, missing space after assert.

>>   extern int __td_debug attribute_hidden;
>>   #else
>>   # define LOG(c)
> 

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 8/8] sysdeps/pthread/eintr.c: fix warn unused result
  2023-04-18 12:11 ` [PATCH 8/8] sysdeps/pthread/eintr.c: " Frédéric Bérat
@ 2023-04-18 12:54   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 06/15] " Frédéric Bérat
  1 sibling, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-04-18 12:54 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha; +Cc: sipoyare

On 2023-04-18 08:11, Frédéric Bérat via Libc-alpha wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   sysdeps/pthread/eintr.c | 4 ++--
>   1 file changed, 2 insertions(+), 2 deletions(-)
> 
> diff --git a/sysdeps/pthread/eintr.c b/sysdeps/pthread/eintr.c
> index 000649d24e..8441df0c77 100644
> --- a/sysdeps/pthread/eintr.c
> +++ b/sysdeps/pthread/eintr.c
> @@ -31,10 +31,10 @@ eintr_handler (int sig)
>   {
>     if (sig != the_sig)
>       {
> -      write (STDOUT_FILENO, "eintr_handler: signal number wrong\n", 35);
> +      if (write (STDOUT_FILENO, "eintr_handler: signal number wrong\n", 35)) {};

Perhaps add a little comment here saying that it avoids the __wur? 
Also, make the check < 35 so that it's semantically correct.

>         _exit (1);
>       }
> -  write (STDOUT_FILENO, ".", 1);
> +  if (write (STDOUT_FILENO, ".", 1)) {};
>   }

Likewise.

>   
>   

Thanks,
Sid

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 3/8] inet/rcmd.c: fix warn unused result
  2023-04-18 12:11 ` [PATCH 3/8] inet/rcmd.c: " Frédéric Bérat
  2023-04-18 12:37   ` Siddhesh Poyarekar
@ 2023-04-18 13:40   ` Andreas Schwab
  2023-04-18 13:50     ` Siddhesh Poyarekar
  1 sibling, 1 reply; 109+ messages in thread
From: Andreas Schwab @ 2023-04-18 13:40 UTC (permalink / raw)
  To: Frédéric Bérat via Libc-alpha
  Cc: Frédéric Bérat, sipoyare

On Apr 18 2023, Frédéric Bérat via Libc-alpha wrote:

> diff --git a/inet/rcmd.c b/inet/rcmd.c
> index ad8a894907..c1cd9daeb5 100644
> --- a/inet/rcmd.c
> +++ b/inet/rcmd.c
> @@ -561,7 +561,9 @@ ruserok2_sa (struct sockaddr *ra, size_t ralen, int superuser,
>  	  reading an NFS mounted file system, can't read files that
>  	  are protected read/write owner only.  */
>         uid = __geteuid ();
> -       seteuid (pwd->pw_uid);
> +       if (seteuid (pwd->pw_uid) < 0)
> +	       return -1;

Wrong indentation.

> @@ -570,7 +572,8 @@ ruserok2_sa (struct sockaddr *ra, size_t ralen, int superuser,
>  	   fclose (hostf);
>  	 }
>  
> -       seteuid (uid);
> +       if (seteuid (uid) < 0)
> +	       return -1;

Wrong indentation.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510  2552 DF73 E780 A9DA AEC1
"And now for something completely different."

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 3/8] inet/rcmd.c: fix warn unused result
  2023-04-18 13:40   ` Andreas Schwab
@ 2023-04-18 13:50     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-04-18 13:50 UTC (permalink / raw)
  To: Andreas Schwab, Frédéric Bérat via Libc-alpha
  Cc: Frédéric Bérat, sipoyare

On 2023-04-18 09:40, Andreas Schwab wrote:
> On Apr 18 2023, Frédéric Bérat via Libc-alpha wrote:
> 
>> diff --git a/inet/rcmd.c b/inet/rcmd.c
>> index ad8a894907..c1cd9daeb5 100644
>> --- a/inet/rcmd.c
>> +++ b/inet/rcmd.c
>> @@ -561,7 +561,9 @@ ruserok2_sa (struct sockaddr *ra, size_t ralen, int superuser,
>>   	  reading an NFS mounted file system, can't read files that
>>   	  are protected read/write owner only.  */
>>          uid = __geteuid ();
>> -       seteuid (pwd->pw_uid);
>> +       if (seteuid (pwd->pw_uid) < 0)
>> +	       return -1;
> 
> Wrong indentation.
> 
>> @@ -570,7 +572,8 @@ ruserok2_sa (struct sockaddr *ra, size_t ralen, int superuser,
>>   	   fclose (hostf);
>>   	 }
>>   
>> -       seteuid (uid);
>> +       if (seteuid (uid) < 0)
>> +	       return -1;
> 
> Wrong indentation.
> 

Oops, I pushed an obvious change fixing that.

Thanks,
Sid

^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 00/15] Fix warn unused result
  2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
                   ` (7 preceding siblings ...)
  2023-04-18 12:11 ` [PATCH 8/8] sysdeps/pthread/eintr.c: " Frédéric Bérat
@ 2023-04-28 12:21 ` Frédéric Bérat
  2023-05-25  1:53   ` Siddhesh Poyarekar
  2023-06-02 15:28 ` [PATCH v6 0/7] " Frédéric Bérat
  2023-06-12 15:18 ` [PATCH v7 0/4] Fix warn unused result Frédéric Bérat
  10 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Hello,

Enabling _FORTIFY_SOURCE on Glibc itself implies that some function get the
__wur macro enabed.
This leads to build failures when -Werror is set.

I went for multiple patches for these fixes, a big one that cover the tests,
and then separate one for the other ones, assuming they may need to get a
closer look at.

While most of the changes look trivial, decision I made on how to fix them may
be open to debate, e.g. the additional checks may lead to failures while they
weren't earlier, some other have no impact while it may be useful to have one.

Feedback is therefore highly appreciated.

Fred.

Changes since v1:
 - "tests: fix warn unused result" has been split into several sub-patches. New
   x-variant of functions have been created when necessary.
 - "inet/rcmd.c" is removed has it has been merged upstream.
 - catgets/gencat.c:
   - introduced a static write_all routine, that mimics support/xwrite
   - fixed review findings (declaration of array_size where defined)
 - locale/programs/locarchive.c: untouched (I forgot it)
 - malloc/{memusage.c,memusagestat.c}: introduced write_all/read_all functions
 - nptl_db/thread_dbP.h: fixed missing white space.
 - sunrpc/netname.c: fixed missing whitespace, and "return 0;" indent.
 - sysdeps/pthread/eintr.c: Added comment on why the if statement is used.

Changes since v2:
 - Rebased on master
 - catgets/gencat.c: Don't use errno if write returns 0.
 - locale/programs/locarchive.c: Freeing normalized_codeset before returning.
 - malloc/{memusage.c,memusagestat.c}: Don't use errno if write returns 0.
 - tests: replace read by xread:
   - Add missing xunistd.h includes
   - Add libsupport to tst-fini1mod.so
 - tests: replace fread by xfread:
   - Fixed xfread prototype
   - Add missing xstdio.h include

Changes since v3:
 - Rebased on master

---
Frédéric Bérat (15):
  catgets/gencat.c: fix warn unused result
  locale/programs/locarchive.c: fix warn unused result
  malloc/{memusage.c,memusagestat.c}: fix warn unused result
  nptl_db/thread_dbP.h: fix warn unused result
  sunrpc/netname.c: fix warn unused result
  sysdeps/pthread/eintr.c: fix warn unused result
  tests: fix warn unused result on asprintf calls
  tests: replace write by xwrite
  tests: replace read by xread
  tests: replace system by xsystem
  tests: replace fread by xfread
  tests: replace ftruncate by xftruncate
  tests: replace fgets by xfgets
  tests: Replace various function calls with their x variant
  tests: fix warn unused results

 argp/argp-test.c               |  8 +++--
 assert/test-assert-perr.c      |  8 +++--
 assert/test-assert.c           |  8 +++--
 catgets/gencat.c               | 41 +++++++++++++++++++-------
 crypt/cert.c                   |  6 +++-
 dirent/tst-fdopendir.c         |  7 +++--
 elf/tst-stackguard1.c          |  2 +-
 io/tst-copy_file_range.c       |  2 +-
 io/tst-faccessat.c             |  3 +-
 io/tst-fchmodat.c              |  3 +-
 io/tst-fchownat.c              |  3 +-
 io/tst-fstatat.c               |  3 +-
 io/tst-futimesat.c             |  3 +-
 io/tst-linkat.c                |  3 +-
 io/tst-openat.c                |  3 +-
 io/tst-renameat.c              |  3 +-
 io/tst-symlinkat.c             |  3 +-
 io/tst-unlinkat.c              |  3 +-
 libio/bug-fseek.c              |  7 +++--
 libio/bug-mmap-fflush.c        |  7 +++--
 libio/bug-ungetc.c             |  4 ++-
 libio/bug-ungetc3.c            |  4 ++-
 libio/bug-ungetc4.c            |  4 ++-
 libio/bug-wfflush.c            |  4 ++-
 libio/bug-wsetpos.c            |  4 ++-
 locale/programs/locarchive.c   | 24 ++++++++++-----
 malloc/memusage.c              | 53 ++++++++++++++++++++++++++--------
 malloc/memusagestat.c          | 48 +++++++++++++++++++++++++++---
 misc/tst-efgcvt-template.c     |  4 +--
 misc/tst-error1.c              |  2 +-
 nptl/tst-cancel7.c             |  3 +-
 nptl/tst-cleanup4.c            |  4 ++-
 nptl/tst-stackguard1.c         |  6 ++--
 nptl/tst-tls3.c                |  2 ++
 nptl/tst-tls3mod.c             |  5 ++--
 nptl_db/thread_dbP.h           |  2 +-
 nss/tst-nss-db-endpwent.c      |  3 +-
 nss/tst-reload2.c              |  6 +++-
 posix/tst-chmod.c              |  9 ++++--
 posix/tst-execl2.c             |  8 ++---
 posix/tst-execle2.c            |  8 ++---
 posix/tst-execlp2.c            | 11 ++-----
 posix/tst-execv2.c             |  8 ++---
 posix/tst-execve2.c            |  8 ++---
 posix/tst-execvp2.c            | 17 +++--------
 posix/tst-getopt-cancel.c      |  3 +-
 posix/tst-nice.c               |  3 +-
 posix/wordexp-test.c           | 12 ++++++--
 rt/tst-cpuclock2.c             |  4 ++-
 rt/tst-cputimer1.c             |  4 ++-
 rt/tst-cputimer2.c             |  4 ++-
 rt/tst-cputimer3.c             |  4 ++-
 stdio-common/bug12.c           | 12 ++++----
 stdio-common/bug19.c           |  9 ++++--
 stdio-common/bug3.c            |  4 ++-
 stdio-common/bug4.c            |  4 ++-
 stdio-common/bug5.c            |  4 ++-
 stdio-common/bug6.c            |  8 ++---
 stdio-common/test-fwrite.c     |  4 ++-
 stdio-common/test_rdwr.c       | 11 +++----
 stdio-common/tst-cookie.c      |  5 +++-
 stdio-common/tst-fmemopen3.c   |  4 ++-
 stdio-common/tst-fseek.c       |  5 ++--
 stdio-common/tst-perror.c      |  3 +-
 stdio-common/tstscanf.c        | 14 +++++++--
 stdlib/test-canon.c            | 25 +++++++++++++---
 sunrpc/netname.c               |  3 +-
 support/Makefile               |  4 +++
 support/test-container.c       | 15 +++++-----
 support/xfgets.c               | 32 ++++++++++++++++++++
 support/xfread.c               | 39 +++++++++++++++++++++++++
 support/xread.c                | 36 +++++++++++++++++++++++
 support/xstdio.h               |  2 ++
 support/xstdlib.h              | 31 ++++++++++++++++++++
 support/xsystem.c              | 29 +++++++++++++++++++
 support/xunistd.h              |  3 ++
 sysdeps/pthread/Makefile       |  2 +-
 sysdeps/pthread/eintr.c        |  6 ++--
 sysdeps/pthread/tst-cancel11.c |  4 ++-
 sysdeps/pthread/tst-cancel16.c |  6 +++-
 sysdeps/pthread/tst-cancel20.c | 10 ++-----
 sysdeps/pthread/tst-cancel21.c |  9 ++----
 sysdeps/pthread/tst-cancel4.c  |  6 ++--
 sysdeps/pthread/tst-cancel6.c  |  3 +-
 sysdeps/pthread/tst-cond18.c   |  4 ++-
 sysdeps/pthread/tst-fini1mod.c |  4 ++-
 sysdeps/pthread/tst-flock1.c   |  3 +-
 sysdeps/pthread/tst-flock2.c   |  3 +-
 sysdeps/pthread/tst-key1.c     | 11 +++----
 sysdeps/pthread/tst-signal1.c  |  3 +-
 sysdeps/pthread/tst-signal2.c  |  3 +-
 sysdeps/pthread/tst-timer.c    |  3 +-
 time/tst-cpuclock1.c           |  4 ++-
 93 files changed, 587 insertions(+), 214 deletions(-)
 create mode 100644 support/xfgets.c
 create mode 100644 support/xfread.c
 create mode 100644 support/xread.c
 create mode 100644 support/xstdlib.h
 create mode 100644 support/xsystem.c

-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 01/15] catgets/gencat.c: fix warn unused result
  2023-04-18 12:11 ` [PATCH 2/8] catgets/gencat.c: fix warn unused result Frédéric Bérat
  2023-04-18 12:36   ` Siddhesh Poyarekar
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-24 22:47     ` Siddhesh Poyarekar
  2023-06-01 14:27   ` [PATCH v5 01/12] " Frédéric Bérat
  2 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 catgets/gencat.c | 41 ++++++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/catgets/gencat.c b/catgets/gencat.c
index 61ac797349..ceaec67011 100644
--- a/catgets/gencat.c
+++ b/catgets/gencat.c
@@ -838,6 +838,26 @@ 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,
+	       "write of %zu bytes failed after %td: %m",
+	       length, p - (const char *) buffer);
+
+      if (ret == 0)
+	error (EXIT_FAILURE, 0,
+	       "write return 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,
@@ -927,12 +947,11 @@ write_out (struct catalog *catalog, const char *output_name,
   obj.plane_size = best_size;
   obj.plane_depth = best_depth;
 
+  uint32_t array_size = best_size * best_depth * sizeof (uint32_t) * 3;
   /* Allocate room for all needed arrays.  */
-  array1 =
-    (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
-  memset (array1, '\0', best_size * best_depth * sizeof (uint32_t) * 3);
-  array2
-    = (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
+  array1 = (uint32_t *) alloca (array_size);
+  memset (array1, '\0', array_size);
+  array2 = (uint32_t *) alloca (array_size);
   obstack_init (&string_pool);
 
   set_run = catalog->all_sets;
@@ -985,22 +1004,22 @@ write_out (struct catalog *catalog, const char *output_name,
     }
 
   /* Write out header.  */
-  write (fd, &obj, sizeof (obj));
+  write_all(fd, &obj, sizeof (obj));
 
   /* We always write out the little endian version of the index
      arrays.  */
 #if __BYTE_ORDER == __LITTLE_ENDIAN
-  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
-  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
+  write_all(fd, array1, array_size);
+  write_all(fd, array2, array_size);
 #elif __BYTE_ORDER == __BIG_ENDIAN
-  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
-  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
+  write_all(fd, array2, array_size);
+  write_all(fd, array1, array_size);
 #else
 # error Cannot handle __BYTE_ORDER byte order
 #endif
 
   /* Finally write the strings.  */
-  write (fd, strings, strings_size);
+  write_all(fd, strings, strings_size);
 
   if (fd != STDOUT_FILENO)
     close (fd);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 02/15] locale/programs/locarchive.c: fix warn unused result
  2023-04-18 12:11 ` [PATCH 4/8] locale/programs/locarchive.c: " Frédéric Bérat
  2023-04-18 12:43   ` Siddhesh Poyarekar
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-24 22:49     ` Siddhesh Poyarekar
  2023-06-01 14:27   ` [PATCH v5 02/12] malloc/{memusage.c,memusagestat.c}: " Frédéric Bérat
  2 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Changes since v2:
 - Freeing normalized_codeset before returning.

Changes since v3:
 - Rebased on master
--8<--

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 locale/programs/locarchive.c | 24 ++++++++++++++++--------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/locale/programs/locarchive.c b/locale/programs/locarchive.c
index 8d7d51af6c..71fd9f34fa 100644
--- a/locale/programs/locarchive.c
+++ b/locale/programs/locarchive.c
@@ -1154,10 +1154,14 @@ add_locale_to_archive (struct locarhandle *ah, const char *name,
   if (mask & XPG_NORM_CODESET)
     /* This name contains a codeset in unnormalized form.
        We will store it in the archive with a normalized name.  */
-    asprintf (&normalized_name, "%s%s%s.%s%s%s",
-	      language, territory == NULL ? "" : "_", territory ?: "",
-	      normalized_codeset,
-	      modifier == NULL ? "" : "@", modifier ?: "");
+    if (asprintf (&normalized_name, "%s%s%s.%s%s%s",
+		  language, territory == NULL ? "" : "_", territory ?: "",
+		  normalized_codeset,
+		  modifier == NULL ? "" : "@", modifier ?: "") < 0)
+      {
+	free ((char *) normalized_codeset);
+	return -1;
+      }
 
   /* This call does the main work.  */
   locrec_offset = add_locale (ah, normalized_name ?: name, data, replace);
@@ -1188,10 +1192,14 @@ add_locale_to_archive (struct locarhandle *ah, const char *name,
       normalized_codeset = _nl_normalize_codeset (codeset, strlen (codeset));
       mask |= XPG_NORM_CODESET;
 
-      asprintf (&normalized_codeset_name, "%s%s%s.%s%s%s",
-		language, territory == NULL ? "" : "_", territory ?: "",
-		normalized_codeset,
-		modifier == NULL ? "" : "@", modifier ?: "");
+      if (asprintf (&normalized_codeset_name, "%s%s%s.%s%s%s",
+		    language, territory == NULL ? "" : "_", territory ?: "",
+		    normalized_codeset,
+		    modifier == NULL ? "" : "@", modifier ?: "") < 0)
+	{
+	  free ((char *) normalized_codeset);
+	  return -1;
+	}
 
       add_alias (ah, normalized_codeset_name, replace,
 		 normalized_name ?: name, &locrec_offset);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 03/15] malloc/{memusage.c,memusagestat.c}: fix warn unused result
  2023-04-18 12:11 ` [PATCH 5/8] " Frédéric Bérat
  2023-04-18 12:47   ` [PATCH 5/8] malloc/{memusage.c, memusagestat.c}: " Siddhesh Poyarekar
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-25  0:50     ` Siddhesh Poyarekar
  1 sibling, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 malloc/memusage.c     | 53 +++++++++++++++++++++++++++++++++----------
 malloc/memusagestat.c | 48 +++++++++++++++++++++++++++++++++++----
 2 files changed, 85 insertions(+), 16 deletions(-)

diff --git a/malloc/memusage.c b/malloc/memusage.c
index 2a3a508557..0c414b0e85 100644
--- a/malloc/memusage.c
+++ b/malloc/memusage.c
@@ -18,6 +18,8 @@
 
 #include <assert.h>
 #include <dlfcn.h>
+#include <errno.h>
+#include <error.h>
 #include <fcntl.h>
 #include <stdatomic.h>
 #include <stdbool.h>
@@ -142,6 +144,27 @@ 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,
+	       "write of %zu bytes failed after %td: %m",
+	       length, p - (const char *) buffer);
+
+      if (ret == 0)
+	error (EXIT_FAILURE, 0,
+	       "write return 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)
@@ -210,10 +233,11 @@ update_data (struct header *result, size_t len, size_t old_len)
       gettime (&buffer[idx]);
 
       /* Write out buffer if it is full.  */
-      if (idx + 1 == buffer_size)
-        write (fd, buffer, buffer_size * sizeof (struct entry));
-      else if (idx + 1 == 2 * buffer_size)
-        write (fd, &buffer[buffer_size], buffer_size * sizeof (struct entry));
+      if (idx + 1 == buffer_size || idx + 1 == 2 * buffer_size)
+        {
+	  uint32_t write_size = buffer_size * sizeof (buffer[0]);
+	  write_all (fd, &buffer[idx + 1 - buffer_size], write_size);
+        }
     }
 }
 
@@ -299,8 +323,8 @@ me (void)
               first.stack = 0;
               gettime (&first);
               /* Write it two times since we need the starting and end time. */
-              write (fd, &first, sizeof (first));
-              write (fd, &first, sizeof (first));
+	      write_all (fd, &first, sizeof (first));
+	      write_all (fd, &first, sizeof (first));
 
               /* Determine the buffer size.  We use the default if the
                  environment variable is not present.  */
@@ -850,24 +874,29 @@ dest (void)
   if (fd != -1)
     {
       /* Write the partially filled buffer.  */
+      struct entry *start = buffer;
+      uint32_t write_cnt = buffer_cnt;
+
       if (buffer_cnt > buffer_size)
-        write (fd, buffer + buffer_size,
-               (buffer_cnt - buffer_size) * sizeof (struct entry));
-      else
-        write (fd, buffer, buffer_cnt * sizeof (struct entry));
+        {
+          start = buffer + buffer_size;
+          write_cnt = buffer_cnt - buffer_size;
+        }
+
+      write_all (fd, start, write_cnt * sizeof (buffer[0]));
 
       /* Go back to the beginning of the file.  We allocated two records
          here when we opened the file.  */
       lseek (fd, 0, SEEK_SET);
       /* Write out a record containing the total size.  */
       first.stack = peak_total;
-      write (fd, &first, sizeof (struct entry));
+      write_all (fd, &first, sizeof (first));
       /* Write out another record containing the maximum for heap and
          stack.  */
       first.heap = peak_heap;
       first.stack = peak_stack;
       gettime (&first);
-      write (fd, &first, sizeof (struct entry));
+      write_all (fd, &first, sizeof (first));
 
       /* Close the file.  */
       close (fd);
diff --git a/malloc/memusagestat.c b/malloc/memusagestat.c
index 67c5131f79..9fe6dba2d8 100644
--- a/malloc/memusagestat.c
+++ b/malloc/memusagestat.c
@@ -114,6 +114,45 @@ 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,
+	       "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,
+	       "write of %zu bytes failed after %td: %m",
+	       length, p - (const char *) buffer);
+
+      if (ret == 0)
+	error (EXIT_FAILURE, 0,
+	       "write return 0 after writing %td bytes of %zu",
+	       p - (const char *) buffer, length);
+      p += ret;
+    }
+}
+
+
 int
 main (int argc, char *argv[])
 {
@@ -188,7 +227,7 @@ main (int argc, char *argv[])
   total = st.st_size / sizeof (struct entry) - 2;
 
   /* Read the administrative information.  */
-  read (fd, headent, sizeof (headent));
+  read_all (fd, headent, sizeof (headent));
   maxsize_heap = headent[1].heap;
   maxsize_stack = headent[1].stack;
   maxsize_total = headent[0].stack;
@@ -220,7 +259,8 @@ main (int argc, char *argv[])
 
       /* Write the computed values in the file.  */
       lseek (fd, 0, SEEK_SET);
-      write (fd, headent, 2 * sizeof (struct entry));
+      write_all (fd, headent, sizeof (headent));
+
     }
 
   if (also_total)
@@ -372,7 +412,7 @@ main (int argc, char *argv[])
           size_t new[2];
           uint64_t now;
 
-          read (fd, &entry, sizeof (entry));
+          read_all (fd, &entry, sizeof (entry));
 
           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
 
@@ -455,7 +495,7 @@ main (int argc, char *argv[])
           size_t xpos;
           uint64_t now;
 
-          read (fd, &entry, sizeof (entry));
+          read_all (fd, &entry, sizeof (entry));
 
           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
           xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 04/15] nptl_db/thread_dbP.h: fix warn unused result
  2023-04-18 12:11 ` [PATCH 6/8] nptl_db/thread_dbP.h: fix warn unused result Frédéric Bérat
  2023-04-18 12:49   ` Siddhesh Poyarekar
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-25  0:51     ` Siddhesh Poyarekar
  2023-06-01 14:27   ` [PATCH v5 03/12] " Frédéric Bérat
  2 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 nptl_db/thread_dbP.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/nptl_db/thread_dbP.h b/nptl_db/thread_dbP.h
index 8845722109..4a9333658b 100644
--- a/nptl_db/thread_dbP.h
+++ b/nptl_db/thread_dbP.h
@@ -61,7 +61,7 @@ enum
 
 /* Comment out the following for less verbose output.  */
 #ifndef NDEBUG
-# define LOG(c) if (__td_debug) write (2, c "\n", strlen (c "\n"))
+# define LOG(c) if (__td_debug) assert (write (2, c "\n", strlen (c "\n")) == strlen (c "\n"))
 extern int __td_debug attribute_hidden;
 #else
 # define LOG(c)
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 05/15] sunrpc/netname.c: fix warn unused result
  2023-04-18 12:11 ` [PATCH 7/8] sunrpc/netname.c: " Frédéric Bérat
  2023-04-18 12:51   ` Siddhesh Poyarekar
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-25  0:53     ` Siddhesh Poyarekar
  1 sibling, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 sunrpc/netname.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/sunrpc/netname.c b/sunrpc/netname.c
index dd2cb1b441..d9d82501b0 100644
--- a/sunrpc/netname.c
+++ b/sunrpc/netname.c
@@ -96,7 +96,8 @@ host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
       else
 	{
 	  domainname[0] = 0;
-	  getdomainname (domainname, MAXHOSTNAMELEN);
+	  if (getdomainname (domainname, MAXHOSTNAMELEN))
+	    return 0;
 	}
     }
   else
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 06/15] sysdeps/pthread/eintr.c: fix warn unused result
  2023-04-18 12:11 ` [PATCH 8/8] sysdeps/pthread/eintr.c: " Frédéric Bérat
  2023-04-18 12:54   ` Siddhesh Poyarekar
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-25  0:59     ` Siddhesh Poyarekar
  1 sibling, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
 sysdeps/pthread/eintr.c | 6 ++++--
 1 file changed, 4 insertions(+), 2 deletions(-)

diff --git a/sysdeps/pthread/eintr.c b/sysdeps/pthread/eintr.c
index 000649d24e..16191395a7 100644
--- a/sysdeps/pthread/eintr.c
+++ b/sysdeps/pthread/eintr.c
@@ -31,10 +31,12 @@ eintr_handler (int sig)
 {
   if (sig != the_sig)
     {
-      write (STDOUT_FILENO, "eintr_handler: signal number wrong\n", 35);
+      /* empty if statement avoids warn unused result */
+      if (write (STDOUT_FILENO,
+		 "eintr_handler: signal number wrong\n", 35) < 35) {};
       _exit (1);
     }
-  write (STDOUT_FILENO, ".", 1);
+  if (write (STDOUT_FILENO, ".", 1)) {/* Avoid warn unused result */};
 }
 
 
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 07/15] tests: fix warn unused result on asprintf calls
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
  2023-04-18 12:23   ` Siddhesh Poyarekar
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-25  1:07     ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 08/15] tests: replace write by xwrite Frédéric Bérat
                     ` (10 subsequent siblings)
  12 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

When enabling _FORTIFY_SOURCE, some functions now lead to warnings when
their result is not checked.
---
 argp/argp-test.c           |  8 +++++---
 posix/tst-execl2.c         |  8 ++------
 posix/tst-execle2.c        |  8 ++------
 posix/tst-execlp2.c        | 11 +++--------
 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, 28 insertions(+), 53 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 <string.h>
 #include <argp.h>
 
+#include <support/support.h>
+
 const char *argp_program_version = "argp-test 1.0";
 \f
 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..8f10d4b7f8 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");
@@ -59,8 +55,7 @@ do_test (void)
       return 1;
     }
   char *path;
-  asprintf (&path, "%s:../libio:../elf", bindir);
-  if (path == NULL)
+  if (asprintf (&path, "%s:../libio:../elf", bindir) < 0 || path == NULL)
     {
       puts ("asprintf  failed");
       return 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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/support.h>
+
 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 <stdio.h>
 #include <string.h>
 
+#include <support/support.h>
+
 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 <time.h>
 #include <sys/stat.h>
 
+#include <support/support.h>
 
 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);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 08/15] tests: replace write by xwrite
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
  2023-04-18 12:23   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 07/15] tests: fix warn unused result on asprintf calls Frédéric Bérat
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-25  1:16     ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 09/15] tests: replace read by xread Frédéric Bérat
                     ` (9 subsequent siblings)
  12 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Using write without cheks leads to warn unused result when __wur is
enabled.
---
 dirent/tst-fdopendir.c        |  4 +++-
 io/tst-faccessat.c            |  3 ++-
 io/tst-fchmodat.c             |  3 ++-
 io/tst-fchownat.c             |  3 ++-
 io/tst-fstatat.c              |  3 ++-
 io/tst-futimesat.c            |  3 ++-
 io/tst-linkat.c               |  3 ++-
 io/tst-openat.c               |  3 ++-
 io/tst-renameat.c             |  3 ++-
 io/tst-symlinkat.c            |  3 ++-
 io/tst-unlinkat.c             |  3 ++-
 libio/bug-ungetc.c            |  4 +++-
 libio/bug-ungetc3.c           |  4 +++-
 libio/bug-ungetc4.c           |  4 +++-
 libio/bug-wfflush.c           |  4 +++-
 libio/bug-wsetpos.c           |  4 +++-
 nptl/tst-stackguard1.c        |  4 +++-
 nptl/tst-tls3.c               |  2 ++
 nptl/tst-tls3mod.c            |  5 +++--
 rt/tst-cpuclock2.c            |  4 +++-
 rt/tst-cputimer1.c            |  4 +++-
 rt/tst-cputimer2.c            |  4 +++-
 rt/tst-cputimer3.c            |  4 +++-
 support/test-container.c      |  8 ++++----
 sysdeps/pthread/tst-cond18.c  |  4 +++-
 sysdeps/pthread/tst-flock1.c  |  3 ++-
 sysdeps/pthread/tst-flock2.c  |  3 ++-
 sysdeps/pthread/tst-key1.c    | 11 ++++++-----
 sysdeps/pthread/tst-signal1.c |  3 ++-
 sysdeps/pthread/tst-signal2.c |  3 ++-
 sysdeps/pthread/tst-timer.c   |  3 ++-
 time/tst-cpuclock1.c          |  4 +++-
 32 files changed, 84 insertions(+), 39 deletions(-)

diff --git a/dirent/tst-fdopendir.c b/dirent/tst-fdopendir.c
index 6321af1daa..2c9520574d 100644
--- a/dirent/tst-fdopendir.c
+++ b/dirent/tst-fdopendir.c
@@ -7,6 +7,8 @@
 #include <string.h>
 #include <sys/stat.h>
 
+#include <support/xunistd.h>
+
 #ifndef O_NOATIME
 # define O_NOATIME	0
 #endif
@@ -22,7 +24,7 @@ do_test (void)
       return 1;
     }
 
-  write (fd, "hello", 5);
+  xwrite (fd, "hello", 5);
   close (fd);
 
   struct stat64 st;
diff --git a/io/tst-faccessat.c b/io/tst-faccessat.c
index 7bdeed008c..b90954e318 100644
--- a/io/tst-faccessat.c
+++ b/io/tst-faccessat.c
@@ -8,6 +8,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 
+#include <support/xunistd.h>
 
 static void prepare (void);
 #define PREPARE(argc, argv) prepare ()
@@ -96,7 +97,7 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  xwrite (fd, "hello", 5);
   puts ("file created");
 
   /* Before closing the file, try using this file descriptor to open
diff --git a/io/tst-fchmodat.c b/io/tst-fchmodat.c
index 7d4a8717ff..83003e2f21 100644
--- a/io/tst-fchmodat.c
+++ b/io/tst-fchmodat.c
@@ -8,6 +8,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 
+#include <support/xunistd.h>
 
 static void prepare (void);
 #define PREPARE(argc, argv) prepare ()
@@ -98,7 +99,7 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  xwrite (fd, "hello", 5);
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-fchownat.c b/io/tst-fchownat.c
index e8adf6229f..c0b87cda8f 100644
--- a/io/tst-fchownat.c
+++ b/io/tst-fchownat.c
@@ -6,6 +6,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 
+#include <support/xunistd.h>
 
 static void prepare (void);
 #define PREPARE(argc, argv) prepare ()
@@ -106,7 +107,7 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  xwrite (fd, "hello", 5);
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-fstatat.c b/io/tst-fstatat.c
index 4766bb2e71..6a60024b63 100644
--- a/io/tst-fstatat.c
+++ b/io/tst-fstatat.c
@@ -6,6 +6,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 
+#include <support/xunistd.h>
 
 static void prepare (void);
 #define PREPARE(argc, argv) prepare ()
@@ -94,7 +95,7 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  xwrite (fd, "hello", 5);
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-futimesat.c b/io/tst-futimesat.c
index 3d41721f42..b7ef386e06 100644
--- a/io/tst-futimesat.c
+++ b/io/tst-futimesat.c
@@ -28,6 +28,7 @@
 
 #include <support/test-driver.h>
 #include <support/temp_file.h>
+#include <support/xunistd.h>
 
 #ifndef struct_stat
 # define struct_stat struct stat64
@@ -114,7 +115,7 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  xwrite (fd, "hello", 5);
   puts ("file created");
 
   struct_stat st1;
diff --git a/io/tst-linkat.c b/io/tst-linkat.c
index 97445b7954..6b22a01c88 100644
--- a/io/tst-linkat.c
+++ b/io/tst-linkat.c
@@ -6,6 +6,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 
+#include <support/xunistd.h>
 
 static void prepare (void);
 #define PREPARE(argc, argv) prepare ()
@@ -94,7 +95,7 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  xwrite (fd, "hello", 5);
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-openat.c b/io/tst-openat.c
index 741b8d0ad2..2ce89e3db1 100644
--- a/io/tst-openat.c
+++ b/io/tst-openat.c
@@ -6,6 +6,7 @@
 #include <string.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
 
 static void prepare (void);
 #define PREPARE(argc, argv) prepare ()
@@ -94,7 +95,7 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  xwrite (fd, "hello", 5);
 
   /* Before closing the file, try using this file descriptor to open
      another file.  This must fail.  */
diff --git a/io/tst-renameat.c b/io/tst-renameat.c
index 435302b52b..0b9da5fd6d 100644
--- a/io/tst-renameat.c
+++ b/io/tst-renameat.c
@@ -6,6 +6,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 
+#include <support/xunistd.h>
 
 static void prepare (void);
 #define PREPARE(argc, argv) prepare ()
@@ -94,7 +95,7 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  xwrite (fd, "hello", 5);
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-symlinkat.c b/io/tst-symlinkat.c
index 214a8e348e..4a34994df7 100644
--- a/io/tst-symlinkat.c
+++ b/io/tst-symlinkat.c
@@ -6,6 +6,7 @@
 #include <unistd.h>
 #include <sys/stat.h>
 
+#include <support/xunistd.h>
 
 static void prepare (void);
 #define PREPARE(argc, argv) prepare ()
@@ -94,7 +95,7 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  xwrite (fd, "hello", 5);
   puts ("file created");
 
   struct stat64 st1;
diff --git a/io/tst-unlinkat.c b/io/tst-unlinkat.c
index e21d56f9f7..21a2dbaf57 100644
--- a/io/tst-unlinkat.c
+++ b/io/tst-unlinkat.c
@@ -6,6 +6,7 @@
 #include <string.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
 
 static void prepare (void);
 #define PREPARE(argc, argv) prepare ()
@@ -94,7 +95,7 @@ do_test (void)
       puts ("file creation failed");
       return 1;
     }
-  write (fd, "hello", 5);
+  xwrite (fd, "hello", 5);
   close (fd);
   puts ("file created");
 
diff --git a/libio/bug-ungetc.c b/libio/bug-ungetc.c
index 51940b68f5..4ea2d14ed6 100644
--- a/libio/bug-ungetc.c
+++ b/libio/bug-ungetc.c
@@ -2,6 +2,8 @@
 
 #include <stdio.h>
 
+#include <support/xunistd.h>
+
 static void do_prepare (void);
 #define PREPARE(argc, argv) do_prepare ()
 static int do_test (void);
@@ -20,7 +22,7 @@ do_prepare (void)
       printf ("cannot create temporary file: %m\n");
       exit (1);
     }
-  write (fd, pattern, sizeof (pattern));
+  xwrite (fd, pattern, sizeof (pattern));
   close (fd);
 }
 
diff --git a/libio/bug-ungetc3.c b/libio/bug-ungetc3.c
index 0c83c1161e..6100d7a936 100644
--- a/libio/bug-ungetc3.c
+++ b/libio/bug-ungetc3.c
@@ -2,6 +2,8 @@
 
 #include <stdio.h>
 
+#include <support/xunistd.h>
+
 static void do_prepare (void);
 #define PREPARE(argc, argv) do_prepare ()
 static int do_test (void);
@@ -20,7 +22,7 @@ do_prepare (void)
       printf ("cannot create temporary file: %m\n");
       exit (1);
     }
-  write (fd, pattern, sizeof (pattern));
+  xwrite (fd, pattern, sizeof (pattern));
   close (fd);
 }
 
diff --git a/libio/bug-ungetc4.c b/libio/bug-ungetc4.c
index 0bd02a570d..8a05def686 100644
--- a/libio/bug-ungetc4.c
+++ b/libio/bug-ungetc4.c
@@ -18,6 +18,8 @@
 
 #include <stdio.h>
 
+#include <support/xunistd.h>
+
 static void do_prepare (void);
 #define PREPARE(argc, argv) do_prepare ()
 static int do_test (void);
@@ -36,7 +38,7 @@ do_prepare (void)
       printf ("cannot create temporary file: %m\n");
       exit (1);
     }
-  write (fd, pattern, sizeof (pattern) - 1);
+  xwrite (fd, pattern, sizeof (pattern) - 1);
   close (fd);
 }
 
diff --git a/libio/bug-wfflush.c b/libio/bug-wfflush.c
index a8fd61e997..d1b9d8e9de 100644
--- a/libio/bug-wfflush.c
+++ b/libio/bug-wfflush.c
@@ -3,6 +3,8 @@
 #include <stdio.h>
 #include <wchar.h>
 
+#include <support/xunistd.h>
+
 static void do_prepare (void);
 #define PREPARE(argc, argv) do_prepare ()
 static int do_test (void);
@@ -20,7 +22,7 @@ do_prepare (void)
       printf ("cannot create temporary file: %m\n");
       exit (1);
     }
-  write (fd, "1!", 2);
+  xwrite (fd, "1!", 2);
   close (fd);
 }
 
diff --git a/libio/bug-wsetpos.c b/libio/bug-wsetpos.c
index ccb22a4b62..0fc373ba49 100644
--- a/libio/bug-wsetpos.c
+++ b/libio/bug-wsetpos.c
@@ -4,6 +4,8 @@
 #include <stdio.h>
 #include <wchar.h>
 
+#include <support/xunistd.h>
+
 static void do_prepare (void);
 #define PREPARE(argc, argv) do_prepare ()
 static int do_test (void);
@@ -22,7 +24,7 @@ do_prepare (void)
       printf ("cannot create temporary file: %m\n");
       exit (1);
     }
-  write (fd, pattern, sizeof (pattern));
+  xwrite (fd, pattern, sizeof (pattern));
   close (fd);
 }
 
diff --git a/nptl/tst-stackguard1.c b/nptl/tst-stackguard1.c
index b9cf6844de..4ac57157e9 100644
--- a/nptl/tst-stackguard1.c
+++ b/nptl/tst-stackguard1.c
@@ -27,6 +27,8 @@
 #include <tls.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 static const char *command;
 static bool child;
 static uintptr_t stack_chk_guard_copy;
@@ -96,7 +98,7 @@ do_test (void)
 	else if (ret != NULL)
 	  return 1;
 
-      write (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy));
+      xwrite (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy));
       return 0;
     }
 
diff --git a/nptl/tst-tls3.c b/nptl/tst-tls3.c
index b1a40c624a..33d94c8cc5 100644
--- a/nptl/tst-tls3.c
+++ b/nptl/tst-tls3.c
@@ -26,6 +26,8 @@
 #include <unistd.h>
 #include <pthreaddef.h>
 
+#include <support/xunistd.h>
+
 #define THE_SIG SIGUSR1
 
 /* The stack size can be overriden.  With a sufficiently large stack
diff --git a/nptl/tst-tls3mod.c b/nptl/tst-tls3mod.c
index c6e8910b1e..345a48e1c7 100644
--- a/nptl/tst-tls3mod.c
+++ b/nptl/tst-tls3mod.c
@@ -25,6 +25,7 @@
 #include <pthreaddef.h>
 #include <descr.h>
 
+#include <support/xunistd.h>
 
 extern pthread_barrier_t b;
 
@@ -43,7 +44,7 @@ handler (int sig)
 {
   if (sig != THE_SIG)
     {
-      write (STDOUT_FILENO, "wrong signal\n", 13);
+      xwrite (STDOUT_FILENO, "wrong signal\n", 13);
       _exit (1);
     }
 
@@ -51,7 +52,7 @@ handler (int sig)
 
   if (sem_post (&s) != 0)
     {
-      write (STDOUT_FILENO, "sem_post failed\n", 16);
+      xwrite (STDOUT_FILENO, "sem_post failed\n", 16);
       _exit (1);
     }
 }
diff --git a/rt/tst-cpuclock2.c b/rt/tst-cpuclock2.c
index e4584d8791..8afd34ed9c 100644
--- a/rt/tst-cpuclock2.c
+++ b/rt/tst-cpuclock2.c
@@ -37,6 +37,8 @@ do_test ()
 #include <errno.h>
 #include <pthread.h>
 
+#include <support/xunistd.h>
+
 static pthread_barrier_t barrier;
 
 /* This function is intended to rack up both user and system time.  */
@@ -55,7 +57,7 @@ chew_cpu (void *arg)
       for (int i = 0; i < 100; ++i)
 	for (size_t j = 0; j < sizeof buf; ++j)
 	  buf[j] = 0xbb;
-      write (nullfd, (char *) buf, sizeof buf);
+      xwrite (nullfd, (char *) buf, sizeof buf);
       close (nullfd);
     }
 
diff --git a/rt/tst-cputimer1.c b/rt/tst-cputimer1.c
index 8f5dd76cf2..18d8b195a2 100644
--- a/rt/tst-cputimer1.c
+++ b/rt/tst-cputimer1.c
@@ -11,6 +11,8 @@
 #include <time.h>
 #include <pthread.h>
 
+#include <support/xunistd.h>
+
 #define TEST_CLOCK CLOCK_PROCESS_CPUTIME_ID
 #define TEST_CLOCK_MISSING(clock) \
   (setup_test () ? "process CPU clock timer support" : NULL)
@@ -29,7 +31,7 @@ chew_cpu (void *arg)
       for (int i = 0; i < 100; ++i)
 	for (size_t j = 0; j < sizeof buf; ++j)
 	  buf[j] = 0xbb;
-      write (nullfd, (char *) buf, sizeof buf);
+      xwrite (nullfd, (char *) buf, sizeof buf);
       close (nullfd);
     }
 
diff --git a/rt/tst-cputimer2.c b/rt/tst-cputimer2.c
index 397d7998c0..a5700d4bac 100644
--- a/rt/tst-cputimer2.c
+++ b/rt/tst-cputimer2.c
@@ -12,6 +12,8 @@
 #include <time.h>
 #include <pthread.h>
 
+#include <support/xunistd.h>
+
 static clockid_t worker_thread_clock;
 
 #define TEST_CLOCK worker_thread_clock
@@ -32,7 +34,7 @@ chew_cpu (void *arg)
       for (int i = 0; i < 100; ++i)
 	for (size_t j = 0; j < sizeof buf; ++j)
 	  buf[j] = 0xbb;
-      write (nullfd, (char *) buf, sizeof buf);
+      xwrite (nullfd, (char *) buf, sizeof buf);
       close (nullfd);
     }
 
diff --git a/rt/tst-cputimer3.c b/rt/tst-cputimer3.c
index 056766a377..786de93a02 100644
--- a/rt/tst-cputimer3.c
+++ b/rt/tst-cputimer3.c
@@ -13,6 +13,8 @@
 #include <signal.h>
 #include <sys/wait.h>
 
+#include <support/xunistd.h>
+
 static clockid_t child_clock;
 
 #define TEST_CLOCK child_clock
@@ -33,7 +35,7 @@ chew_cpu (void)
       for (int i = 0; i < 100; ++i)
 	for (size_t j = 0; j < sizeof buf; ++j)
 	  buf[j] = 0xbb;
-      write (nullfd, (char *) buf, sizeof buf);
+      xwrite (nullfd, (char *) buf, sizeof buf);
       close (nullfd);
       if (getppid () == 1)
 	_exit (2);
diff --git a/support/test-container.c b/support/test-container.c
index e68f16eecf..d4ca41fe7c 100644
--- a/support/test-container.c
+++ b/support/test-container.c
@@ -1186,7 +1186,7 @@ main (int argc, char **argv)
       int status;
 
       /* Send the child's "outside" pid to it.  */
-      write (pipes[1], &child, sizeof(child));
+      xwrite (pipes[1], &child, sizeof(child));
       close (pipes[0]);
       close (pipes[1]);
 
@@ -1255,7 +1255,7 @@ main (int argc, char **argv)
 
       sprintf (tmp, "%lld %lld 1\n",
 	       (long long) (be_su ? 0 : original_uid), (long long) original_uid);
-      write (UMAP, tmp, strlen (tmp));
+      xwrite (UMAP, tmp, strlen (tmp));
       xclose (UMAP);
 
       /* We must disable setgroups () before we can map our groups, else we
@@ -1264,7 +1264,7 @@ main (int argc, char **argv)
       if (GMAP >= 0)
 	{
 	  /* We support kernels old enough to not have this.  */
-	  write (GMAP, "deny\n", 5);
+	  xwrite (GMAP, "deny\n", 5);
 	  xclose (GMAP);
 	}
 
@@ -1276,7 +1276,7 @@ main (int argc, char **argv)
 
       sprintf (tmp, "%lld %lld 1\n",
 	       (long long) (be_su ? 0 : original_gid), (long long) original_gid);
-      write (GMAP, tmp, strlen (tmp));
+      xwrite (GMAP, tmp, strlen (tmp));
       xclose (GMAP);
     }
 
diff --git a/sysdeps/pthread/tst-cond18.c b/sysdeps/pthread/tst-cond18.c
index edac4fa4ff..ffae356c04 100644
--- a/sysdeps/pthread/tst-cond18.c
+++ b/sysdeps/pthread/tst-cond18.c
@@ -24,6 +24,8 @@
 #include <stdio.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
 pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
 bool exiting;
@@ -40,7 +42,7 @@ tf (void *id)
       while (!exiting)
 	{
 	  if ((spins++ % 1000) == 0)
-	    write (fd, ".", 1);
+	    xwrite (fd, ".", 1);
 	  pthread_mutex_unlock (&lock);
 
 	  pthread_mutex_lock (&lock);
diff --git a/sysdeps/pthread/tst-flock1.c b/sysdeps/pthread/tst-flock1.c
index 7eef9070ab..9de148afd3 100644
--- a/sysdeps/pthread/tst-flock1.c
+++ b/sysdeps/pthread/tst-flock1.c
@@ -21,6 +21,7 @@
 #include <unistd.h>
 #include <sys/file.h>
 
+#include <support/xunistd.h>
 
 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
 
@@ -56,7 +57,7 @@ do_test (void)
 
   unlink (tmp);
 
-  write (fd, "foobar xyzzy", 12);
+  xwrite (fd, "foobar xyzzy", 12);
 
   if (flock (fd, LOCK_EX | LOCK_NB) != 0)
     {
diff --git a/sysdeps/pthread/tst-flock2.c b/sysdeps/pthread/tst-flock2.c
index 8762e93b52..952b79e5db 100644
--- a/sysdeps/pthread/tst-flock2.c
+++ b/sysdeps/pthread/tst-flock2.c
@@ -24,6 +24,7 @@
 #include <sys/mman.h>
 #include <sys/wait.h>
 
+#include <support/xunistd.h>
 
 static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
 static pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;
@@ -70,7 +71,7 @@ do_test (void)
 
   int i;
   for (i = 0; i < 20; ++i)
-    write (fd, "foobar xyzzy", 12);
+    xwrite (fd, "foobar xyzzy", 12);
 
   pthread_barrier_t *b;
   b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
diff --git a/sysdeps/pthread/tst-key1.c b/sysdeps/pthread/tst-key1.c
index 933edafef8..60245c4e47 100644
--- a/sysdeps/pthread/tst-key1.c
+++ b/sysdeps/pthread/tst-key1.c
@@ -21,6 +21,7 @@
 #include <stdlib.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
 
 static int do_test (void);
 
@@ -51,7 +52,7 @@ do_test (void)
 
 	if (pthread_setspecific (keys[i], (const void *) (i + 100l)) != 0)
 	  {
-	    write (2, "setspecific failed\n", 19);
+	    xwrite (2, "setspecific failed\n", 19);
 	    _exit (1);
 	  }
       }
@@ -60,13 +61,13 @@ do_test (void)
     {
       if (pthread_getspecific (keys[i]) != (void *) (i + 100l))
 	{
-	  write (2, "getspecific failed\n", 19);
+	  xwrite (2, "getspecific failed\n", 19);
 	  _exit (1);
 	}
 
       if (pthread_key_delete (keys[i]) != 0)
 	{
-	  write (2, "key_delete failed\n", 18);
+	  xwrite (2, "key_delete failed\n", 18);
 	  _exit (1);
 	}
     }
@@ -74,13 +75,13 @@ do_test (void)
   /* Now it must be once again possible to allocate keys.  */
   if (pthread_key_create (&keys[0], NULL) != 0)
     {
-      write (2, "2nd key_create failed\n", 22);
+      xwrite (2, "2nd key_create failed\n", 22);
       _exit (1);
     }
 
   if (pthread_key_delete (keys[0]) != 0)
     {
-      write (2, "2nd key_delete failed\n", 22);
+      xwrite (2, "2nd key_delete failed\n", 22);
       _exit (1);
     }
 
diff --git a/sysdeps/pthread/tst-signal1.c b/sysdeps/pthread/tst-signal1.c
index d1073e8459..d1082027ca 100644
--- a/sysdeps/pthread/tst-signal1.c
+++ b/sysdeps/pthread/tst-signal1.c
@@ -25,6 +25,7 @@
 #include <sys/mman.h>
 #include <sys/wait.h>
 
+#include <support/xunistd.h>
 
 static sigset_t ss;
 static pthread_barrier_t *b;
@@ -105,7 +106,7 @@ do_test (void)
 
   int i;
   for (i = 0; i < 20; ++i)
-    write (fd, "foobar xyzzy", 12);
+    xwrite (fd, "foobar xyzzy", 12);
 
   b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
 	    MAP_SHARED, fd, 0);
diff --git a/sysdeps/pthread/tst-signal2.c b/sysdeps/pthread/tst-signal2.c
index dfe7d9f64a..15b7747877 100644
--- a/sysdeps/pthread/tst-signal2.c
+++ b/sysdeps/pthread/tst-signal2.c
@@ -25,6 +25,7 @@
 #include <sys/wait.h>
 #include <string.h>
 
+#include <support/xunistd.h>
 
 static sigset_t ss;
 static pthread_barrier_t *b;
@@ -111,7 +112,7 @@ do_test (void)
 
   int i;
   for (i = 0; i < 20; ++i)
-    write (fd, "foobar xyzzy", 12);
+    xwrite (fd, "foobar xyzzy", 12);
 
   b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
 	    MAP_SHARED, fd, 0);
diff --git a/sysdeps/pthread/tst-timer.c b/sysdeps/pthread/tst-timer.c
index 47472ab8e1..4cfe0b67dc 100644
--- a/sysdeps/pthread/tst-timer.c
+++ b/sysdeps/pthread/tst-timer.c
@@ -24,6 +24,7 @@
 #include <stdlib.h>
 #include <stdint.h>
 
+#include <support/xunistd.h>
 
 static void
 notify_func1 (union sigval sigval)
@@ -44,7 +45,7 @@ signal_func (int sig)
 {
   static const char text[] = "signal_func\n";
   signal (sig, signal_func);
-  write (STDOUT_FILENO, text, sizeof text - 1);
+  xwrite (STDOUT_FILENO, text, sizeof text - 1);
 }
 
 static void
diff --git a/time/tst-cpuclock1.c b/time/tst-cpuclock1.c
index 6f2e70a58a..6a793e06df 100644
--- a/time/tst-cpuclock1.c
+++ b/time/tst-cpuclock1.c
@@ -27,6 +27,8 @@
 #include <stdint.h>
 #include <sys/wait.h>
 
+#include <support/xunistd.h>
+
 /* This function is intended to rack up both user and system time.  */
 static void
 chew_cpu (void)
@@ -41,7 +43,7 @@ chew_cpu (void)
       for (int i = 0; i < 100; ++i)
 	for (size_t j = 0; j < sizeof buf; ++j)
 	  buf[j] = 0xbb;
-      write (nullfd, (char *) buf, sizeof buf);
+      xwrite (nullfd, (char *) buf, sizeof buf);
       close (nullfd);
       if (getppid () == 1)
 	_exit (2);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 09/15] tests: replace read by xread
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
                     ` (2 preceding siblings ...)
  2023-04-28 12:21   ` [PATCH v4 08/15] tests: replace write by xwrite Frédéric Bérat
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-04-28 12:21   ` [PATCH v4 10/15] tests: replace system by xsystem Frédéric Bérat
                     ` (8 subsequent siblings)
  12 siblings, 0 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Changes since v2:
 - Add missing xunistd.h includes
 - Add libsupport to tst-fini1mod.so

--8<--
With fortification enabled, read calls return result needs to be checked,
has it gets the __wur macro enabled.
---
 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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 /* 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 71d5148513..a4ac86544c 100644
--- a/support/Makefile
+++ b/support/Makefile
@@ -193,6 +193,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 d4ca41fe7c..37beb778d6 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xunistd.h>
+
+#include <support/check.h>
+
+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 c2f5588bd9..86d41a8c88 100644
--- a/sysdeps/pthread/Makefile
+++ b/sysdeps/pthread/Makefile
@@ -275,7 +275,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 <string.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 
 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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 
 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 <sys/wait.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
 
 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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 
 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);
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 10/15] tests: replace system by xsystem
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
                     ` (3 preceding siblings ...)
  2023-04-28 12:21   ` [PATCH v4 09/15] tests: replace read by xread Frédéric Bérat
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-25  1:22     ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 11/15] tests: replace fread by xfread Frédéric Bérat
                     ` (7 subsequent siblings)
  12 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

With fortification enabled, system calls return result needs to be checked,
has it gets the __wur macro enabled.
---
 elf/tst-stackguard1.c     |  2 +-
 libio/bug-mmap-fflush.c   |  7 +++++--
 nptl/tst-cancel7.c        |  3 ++-
 nptl/tst-stackguard1.c    |  2 +-
 nss/tst-nss-db-endpwent.c |  3 ++-
 support/Makefile          |  1 +
 support/xstdlib.h         | 31 +++++++++++++++++++++++++++++++
 support/xsystem.c         | 29 +++++++++++++++++++++++++++++
 8 files changed, 72 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..7dbde6568c 100644
--- a/elf/tst-stackguard1.c
+++ b/elf/tst-stackguard1.c
@@ -108,7 +108,7 @@ do_test (void)
 	  dup2 (fds[1], 2);
 	  close (fds[1]);
 
-	  system (command);
+	  if (system (command)) {/* We aren't interested on the result */}
 	  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 <stdlib.h>
 #include <string.h>
 
+#include <support/xstdlib.h>
 
 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 <support/support.h>
 #include <support/temp_file.h>
 #include <support/xstdio.h>
+#include <support/xstdlib.h>
 #include <support/xunistd.h>
 #include <support/xthread.h>
 
@@ -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..9ec9e8f982 100644
--- a/nptl/tst-stackguard1.c
+++ b/nptl/tst-stackguard1.c
@@ -140,7 +140,7 @@ do_test (void)
 	  dup2 (fds[1], 2);
 	  close (fds[1]);
 
-	  system (command);
+	  if (system (command)) {/* We aren't interested in the result */}
 	  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 <support/support.h>
 #include <support/check.h>
+#include <support/xstdlib.h>
 
 /* 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 a4ac86544c..4c8eb3dedd 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..58642e3542
--- /dev/null
+++ b/support/xstdlib.h
@@ -0,0 +1,31 @@
+/* Error-checking wrappers for stdlib functions.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef SUPPORT_XSTDLIB_H
+#define SUPPORT_XSTDLIB_H
+
+#include <stdlib.h>
+#include <sys/cdefs.h>
+
+__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..8ceaad1531
--- /dev/null
+++ b/support/xsystem.c
@@ -0,0 +1,29 @@
+/* Error-checking replacement for "system".
+   Copyright (C) 2018-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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/support.h>
+#include <support/check.h>
+
+#include <support/xstdlib.h>
+
+void
+xsystem (const char *cmd)
+{
+  if (system (cmd) < 0)
+    FAIL_EXIT1 ("system (\"%s\")", cmd);
+}
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 11/15] tests: replace fread by xfread
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
                     ` (4 preceding siblings ...)
  2023-04-28 12:21   ` [PATCH v4 10/15] tests: replace system by xsystem Frédéric Bérat
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-04-28 12:21   ` [PATCH v4 12/15] tests: replace ftruncate by xftruncate Frédéric Bérat
                     ` (6 subsequent siblings)
  12 siblings, 0 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Changes since v2:
 - Fixed xfread prototype
 - Add missing xstdio.h include
--8<--
With fortification enabled, fread calls return result needs to be checked,
has it gets the __wur macro enabled.
---
 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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xstdio.h>
 
 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 <stdio.h>
 #include <string.h>
 
+#include <support/xstdio.h>
+
 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 <stdio.h>
 #include <string.h>
 
+#include <support/xstdio.h>
+
 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 <unistd.h>
 #include <string.h>
 
+#include <support/xstdio.h>
+
 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 <stdio.h>
 
+#include <support/xstdio.h>
+
 
 #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 3cc2832edc..9297e24ba5 100644
--- a/stdio-common/tst-fmemopen3.c
+++ b/stdio-common/tst-fmemopen3.c
@@ -21,6 +21,8 @@
 #include <string.h>
 #include <sys/types.h>
 
+#include <support/xstdio.h>
+
 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 4c8eb3dedd..c9b3364810 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xstdio.h>
+
+#include <support/check.h>
+#include <stdlib.h>
+
+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
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 12/15] tests: replace ftruncate by xftruncate
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
                     ` (5 preceding siblings ...)
  2023-04-28 12:21   ` [PATCH v4 11/15] tests: replace fread by xfread Frédéric Bérat
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-25  1:25     ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 13/15] tests: replace fgets by xfgets Frédéric Bérat
                     ` (5 subsequent siblings)
  12 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

With fortification enabled, ftruncate calls return result needs to be
checked, has it gets the __wur macro enabled.
---
 io/tst-copy_file_range.c  | 2 +-
 posix/tst-getopt-cancel.c | 3 ++-
 stdio-common/tst-perror.c | 3 ++-
 3 files changed, 5 insertions(+), 3 deletions(-)

diff --git a/io/tst-copy_file_range.c b/io/tst-copy_file_range.c
index d1f3aaa5a9..bd64e9c42b 100644
--- a/io/tst-copy_file_range.c
+++ b/io/tst-copy_file_range.c
@@ -166,7 +166,7 @@ short_copy (void)
           inoff = 3;
           xlseek (infd, shift, SEEK_SET);
         }
-      ftruncate (outfd, 0);
+      xftruncate (outfd, 0);
       xlseek (outfd, 0, SEEK_SET);
       outoff = 0;
 
diff --git a/posix/tst-getopt-cancel.c b/posix/tst-getopt-cancel.c
index 7167d1a914..6f49391690 100644
--- a/posix/tst-getopt-cancel.c
+++ b/posix/tst-getopt-cancel.c
@@ -33,6 +33,7 @@
 #include <support/support.h>
 #include <support/temp_file.h>
 #include <support/xthread.h>
+#include <support/xunistd.h>
 
 static bool
 check_stderr (bool expect_errmsg, FILE *stderr_trapped)
@@ -48,7 +49,7 @@ check_stderr (bool expect_errmsg, FILE *stderr_trapped)
       fputs (lineptr, stdout);
     }
   rewind (stderr_trapped);
-  ftruncate (fileno (stderr_trapped), 0);
+  xftruncate (fileno (stderr_trapped), 0);
   return got_errmsg == expect_errmsg;
 }
 
diff --git a/stdio-common/tst-perror.c b/stdio-common/tst-perror.c
index 57835e0c59..b4ab583462 100644
--- a/stdio-common/tst-perror.c
+++ b/stdio-common/tst-perror.c
@@ -9,6 +9,7 @@
 #include <unistd.h>
 #include <wchar.h>
 
+#include <support/xunistd.h>
 
 #define MB_EXP \
   "null mode test 1: Invalid or incomplete multibyte or wide character\n" \
@@ -94,7 +95,7 @@ do_test (void)
     puts ("multibyte test succeeded");
 
   lseek (fd, 0, SEEK_SET);
-  ftruncate (fd, 0);
+  xftruncate (fd, 0);
 
   if (dup2 (fd, 2) == -1)
     {
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 13/15] tests: replace fgets by xfgets
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
                     ` (6 preceding siblings ...)
  2023-04-28 12:21   ` [PATCH v4 12/15] tests: replace ftruncate by xftruncate Frédéric Bérat
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-04-28 12:21   ` [PATCH v4 14/15] tests: Replace various function calls with their x variant Frédéric Bérat
                     ` (4 subsequent siblings)
  12 siblings, 0 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Changes since v2:
 - Rebased
--8<--
With fortification enabled, fgets calls return result needs to be checked,
has it gets the __wur macro enabled.
---
 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 <string.h>
 #include <setjmp.h>
 
+#include <support/xstdio.h>
+
 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 <string.h>
 #include <setjmp.h>
 
+#include <support/xstdio.h>
+
 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 <stdlib.h>
 #include <string.h>
 
+#include <support/xstdio.h>
 
 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 c9b3364810..344e63e74e 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xstdio.h>
+
+#include <support/check.h>
+#include <stdlib.h>
+
+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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xstdio.h>
 
 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;
 }
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 14/15] tests: Replace various function calls with their x variant
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
                     ` (7 preceding siblings ...)
  2023-04-28 12:21   ` [PATCH v4 13/15] tests: replace fgets by xfgets Frédéric Bérat
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-25  1:29     ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 15/15] tests: fix warn unused results Frédéric Bérat
                     ` (3 subsequent siblings)
  12 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

With fortification enabled, few function calls return result need to be
checked, has they get the __wur macro enabled.
---
 misc/tst-error1.c    | 2 +-
 nss/tst-reload2.c    | 6 +++++-
 posix/tst-chmod.c    | 9 +++++++--
 posix/wordexp-test.c | 6 +++++-
 stdlib/test-canon.c  | 7 ++++++-
 5 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/misc/tst-error1.c b/misc/tst-error1.c
index 9c4a62fbd0..65e3fd0c0e 100644
--- a/misc/tst-error1.c
+++ b/misc/tst-error1.c
@@ -9,7 +9,7 @@ static int
 do_test (int argc, char *argv[])
 {
   mtrace ();
-  (void) freopen (argc == 1 ? "/dev/stdout" : argv[1], "a", stderr);
+  if (freopen (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..b25e5e3528 100644
--- a/nss/tst-reload2.c
+++ b/nss/tst-reload2.c
@@ -121,7 +121,11 @@ do_test (void)
   /* Change the root dir.  */
 
   TEST_VERIFY (chroot ("/subdir") == 0);
-  chdir ("/");
+  if (chdir ("/") < 0)
+    {
+      printf("Failed to change directory: %m");
+      return 1;
+    }
 
   /* Check we're NOT using the "inner" nsswitch.conf.  */
 
diff --git a/posix/tst-chmod.c b/posix/tst-chmod.c
index b98a05a265..bec2d2b8eb 100644
--- a/posix/tst-chmod.c
+++ b/posix/tst-chmod.c
@@ -229,7 +229,12 @@ do_test (int argc, char *argv[])
   close (fd);
 
   snprintf (buf, buflen, "%s/..", testdir);
-  chdir (buf);
+  if (chdir (buf))
+    {
+      printf ("cannot change directory: %m\n");
+      result = 1;
+      goto fail;
+    }
   /* We are now in the directory above the one we create the test
      directory in.  */
 
@@ -349,7 +354,7 @@ do_test (int argc, char *argv[])
     }
 
  fail:
-  chdir (startdir);
+  if (chdir (startdir)) {}
 
   /* Remove all the files.  */
   chmod (testdir, 0700);
diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c
index bae27d6cee..87d537c931 100644
--- a/posix/wordexp-test.c
+++ b/posix/wordexp-test.c
@@ -332,7 +332,11 @@ do_test (int argc, char *argv[])
   if (cwd == NULL)
     cwd = "..";
 
-  chdir (cwd);
+  if (chdir (cwd) < 0)
+    {
+      printf ("failed to change dir: %m");
+      return 1;
+    }
   rmdir (tmpdir);
 
   return 0;
diff --git a/stdlib/test-canon.c b/stdlib/test-canon.c
index 4edee73dd8..5a2e7e1e6e 100644
--- a/stdlib/test-canon.c
+++ b/stdlib/test-canon.c
@@ -154,7 +154,12 @@ do_test (int argc, char ** argv)
     }
 
   for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
-    symlink (symlinks[i].value, symlinks[i].name);
+    if (symlink (symlinks[i].value, symlinks[i].name))
+      {
+        printf ("%s: Unable to create symlink for %s -> %s\n",
+		argv[0], symlinks[i].name, symlinks[i].value);
+        ++errors;
+      }
 
   int has_dir = mkdir ("doesExist", 0777) == 0;
 
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v4 15/15] tests: fix warn unused results
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
                     ` (8 preceding siblings ...)
  2023-04-28 12:21   ` [PATCH v4 14/15] tests: Replace various function calls with their x variant Frédéric Bérat
@ 2023-04-28 12:21   ` Frédéric Bérat
  2023-05-25  1:35     ` Siddhesh Poyarekar
  2023-06-01 14:27   ` [PATCH v5 04/12] tests: fix warn unused result on asprintf calls Frédéric Bérat
                     ` (2 subsequent siblings)
  12 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-04-28 12:21 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

With fortification enabled, few function calls return result need to be
checked, has they get the __wur macro enabled.
---
 crypt/cert.c                   |  6 +++++-
 misc/tst-efgcvt-template.c     |  4 ++--
 posix/tst-nice.c               |  3 +--
 posix/wordexp-test.c           |  6 +++++-
 stdio-common/bug19.c           |  9 +++++++--
 stdio-common/bug6.c            |  8 ++++----
 stdio-common/tstscanf.c        | 14 ++++++++++++--
 stdlib/test-canon.c            | 18 +++++++++++++++---
 support/test-container.c       |  4 ++--
 sysdeps/pthread/tst-cancel16.c |  6 +++++-
 sysdeps/pthread/tst-cancel4.c  |  6 ++++--
 11 files changed, 62 insertions(+), 22 deletions(-)

diff --git a/crypt/cert.c b/crypt/cert.c
index 32c4386caf..5b4277f76d 100644
--- a/crypt/cert.c
+++ b/crypt/cert.c
@@ -99,7 +99,11 @@ get8 (char *cp)
 	int i,j,t;
 
 	for(i=0;i<8;i++){
-		scanf("%2x",&t);
+		if (scanf("%2x",&t) < 1)
+		  {
+		    if(ferror(stdin))
+		      totfails++;
+		  }
 		if(feof(stdin))
 		  good_bye();
 		for(j=0; j<8 ; j++) {
diff --git a/misc/tst-efgcvt-template.c b/misc/tst-efgcvt-template.c
index b924659a3d..87e3ebe4fa 100644
--- a/misc/tst-efgcvt-template.c
+++ b/misc/tst-efgcvt-template.c
@@ -200,8 +200,8 @@ special (void)
     output_error (NAME (ECVT), INFINITY, 10, "inf", 0, 0, p, decpt, sign);
 
   /* Simply make sure these calls with large NDIGITs don't crash.  */
-  (void) ECVT (123.456, 10000, &decpt, &sign);
-  (void) FCVT (123.456, 10000, &decpt, &sign);
+  p = ECVT (123.456, 10000, &decpt, &sign);
+  p = FCVT (123.456, 10000, &decpt, &sign);
 
   /* Some tests for the reentrant functions.  */
   /* Use a too small buffer.  */
diff --git a/posix/tst-nice.c b/posix/tst-nice.c
index fe9888b3f6..59cf953e27 100644
--- a/posix/tst-nice.c
+++ b/posix/tst-nice.c
@@ -58,8 +58,7 @@ do_test (void)
 
   /* BZ #18086. Make sure we don't reset errno.  */
   errno = EBADF;
-  nice (0);
-  if (errno != EBADF)
+  if (nice (0) == -1 || errno != EBADF)
     {
       printf ("FAIL: errno = %i, but wanted EBADF (%i)\n", errno, EBADF);
       return 1;
diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c
index 87d537c931..057c89dd3c 100644
--- a/posix/wordexp-test.c
+++ b/posix/wordexp-test.c
@@ -253,7 +253,11 @@ do_test (int argc, char *argv[])
   cwd = getcwd (NULL, 0);
 
   /* Set up arena for pathname expansion */
-  tmpnam (tmpdir);
+  if (!tmpnam (tmpdir))
+    {
+      printf ("Failed to create a temporary directory with a unique name: %m");
+      return 1;
+    }
   xmkdir (tmpdir, S_IRWXU);
   TEST_VERIFY_EXIT (chdir (tmpdir) == 0);
 
diff --git a/stdio-common/bug19.c b/stdio-common/bug19.c
index e083304bda..9a3deac3fc 100644
--- a/stdio-common/bug19.c
+++ b/stdio-common/bug19.c
@@ -29,12 +29,17 @@ do_test (void)
   printf("checking sscanf\n");
 
   int i, j, n;
+  int result = 0;
 
   i = j = n = 0;
-  FSCANF (fp, L(" %i - %i %n"), &i, &j, &n);
+  if (FSCANF (fp, L(" %i - %i %n"), &i, &j, &n) < 2)
+    {
+      printf ("FSCANF couldn't read all parameters %d\n", errno);
+      result = 1;
+    }
+
   printf ("found %i-%i (length=%i)\n", i, j, n);
 
-  int result = 0;
   if (i != 7)
     {
       printf ("i is %d, expected 7\n", i);
diff --git a/stdio-common/bug6.c b/stdio-common/bug6.c
index 0db63a3b44..50098bf3f2 100644
--- a/stdio-common/bug6.c
+++ b/stdio-common/bug6.c
@@ -7,16 +7,16 @@ main (void)
   int i;
   int lost = 0;
 
-  scanf ("%2s", buf);
+  lost = (scanf ("%2s", buf) < 0);
   lost |= (buf[0] != 'X' || buf[1] != 'Y' || buf[2] != '\0');
   if (lost)
     puts ("test of %2s failed.");
-  scanf (" ");
-  scanf ("%d", &i);
+  lost |= (scanf (" ") < 0);
+  lost |= (scanf ("%d", &i) < 0);
   lost |= (i != 1234);
   if (lost)
     puts ("test of %d failed.");
-  scanf ("%c", buf);
+  lost |= (scanf ("%c", buf) < 0);
   lost |= (buf[0] != 'L');
   if (lost)
     puts ("test of %c failed.\n");
diff --git a/stdio-common/tstscanf.c b/stdio-common/tstscanf.c
index 3a4ebf7524..7e92df4720 100644
--- a/stdio-common/tstscanf.c
+++ b/stdio-common/tstscanf.c
@@ -120,7 +120,12 @@ main (int argc, char **argv)
     int i;
     float x;
     char name[50];
-    (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
+    if (fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name) < 3)
+      {
+	fputs ("test failed!\n", stdout);
+	result = 1;
+      }
+
     fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
     if (i != 56 || x != 789.0F || strcmp (name, "56"))
       {
@@ -164,7 +169,12 @@ main (int argc, char **argv)
 	quant = 0.0;
 	units[0] = item[0] = '\0';
 	count = fscanf (in, "%f%20s of %20s", &quant, units, item);
-	(void) fscanf (in, "%*[^\n]");
+	if (fscanf (in, "%*[^\n]") < 0 && ferror (in))
+	  {
+	    fputs ("test failed!\n", stdout);
+	    result = 1;
+	  }
+
 	fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
 		 count, quant, item, units);
 	if (count != ok[rounds-1].count || quant != ok[rounds-1].quant
diff --git a/stdlib/test-canon.c b/stdlib/test-canon.c
index 5a2e7e1e6e..61fe893609 100644
--- a/stdlib/test-canon.c
+++ b/stdlib/test-canon.c
@@ -123,8 +123,15 @@ do_test (int argc, char ** argv)
   int i, errors = 0;
   char buf[PATH_MAX];
 
-  getcwd (cwd, sizeof (buf));
-  cwd_len = strlen (cwd);
+  if (getcwd (cwd, sizeof (buf)))
+    {
+      cwd_len = strlen (cwd);
+    }
+  else
+    {
+      printf ("%s: current working directory couldn't be retrieved\n", argv[0]);
+      ++errors;
+    }
 
   errno = 0;
   if (realpath (NULL, buf) != NULL || errno != EINVAL)
@@ -210,7 +217,12 @@ do_test (int argc, char ** argv)
       free (result2);
     }
 
-  getcwd (buf, sizeof (buf));
+  if (!getcwd (buf, sizeof (buf)))
+    {
+      printf ("%s: current working directory couldn't be retrieved\n", argv[0]);
+      ++errors;
+    }
+
   if (strcmp (buf, cwd))
     {
       printf ("%s: current working directory changed from %s to %s\n",
diff --git a/support/test-container.c b/support/test-container.c
index 37beb778d6..f51afefedb 100644
--- a/support/test-container.c
+++ b/support/test-container.c
@@ -714,8 +714,8 @@ check_for_unshare_hints (int require_pidns)
         continue;
 
       val = -1; /* Sentinel.  */
-      fscanf (f, "%d", &val);
-      if (val != files[i].bad_value)
+      int cnt = fscanf (f, "%d", &val);
+      if (cnt == 1 && val != files[i].bad_value)
 	continue;
 
       printf ("To enable test-container, please run this as root:\n");
diff --git a/sysdeps/pthread/tst-cancel16.c b/sysdeps/pthread/tst-cancel16.c
index 511b9e1e91..d47c7c68cb 100644
--- a/sysdeps/pthread/tst-cancel16.c
+++ b/sysdeps/pthread/tst-cancel16.c
@@ -50,7 +50,11 @@ tf (void *arg)
   pthread_cleanup_push (cl, NULL);
 
   /* This call should never return.  */
-  (void) lockf (fd, F_LOCK, 0);
+  if (lockf (fd, F_LOCK, 0))
+    {
+      puts ("child thread: lockf failed");
+      exit (1);
+    }
 
   pthread_cleanup_pop (0);
 
diff --git a/sysdeps/pthread/tst-cancel4.c b/sysdeps/pthread/tst-cancel4.c
index 4f5c89314c..4c9e8670ca 100644
--- a/sysdeps/pthread/tst-cancel4.c
+++ b/sysdeps/pthread/tst-cancel4.c
@@ -1009,7 +1009,8 @@ tf_pread (void *arg)
   pthread_cleanup_push (cl, NULL);
 
   char mem[10];
-  pread (tempfd, mem, sizeof (mem), 0);
+  if (pread (tempfd, mem, sizeof (mem), 0) < 0)
+    FAIL_EXIT1 ("pread failed: %m");
 
   pthread_cleanup_pop (0);
 
@@ -1038,7 +1039,8 @@ tf_pwrite (void *arg)
   pthread_cleanup_push (cl, NULL);
 
   char mem[10];
-  pwrite (tempfd, mem, sizeof (mem), 0);
+  if (pwrite (tempfd, mem, sizeof (mem), 0) <0)
+    FAIL_EXIT1 ("pwrite failed: %m");
 
   pthread_cleanup_pop (0);
 
-- 
2.39.2


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 01/15] catgets/gencat.c: fix warn unused result
  2023-04-28 12:21   ` [PATCH v4 01/15] " Frédéric Bérat
@ 2023-05-24 22:47     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-24 22:47 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-04-28 08:21, Frédéric Bérat wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   catgets/gencat.c | 41 ++++++++++++++++++++++++++++++-----------
>   1 file changed, 30 insertions(+), 11 deletions(-)
> 
> diff --git a/catgets/gencat.c b/catgets/gencat.c
> index 61ac797349..ceaec67011 100644
> --- a/catgets/gencat.c
> +++ b/catgets/gencat.c
> @@ -838,6 +838,26 @@ 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,
> +	       "write of %zu bytes failed after %td: %m",
> +	       length, p - (const char *) buffer);
> +
> +      if (ret == 0)
> +	error (EXIT_FAILURE, 0,
> +	       "write return 0 after writing %td bytes of %zu",
> +	       p - (const char *) buffer, length);

Shouldn't the strings be marked for translation with gettext?  It looks 
OK to me otherwise.

Thanks,
Sid

> +      p += ret;
> +    }
> +}
>   
>   static void
>   write_out (struct catalog *catalog, const char *output_name,
> @@ -927,12 +947,11 @@ write_out (struct catalog *catalog, const char *output_name,
>     obj.plane_size = best_size;
>     obj.plane_depth = best_depth;
>   
> +  uint32_t array_size = best_size * best_depth * sizeof (uint32_t) * 3;
>     /* Allocate room for all needed arrays.  */
> -  array1 =
> -    (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
> -  memset (array1, '\0', best_size * best_depth * sizeof (uint32_t) * 3);
> -  array2
> -    = (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
> +  array1 = (uint32_t *) alloca (array_size);
> +  memset (array1, '\0', array_size);
> +  array2 = (uint32_t *) alloca (array_size);
>     obstack_init (&string_pool);
>   
>     set_run = catalog->all_sets;
> @@ -985,22 +1004,22 @@ write_out (struct catalog *catalog, const char *output_name,
>       }
>   
>     /* Write out header.  */
> -  write (fd, &obj, sizeof (obj));
> +  write_all(fd, &obj, sizeof (obj));
>   
>     /* We always write out the little endian version of the index
>        arrays.  */
>   #if __BYTE_ORDER == __LITTLE_ENDIAN
> -  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
> -  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
> +  write_all(fd, array1, array_size);
> +  write_all(fd, array2, array_size);
>   #elif __BYTE_ORDER == __BIG_ENDIAN
> -  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
> -  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
> +  write_all(fd, array2, array_size);
> +  write_all(fd, array1, array_size);
>   #else
>   # error Cannot handle __BYTE_ORDER byte order
>   #endif
>   
>     /* Finally write the strings.  */
> -  write (fd, strings, strings_size);
> +  write_all(fd, strings, strings_size);
>   
>     if (fd != STDOUT_FILENO)
>       close (fd);

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 02/15] locale/programs/locarchive.c: fix warn unused result
  2023-04-28 12:21   ` [PATCH v4 02/15] " Frédéric Bérat
@ 2023-05-24 22:49     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-24 22:49 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-04-28 08:21, Frédéric Bérat wrote:
> Changes since v2:
>   - Freeing normalized_codeset before returning.
> 
> Changes since v3:
>   - Rebased on master
> --8<--
> 
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   locale/programs/locarchive.c | 24 ++++++++++++++++--------
>   1 file changed, 16 insertions(+), 8 deletions(-)

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

PS: Put the bits you don't want in the git commit (e.g. the changelog 
for your patch) after the --- above.  I've never been able to get the 
scissors to work correctly.

> 
> diff --git a/locale/programs/locarchive.c b/locale/programs/locarchive.c
> index 8d7d51af6c..71fd9f34fa 100644
> --- a/locale/programs/locarchive.c
> +++ b/locale/programs/locarchive.c
> @@ -1154,10 +1154,14 @@ add_locale_to_archive (struct locarhandle *ah, const char *name,
>     if (mask & XPG_NORM_CODESET)
>       /* This name contains a codeset in unnormalized form.
>          We will store it in the archive with a normalized name.  */
> -    asprintf (&normalized_name, "%s%s%s.%s%s%s",
> -	      language, territory == NULL ? "" : "_", territory ?: "",
> -	      normalized_codeset,
> -	      modifier == NULL ? "" : "@", modifier ?: "");
> +    if (asprintf (&normalized_name, "%s%s%s.%s%s%s",
> +		  language, territory == NULL ? "" : "_", territory ?: "",
> +		  normalized_codeset,
> +		  modifier == NULL ? "" : "@", modifier ?: "") < 0)
> +      {
> +	free ((char *) normalized_codeset);
> +	return -1;
> +      }
>   
>     /* This call does the main work.  */
>     locrec_offset = add_locale (ah, normalized_name ?: name, data, replace);
> @@ -1188,10 +1192,14 @@ add_locale_to_archive (struct locarhandle *ah, const char *name,
>         normalized_codeset = _nl_normalize_codeset (codeset, strlen (codeset));
>         mask |= XPG_NORM_CODESET;
>   
> -      asprintf (&normalized_codeset_name, "%s%s%s.%s%s%s",
> -		language, territory == NULL ? "" : "_", territory ?: "",
> -		normalized_codeset,
> -		modifier == NULL ? "" : "@", modifier ?: "");
> +      if (asprintf (&normalized_codeset_name, "%s%s%s.%s%s%s",
> +		    language, territory == NULL ? "" : "_", territory ?: "",
> +		    normalized_codeset,
> +		    modifier == NULL ? "" : "@", modifier ?: "") < 0)
> +	{
> +	  free ((char *) normalized_codeset);
> +	  return -1;
> +	}
>   
>         add_alias (ah, normalized_codeset_name, replace,
>   		 normalized_name ?: name, &locrec_offset);

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 03/15] malloc/{memusage.c,memusagestat.c}: fix warn unused result
  2023-04-28 12:21   ` [PATCH v4 03/15] malloc/{memusage.c,memusagestat.c}: " Frédéric Bérat
@ 2023-05-25  0:50     ` Siddhesh Poyarekar
  2023-06-01 15:55       ` [PATCH] Move {read,write}_all functions to a dedicated header Frédéric Bérat
  0 siblings, 1 reply; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  0:50 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-04-28 08:21, Frédéric Bérat wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   malloc/memusage.c     | 53 +++++++++++++++++++++++++++++++++----------
>   malloc/memusagestat.c | 48 +++++++++++++++++++++++++++++++++++----
>   2 files changed, 85 insertions(+), 16 deletions(-)
> 
> diff --git a/malloc/memusage.c b/malloc/memusage.c
> index 2a3a508557..0c414b0e85 100644
> --- a/malloc/memusage.c
> +++ b/malloc/memusage.c
> @@ -18,6 +18,8 @@
>   
>   #include <assert.h>
>   #include <dlfcn.h>
> +#include <errno.h>
> +#include <error.h>
>   #include <fcntl.h>
>   #include <stdatomic.h>
>   #include <stdbool.h>
> @@ -142,6 +144,27 @@ 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,
> +	       "write of %zu bytes failed after %td: %m",
> +	       length, p - (const char *) buffer);
> +
> +      if (ret == 0)
> +	error (EXIT_FAILURE, 0,
> +	       "write return 0 after writing %td bytes of %zu",
> +	       p - (const char *) buffer, length);

Same as 1/15, this needs to be a translatable string.  Also "write 
returned 0 after...".

Also since this is repeated thrice, I wonder if it should get pulled out 
into its own header and included in memusage.c, memusagestat.c and gencat.c.

> +      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)
> @@ -210,10 +233,11 @@ update_data (struct header *result, size_t len, size_t old_len)
>         gettime (&buffer[idx]);
>   
>         /* Write out buffer if it is full.  */
> -      if (idx + 1 == buffer_size)
> -        write (fd, buffer, buffer_size * sizeof (struct entry));
> -      else if (idx + 1 == 2 * buffer_size)
> -        write (fd, &buffer[buffer_size], buffer_size * sizeof (struct entry));
> +      if (idx + 1 == buffer_size || idx + 1 == 2 * buffer_size)
> +        {
> +	  uint32_t write_size = buffer_size * sizeof (buffer[0]);
> +	  write_all (fd, &buffer[idx + 1 - buffer_size], write_size);
> +        }
>       }
>   }
>   
> @@ -299,8 +323,8 @@ me (void)
>                 first.stack = 0;
>                 gettime (&first);
>                 /* Write it two times since we need the starting and end time. */
> -              write (fd, &first, sizeof (first));
> -              write (fd, &first, sizeof (first));
> +	      write_all (fd, &first, sizeof (first));
> +	      write_all (fd, &first, sizeof (first));
>   
>                 /* Determine the buffer size.  We use the default if the
>                    environment variable is not present.  */
> @@ -850,24 +874,29 @@ dest (void)
>     if (fd != -1)
>       {
>         /* Write the partially filled buffer.  */
> +      struct entry *start = buffer;
> +      uint32_t write_cnt = buffer_cnt;
> +
>         if (buffer_cnt > buffer_size)
> -        write (fd, buffer + buffer_size,
> -               (buffer_cnt - buffer_size) * sizeof (struct entry));
> -      else
> -        write (fd, buffer, buffer_cnt * sizeof (struct entry));
> +        {
> +          start = buffer + buffer_size;
> +          write_cnt = buffer_cnt - buffer_size;
> +        }
> +
> +      write_all (fd, start, write_cnt * sizeof (buffer[0]));
>   
>         /* Go back to the beginning of the file.  We allocated two records
>            here when we opened the file.  */
>         lseek (fd, 0, SEEK_SET);
>         /* Write out a record containing the total size.  */
>         first.stack = peak_total;
> -      write (fd, &first, sizeof (struct entry));
> +      write_all (fd, &first, sizeof (first));
>         /* Write out another record containing the maximum for heap and
>            stack.  */
>         first.heap = peak_heap;
>         first.stack = peak_stack;
>         gettime (&first);
> -      write (fd, &first, sizeof (struct entry));
> +      write_all (fd, &first, sizeof (first));
>   
>         /* Close the file.  */
>         close (fd);
> diff --git a/malloc/memusagestat.c b/malloc/memusagestat.c
> index 67c5131f79..9fe6dba2d8 100644
> --- a/malloc/memusagestat.c
> +++ b/malloc/memusagestat.c
> @@ -114,6 +114,45 @@ 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,
> +	       "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,
> +	       "write of %zu bytes failed after %td: %m",
> +	       length, p - (const char *) buffer);
> +
> +      if (ret == 0)
> +	error (EXIT_FAILURE, 0,
> +	       "write return 0 after writing %td bytes of %zu",
> +	       p - (const char *) buffer, length);
> +      p += ret;
> +    }
> +}
> +
> +
>   int
>   main (int argc, char *argv[])
>   {
> @@ -188,7 +227,7 @@ main (int argc, char *argv[])
>     total = st.st_size / sizeof (struct entry) - 2;
>   
>     /* Read the administrative information.  */
> -  read (fd, headent, sizeof (headent));
> +  read_all (fd, headent, sizeof (headent));
>     maxsize_heap = headent[1].heap;
>     maxsize_stack = headent[1].stack;
>     maxsize_total = headent[0].stack;
> @@ -220,7 +259,8 @@ main (int argc, char *argv[])
>   
>         /* Write the computed values in the file.  */
>         lseek (fd, 0, SEEK_SET);
> -      write (fd, headent, 2 * sizeof (struct entry));
> +      write_all (fd, headent, sizeof (headent));
> +
>       }
>   
>     if (also_total)
> @@ -372,7 +412,7 @@ main (int argc, char *argv[])
>             size_t new[2];
>             uint64_t now;
>   
> -          read (fd, &entry, sizeof (entry));
> +          read_all (fd, &entry, sizeof (entry));
>   
>             now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
>   
> @@ -455,7 +495,7 @@ main (int argc, char *argv[])
>             size_t xpos;
>             uint64_t now;
>   
> -          read (fd, &entry, sizeof (entry));
> +          read_all (fd, &entry, sizeof (entry));
>   
>             now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
>             xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 04/15] nptl_db/thread_dbP.h: fix warn unused result
  2023-04-28 12:21   ` [PATCH v4 04/15] " Frédéric Bérat
@ 2023-05-25  0:51     ` Siddhesh Poyarekar
  2023-05-25  1:52       ` Siddhesh Poyarekar
  0 siblings, 1 reply; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  0:51 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-04-28 08:21, Frédéric Bérat wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   nptl_db/thread_dbP.h | 2 +-
>   1 file changed, 1 insertion(+), 1 deletion(-)

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
> diff --git a/nptl_db/thread_dbP.h b/nptl_db/thread_dbP.h
> index 8845722109..4a9333658b 100644
> --- a/nptl_db/thread_dbP.h
> +++ b/nptl_db/thread_dbP.h
> @@ -61,7 +61,7 @@ enum
>   
>   /* Comment out the following for less verbose output.  */
>   #ifndef NDEBUG
> -# define LOG(c) if (__td_debug) write (2, c "\n", strlen (c "\n"))
> +# define LOG(c) if (__td_debug) assert (write (2, c "\n", strlen (c "\n")) == strlen (c "\n"))
>   extern int __td_debug attribute_hidden;
>   #else
>   # define LOG(c)

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 05/15] sunrpc/netname.c: fix warn unused result
  2023-04-28 12:21   ` [PATCH v4 05/15] " Frédéric Bérat
@ 2023-05-25  0:53     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  0:53 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha

On 2023-04-28 08:21, Frédéric Bérat wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   sunrpc/netname.c | 3 ++-
>   1 file changed, 2 insertions(+), 1 deletion(-)

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
> diff --git a/sunrpc/netname.c b/sunrpc/netname.c
> index dd2cb1b441..d9d82501b0 100644
> --- a/sunrpc/netname.c
> +++ b/sunrpc/netname.c
> @@ -96,7 +96,8 @@ host2netname (char netname[MAXNETNAMELEN + 1], const char *host,
>         else
>   	{
>   	  domainname[0] = 0;
> -	  getdomainname (domainname, MAXHOSTNAMELEN);
> +	  if (getdomainname (domainname, MAXHOSTNAMELEN))
> +	    return 0;
>   	}
>       }
>     else

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 06/15] sysdeps/pthread/eintr.c: fix warn unused result
  2023-04-28 12:21   ` [PATCH v4 06/15] " Frédéric Bérat
@ 2023-05-25  0:59     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  0:59 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-04-28 08:21, Frédéric Bérat wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
>   sysdeps/pthread/eintr.c | 6 ++++--
>   1 file changed, 4 insertions(+), 2 deletions(-)

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
> diff --git a/sysdeps/pthread/eintr.c b/sysdeps/pthread/eintr.c
> index 000649d24e..16191395a7 100644
> --- a/sysdeps/pthread/eintr.c
> +++ b/sysdeps/pthread/eintr.c
> @@ -31,10 +31,12 @@ eintr_handler (int sig)
>   {
>     if (sig != the_sig)
>       {
> -      write (STDOUT_FILENO, "eintr_handler: signal number wrong\n", 35);
> +      /* empty if statement avoids warn unused result */
> +      if (write (STDOUT_FILENO,
> +		 "eintr_handler: signal number wrong\n", 35) < 35) {};
>         _exit (1);
>       }
> -  write (STDOUT_FILENO, ".", 1);
> +  if (write (STDOUT_FILENO, ".", 1)) {/* Avoid warn unused result */};
>   }
>   
>   

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 07/15] tests: fix warn unused result on asprintf calls
  2023-04-28 12:21   ` [PATCH v4 07/15] tests: fix warn unused result on asprintf calls Frédéric Bérat
@ 2023-05-25  1:07     ` Siddhesh Poyarekar
  2023-05-31 14:36       ` Frederic Berat
  0 siblings, 1 reply; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  1:07 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha

On 2023-04-28 08:21, Frédéric Bérat wrote:
> When enabling _FORTIFY_SOURCE, some functions now lead to warnings when
> their result is not checked.
> ---
>   argp/argp-test.c           |  8 +++++---
>   posix/tst-execl2.c         |  8 ++------
>   posix/tst-execle2.c        |  8 ++------
>   posix/tst-execlp2.c        | 11 +++--------
>   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, 28 insertions(+), 53 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 <string.h>
>   #include <argp.h>
>   
> +#include <support/support.h>
> +
>   const char *argp_program_version = "argp-test 1.0";
>   \f
>   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..8f10d4b7f8 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");
> @@ -59,8 +55,7 @@ do_test (void)
>         return 1;
>       }
>     char *path;
> -  asprintf (&path, "%s:../libio:../elf", bindir);
> -  if (path == NULL)
> +  if (asprintf (&path, "%s:../libio:../elf", bindir) < 0 || path == NULL)

Couldn't this be:

   path = xasprintf (...);
   TEST_VERIFY (path != NULL);

Also this is an old style test that uses test-skeleton.c, it needs to be 
ported over to using support.h.  That's probably true for other tests 
you're touching too (sorry!)

>       {
>         puts ("asprintf  failed");
>         return 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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/support.h>
> +
>   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 <stdio.h>
>   #include <string.h>
>   
> +#include <support/support.h>
> +
>   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 <time.h>
>   #include <sys/stat.h>
>   
> +#include <support/support.h>
>   
>   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);

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 08/15] tests: replace write by xwrite
  2023-04-28 12:21   ` [PATCH v4 08/15] tests: replace write by xwrite Frédéric Bérat
@ 2023-05-25  1:16     ` Siddhesh Poyarekar
  2023-06-01 16:39       ` Siddhesh Poyarekar
  0 siblings, 1 reply; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  1:16 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-04-28 08:21, Frédéric Bérat wrote:
> Using write without cheks leads to warn unused result when __wur is
> enabled.
> ---
>   dirent/tst-fdopendir.c        |  4 +++-
>   io/tst-faccessat.c            |  3 ++-
>   io/tst-fchmodat.c             |  3 ++-
>   io/tst-fchownat.c             |  3 ++-
>   io/tst-fstatat.c              |  3 ++-
>   io/tst-futimesat.c            |  3 ++-
>   io/tst-linkat.c               |  3 ++-
>   io/tst-openat.c               |  3 ++-
>   io/tst-renameat.c             |  3 ++-
>   io/tst-symlinkat.c            |  3 ++-
>   io/tst-unlinkat.c             |  3 ++-
>   libio/bug-ungetc.c            |  4 +++-
>   libio/bug-ungetc3.c           |  4 +++-
>   libio/bug-ungetc4.c           |  4 +++-
>   libio/bug-wfflush.c           |  4 +++-
>   libio/bug-wsetpos.c           |  4 +++-
>   nptl/tst-stackguard1.c        |  4 +++-
>   nptl/tst-tls3.c               |  2 ++
>   nptl/tst-tls3mod.c            |  5 +++--
>   rt/tst-cpuclock2.c            |  4 +++-
>   rt/tst-cputimer1.c            |  4 +++-
>   rt/tst-cputimer2.c            |  4 +++-
>   rt/tst-cputimer3.c            |  4 +++-
>   support/test-container.c      |  8 ++++----
>   sysdeps/pthread/tst-cond18.c  |  4 +++-
>   sysdeps/pthread/tst-flock1.c  |  3 ++-
>   sysdeps/pthread/tst-flock2.c  |  3 ++-
>   sysdeps/pthread/tst-key1.c    | 11 ++++++-----
>   sysdeps/pthread/tst-signal1.c |  3 ++-
>   sysdeps/pthread/tst-signal2.c |  3 ++-
>   sysdeps/pthread/tst-timer.c   |  3 ++-
>   time/tst-cpuclock1.c          |  4 +++-
>   32 files changed, 84 insertions(+), 39 deletions(-)
> 
> diff --git a/dirent/tst-fdopendir.c b/dirent/tst-fdopendir.c
> index 6321af1daa..2c9520574d 100644
> --- a/dirent/tst-fdopendir.c
> +++ b/dirent/tst-fdopendir.c

The test needs to be ported to support.h, that's probably true for other 
tests in here too I'm afraid.  The change otherwise looks OK, so it just 
needs an additional patch that ports the tests to support.h.

Thanks,
Sid

> @@ -7,6 +7,8 @@
>   #include <string.h>
>   #include <sys/stat.h>
>   
> +#include <support/xunistd.h>
> +
>   #ifndef O_NOATIME
>   # define O_NOATIME	0
>   #endif
> @@ -22,7 +24,7 @@ do_test (void)
>         return 1;
>       }
>   
> -  write (fd, "hello", 5);
> +  xwrite (fd, "hello", 5);
>     close (fd);
>   
>     struct stat64 st;
> diff --git a/io/tst-faccessat.c b/io/tst-faccessat.c
> index 7bdeed008c..b90954e318 100644
> --- a/io/tst-faccessat.c
> +++ b/io/tst-faccessat.c
> @@ -8,6 +8,7 @@
>   #include <unistd.h>
>   #include <sys/stat.h>
>   
> +#include <support/xunistd.h>
>   
>   static void prepare (void);
>   #define PREPARE(argc, argv) prepare ()
> @@ -96,7 +97,7 @@ do_test (void)
>         puts ("file creation failed");
>         return 1;
>       }
> -  write (fd, "hello", 5);
> +  xwrite (fd, "hello", 5);
>     puts ("file created");
>   
>     /* Before closing the file, try using this file descriptor to open
> diff --git a/io/tst-fchmodat.c b/io/tst-fchmodat.c
> index 7d4a8717ff..83003e2f21 100644
> --- a/io/tst-fchmodat.c
> +++ b/io/tst-fchmodat.c
> @@ -8,6 +8,7 @@
>   #include <unistd.h>
>   #include <sys/stat.h>
>   
> +#include <support/xunistd.h>
>   
>   static void prepare (void);
>   #define PREPARE(argc, argv) prepare ()
> @@ -98,7 +99,7 @@ do_test (void)
>         puts ("file creation failed");
>         return 1;
>       }
> -  write (fd, "hello", 5);
> +  xwrite (fd, "hello", 5);
>     puts ("file created");
>   
>     struct stat64 st1;
> diff --git a/io/tst-fchownat.c b/io/tst-fchownat.c
> index e8adf6229f..c0b87cda8f 100644
> --- a/io/tst-fchownat.c
> +++ b/io/tst-fchownat.c
> @@ -6,6 +6,7 @@
>   #include <unistd.h>
>   #include <sys/stat.h>
>   
> +#include <support/xunistd.h>
>   
>   static void prepare (void);
>   #define PREPARE(argc, argv) prepare ()
> @@ -106,7 +107,7 @@ do_test (void)
>         puts ("file creation failed");
>         return 1;
>       }
> -  write (fd, "hello", 5);
> +  xwrite (fd, "hello", 5);
>     puts ("file created");
>   
>     struct stat64 st1;
> diff --git a/io/tst-fstatat.c b/io/tst-fstatat.c
> index 4766bb2e71..6a60024b63 100644
> --- a/io/tst-fstatat.c
> +++ b/io/tst-fstatat.c
> @@ -6,6 +6,7 @@
>   #include <unistd.h>
>   #include <sys/stat.h>
>   
> +#include <support/xunistd.h>
>   
>   static void prepare (void);
>   #define PREPARE(argc, argv) prepare ()
> @@ -94,7 +95,7 @@ do_test (void)
>         puts ("file creation failed");
>         return 1;
>       }
> -  write (fd, "hello", 5);
> +  xwrite (fd, "hello", 5);
>     puts ("file created");
>   
>     struct stat64 st1;
> diff --git a/io/tst-futimesat.c b/io/tst-futimesat.c
> index 3d41721f42..b7ef386e06 100644
> --- a/io/tst-futimesat.c
> +++ b/io/tst-futimesat.c
> @@ -28,6 +28,7 @@
>   
>   #include <support/test-driver.h>
>   #include <support/temp_file.h>
> +#include <support/xunistd.h>
>   
>   #ifndef struct_stat
>   # define struct_stat struct stat64
> @@ -114,7 +115,7 @@ do_test (void)
>         puts ("file creation failed");
>         return 1;
>       }
> -  write (fd, "hello", 5);
> +  xwrite (fd, "hello", 5);
>     puts ("file created");
>   
>     struct_stat st1;
> diff --git a/io/tst-linkat.c b/io/tst-linkat.c
> index 97445b7954..6b22a01c88 100644
> --- a/io/tst-linkat.c
> +++ b/io/tst-linkat.c
> @@ -6,6 +6,7 @@
>   #include <unistd.h>
>   #include <sys/stat.h>
>   
> +#include <support/xunistd.h>
>   
>   static void prepare (void);
>   #define PREPARE(argc, argv) prepare ()
> @@ -94,7 +95,7 @@ do_test (void)
>         puts ("file creation failed");
>         return 1;
>       }
> -  write (fd, "hello", 5);
> +  xwrite (fd, "hello", 5);
>     puts ("file created");
>   
>     struct stat64 st1;
> diff --git a/io/tst-openat.c b/io/tst-openat.c
> index 741b8d0ad2..2ce89e3db1 100644
> --- a/io/tst-openat.c
> +++ b/io/tst-openat.c
> @@ -6,6 +6,7 @@
>   #include <string.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
>   
>   static void prepare (void);
>   #define PREPARE(argc, argv) prepare ()
> @@ -94,7 +95,7 @@ do_test (void)
>         puts ("file creation failed");
>         return 1;
>       }
> -  write (fd, "hello", 5);
> +  xwrite (fd, "hello", 5);
>   
>     /* Before closing the file, try using this file descriptor to open
>        another file.  This must fail.  */
> diff --git a/io/tst-renameat.c b/io/tst-renameat.c
> index 435302b52b..0b9da5fd6d 100644
> --- a/io/tst-renameat.c
> +++ b/io/tst-renameat.c
> @@ -6,6 +6,7 @@
>   #include <unistd.h>
>   #include <sys/stat.h>
>   
> +#include <support/xunistd.h>
>   
>   static void prepare (void);
>   #define PREPARE(argc, argv) prepare ()
> @@ -94,7 +95,7 @@ do_test (void)
>         puts ("file creation failed");
>         return 1;
>       }
> -  write (fd, "hello", 5);
> +  xwrite (fd, "hello", 5);
>     puts ("file created");
>   
>     struct stat64 st1;
> diff --git a/io/tst-symlinkat.c b/io/tst-symlinkat.c
> index 214a8e348e..4a34994df7 100644
> --- a/io/tst-symlinkat.c
> +++ b/io/tst-symlinkat.c
> @@ -6,6 +6,7 @@
>   #include <unistd.h>
>   #include <sys/stat.h>
>   
> +#include <support/xunistd.h>
>   
>   static void prepare (void);
>   #define PREPARE(argc, argv) prepare ()
> @@ -94,7 +95,7 @@ do_test (void)
>         puts ("file creation failed");
>         return 1;
>       }
> -  write (fd, "hello", 5);
> +  xwrite (fd, "hello", 5);
>     puts ("file created");
>   
>     struct stat64 st1;
> diff --git a/io/tst-unlinkat.c b/io/tst-unlinkat.c
> index e21d56f9f7..21a2dbaf57 100644
> --- a/io/tst-unlinkat.c
> +++ b/io/tst-unlinkat.c
> @@ -6,6 +6,7 @@
>   #include <string.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
>   
>   static void prepare (void);
>   #define PREPARE(argc, argv) prepare ()
> @@ -94,7 +95,7 @@ do_test (void)
>         puts ("file creation failed");
>         return 1;
>       }
> -  write (fd, "hello", 5);
> +  xwrite (fd, "hello", 5);
>     close (fd);
>     puts ("file created");
>   
> diff --git a/libio/bug-ungetc.c b/libio/bug-ungetc.c
> index 51940b68f5..4ea2d14ed6 100644
> --- a/libio/bug-ungetc.c
> +++ b/libio/bug-ungetc.c
> @@ -2,6 +2,8 @@
>   
>   #include <stdio.h>
>   
> +#include <support/xunistd.h>
> +
>   static void do_prepare (void);
>   #define PREPARE(argc, argv) do_prepare ()
>   static int do_test (void);
> @@ -20,7 +22,7 @@ do_prepare (void)
>         printf ("cannot create temporary file: %m\n");
>         exit (1);
>       }
> -  write (fd, pattern, sizeof (pattern));
> +  xwrite (fd, pattern, sizeof (pattern));
>     close (fd);
>   }
>   
> diff --git a/libio/bug-ungetc3.c b/libio/bug-ungetc3.c
> index 0c83c1161e..6100d7a936 100644
> --- a/libio/bug-ungetc3.c
> +++ b/libio/bug-ungetc3.c
> @@ -2,6 +2,8 @@
>   
>   #include <stdio.h>
>   
> +#include <support/xunistd.h>
> +
>   static void do_prepare (void);
>   #define PREPARE(argc, argv) do_prepare ()
>   static int do_test (void);
> @@ -20,7 +22,7 @@ do_prepare (void)
>         printf ("cannot create temporary file: %m\n");
>         exit (1);
>       }
> -  write (fd, pattern, sizeof (pattern));
> +  xwrite (fd, pattern, sizeof (pattern));
>     close (fd);
>   }
>   
> diff --git a/libio/bug-ungetc4.c b/libio/bug-ungetc4.c
> index 0bd02a570d..8a05def686 100644
> --- a/libio/bug-ungetc4.c
> +++ b/libio/bug-ungetc4.c
> @@ -18,6 +18,8 @@
>   
>   #include <stdio.h>
>   
> +#include <support/xunistd.h>
> +
>   static void do_prepare (void);
>   #define PREPARE(argc, argv) do_prepare ()
>   static int do_test (void);
> @@ -36,7 +38,7 @@ do_prepare (void)
>         printf ("cannot create temporary file: %m\n");
>         exit (1);
>       }
> -  write (fd, pattern, sizeof (pattern) - 1);
> +  xwrite (fd, pattern, sizeof (pattern) - 1);
>     close (fd);
>   }
>   
> diff --git a/libio/bug-wfflush.c b/libio/bug-wfflush.c
> index a8fd61e997..d1b9d8e9de 100644
> --- a/libio/bug-wfflush.c
> +++ b/libio/bug-wfflush.c
> @@ -3,6 +3,8 @@
>   #include <stdio.h>
>   #include <wchar.h>
>   
> +#include <support/xunistd.h>
> +
>   static void do_prepare (void);
>   #define PREPARE(argc, argv) do_prepare ()
>   static int do_test (void);
> @@ -20,7 +22,7 @@ do_prepare (void)
>         printf ("cannot create temporary file: %m\n");
>         exit (1);
>       }
> -  write (fd, "1!", 2);
> +  xwrite (fd, "1!", 2);
>     close (fd);
>   }
>   
> diff --git a/libio/bug-wsetpos.c b/libio/bug-wsetpos.c
> index ccb22a4b62..0fc373ba49 100644
> --- a/libio/bug-wsetpos.c
> +++ b/libio/bug-wsetpos.c
> @@ -4,6 +4,8 @@
>   #include <stdio.h>
>   #include <wchar.h>
>   
> +#include <support/xunistd.h>
> +
>   static void do_prepare (void);
>   #define PREPARE(argc, argv) do_prepare ()
>   static int do_test (void);
> @@ -22,7 +24,7 @@ do_prepare (void)
>         printf ("cannot create temporary file: %m\n");
>         exit (1);
>       }
> -  write (fd, pattern, sizeof (pattern));
> +  xwrite (fd, pattern, sizeof (pattern));
>     close (fd);
>   }
>   
> diff --git a/nptl/tst-stackguard1.c b/nptl/tst-stackguard1.c
> index b9cf6844de..4ac57157e9 100644
> --- a/nptl/tst-stackguard1.c
> +++ b/nptl/tst-stackguard1.c
> @@ -27,6 +27,8 @@
>   #include <tls.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
> +
>   static const char *command;
>   static bool child;
>   static uintptr_t stack_chk_guard_copy;
> @@ -96,7 +98,7 @@ do_test (void)
>   	else if (ret != NULL)
>   	  return 1;
>   
> -      write (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy));
> +      xwrite (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy));
>         return 0;
>       }
>   
> diff --git a/nptl/tst-tls3.c b/nptl/tst-tls3.c
> index b1a40c624a..33d94c8cc5 100644
> --- a/nptl/tst-tls3.c
> +++ b/nptl/tst-tls3.c
> @@ -26,6 +26,8 @@
>   #include <unistd.h>
>   #include <pthreaddef.h>
>   
> +#include <support/xunistd.h>
> +
>   #define THE_SIG SIGUSR1
>   
>   /* The stack size can be overriden.  With a sufficiently large stack
> diff --git a/nptl/tst-tls3mod.c b/nptl/tst-tls3mod.c
> index c6e8910b1e..345a48e1c7 100644
> --- a/nptl/tst-tls3mod.c
> +++ b/nptl/tst-tls3mod.c
> @@ -25,6 +25,7 @@
>   #include <pthreaddef.h>
>   #include <descr.h>
>   
> +#include <support/xunistd.h>
>   
>   extern pthread_barrier_t b;
>   
> @@ -43,7 +44,7 @@ handler (int sig)
>   {
>     if (sig != THE_SIG)
>       {
> -      write (STDOUT_FILENO, "wrong signal\n", 13);
> +      xwrite (STDOUT_FILENO, "wrong signal\n", 13);
>         _exit (1);
>       }
>   
> @@ -51,7 +52,7 @@ handler (int sig)
>   
>     if (sem_post (&s) != 0)
>       {
> -      write (STDOUT_FILENO, "sem_post failed\n", 16);
> +      xwrite (STDOUT_FILENO, "sem_post failed\n", 16);
>         _exit (1);
>       }
>   }
> diff --git a/rt/tst-cpuclock2.c b/rt/tst-cpuclock2.c
> index e4584d8791..8afd34ed9c 100644
> --- a/rt/tst-cpuclock2.c
> +++ b/rt/tst-cpuclock2.c
> @@ -37,6 +37,8 @@ do_test ()
>   #include <errno.h>
>   #include <pthread.h>
>   
> +#include <support/xunistd.h>
> +
>   static pthread_barrier_t barrier;
>   
>   /* This function is intended to rack up both user and system time.  */
> @@ -55,7 +57,7 @@ chew_cpu (void *arg)
>         for (int i = 0; i < 100; ++i)
>   	for (size_t j = 0; j < sizeof buf; ++j)
>   	  buf[j] = 0xbb;
> -      write (nullfd, (char *) buf, sizeof buf);
> +      xwrite (nullfd, (char *) buf, sizeof buf);
>         close (nullfd);
>       }
>   
> diff --git a/rt/tst-cputimer1.c b/rt/tst-cputimer1.c
> index 8f5dd76cf2..18d8b195a2 100644
> --- a/rt/tst-cputimer1.c
> +++ b/rt/tst-cputimer1.c
> @@ -11,6 +11,8 @@
>   #include <time.h>
>   #include <pthread.h>
>   
> +#include <support/xunistd.h>
> +
>   #define TEST_CLOCK CLOCK_PROCESS_CPUTIME_ID
>   #define TEST_CLOCK_MISSING(clock) \
>     (setup_test () ? "process CPU clock timer support" : NULL)
> @@ -29,7 +31,7 @@ chew_cpu (void *arg)
>         for (int i = 0; i < 100; ++i)
>   	for (size_t j = 0; j < sizeof buf; ++j)
>   	  buf[j] = 0xbb;
> -      write (nullfd, (char *) buf, sizeof buf);
> +      xwrite (nullfd, (char *) buf, sizeof buf);
>         close (nullfd);
>       }
>   
> diff --git a/rt/tst-cputimer2.c b/rt/tst-cputimer2.c
> index 397d7998c0..a5700d4bac 100644
> --- a/rt/tst-cputimer2.c
> +++ b/rt/tst-cputimer2.c
> @@ -12,6 +12,8 @@
>   #include <time.h>
>   #include <pthread.h>
>   
> +#include <support/xunistd.h>
> +
>   static clockid_t worker_thread_clock;
>   
>   #define TEST_CLOCK worker_thread_clock
> @@ -32,7 +34,7 @@ chew_cpu (void *arg)
>         for (int i = 0; i < 100; ++i)
>   	for (size_t j = 0; j < sizeof buf; ++j)
>   	  buf[j] = 0xbb;
> -      write (nullfd, (char *) buf, sizeof buf);
> +      xwrite (nullfd, (char *) buf, sizeof buf);
>         close (nullfd);
>       }
>   
> diff --git a/rt/tst-cputimer3.c b/rt/tst-cputimer3.c
> index 056766a377..786de93a02 100644
> --- a/rt/tst-cputimer3.c
> +++ b/rt/tst-cputimer3.c
> @@ -13,6 +13,8 @@
>   #include <signal.h>
>   #include <sys/wait.h>
>   
> +#include <support/xunistd.h>
> +
>   static clockid_t child_clock;
>   
>   #define TEST_CLOCK child_clock
> @@ -33,7 +35,7 @@ chew_cpu (void)
>         for (int i = 0; i < 100; ++i)
>   	for (size_t j = 0; j < sizeof buf; ++j)
>   	  buf[j] = 0xbb;
> -      write (nullfd, (char *) buf, sizeof buf);
> +      xwrite (nullfd, (char *) buf, sizeof buf);
>         close (nullfd);
>         if (getppid () == 1)
>   	_exit (2);
> diff --git a/support/test-container.c b/support/test-container.c
> index e68f16eecf..d4ca41fe7c 100644
> --- a/support/test-container.c
> +++ b/support/test-container.c
> @@ -1186,7 +1186,7 @@ main (int argc, char **argv)
>         int status;
>   
>         /* Send the child's "outside" pid to it.  */
> -      write (pipes[1], &child, sizeof(child));
> +      xwrite (pipes[1], &child, sizeof(child));
>         close (pipes[0]);
>         close (pipes[1]);
>   
> @@ -1255,7 +1255,7 @@ main (int argc, char **argv)
>   
>         sprintf (tmp, "%lld %lld 1\n",
>   	       (long long) (be_su ? 0 : original_uid), (long long) original_uid);
> -      write (UMAP, tmp, strlen (tmp));
> +      xwrite (UMAP, tmp, strlen (tmp));
>         xclose (UMAP);
>   
>         /* We must disable setgroups () before we can map our groups, else we
> @@ -1264,7 +1264,7 @@ main (int argc, char **argv)
>         if (GMAP >= 0)
>   	{
>   	  /* We support kernels old enough to not have this.  */
> -	  write (GMAP, "deny\n", 5);
> +	  xwrite (GMAP, "deny\n", 5);
>   	  xclose (GMAP);
>   	}
>   
> @@ -1276,7 +1276,7 @@ main (int argc, char **argv)
>   
>         sprintf (tmp, "%lld %lld 1\n",
>   	       (long long) (be_su ? 0 : original_gid), (long long) original_gid);
> -      write (GMAP, tmp, strlen (tmp));
> +      xwrite (GMAP, tmp, strlen (tmp));
>         xclose (GMAP);
>       }
>   
> diff --git a/sysdeps/pthread/tst-cond18.c b/sysdeps/pthread/tst-cond18.c
> index edac4fa4ff..ffae356c04 100644
> --- a/sysdeps/pthread/tst-cond18.c
> +++ b/sysdeps/pthread/tst-cond18.c
> @@ -24,6 +24,8 @@
>   #include <stdio.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
> +
>   pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
>   pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
>   bool exiting;
> @@ -40,7 +42,7 @@ tf (void *id)
>         while (!exiting)
>   	{
>   	  if ((spins++ % 1000) == 0)
> -	    write (fd, ".", 1);
> +	    xwrite (fd, ".", 1);
>   	  pthread_mutex_unlock (&lock);
>   
>   	  pthread_mutex_lock (&lock);
> diff --git a/sysdeps/pthread/tst-flock1.c b/sysdeps/pthread/tst-flock1.c
> index 7eef9070ab..9de148afd3 100644
> --- a/sysdeps/pthread/tst-flock1.c
> +++ b/sysdeps/pthread/tst-flock1.c
> @@ -21,6 +21,7 @@
>   #include <unistd.h>
>   #include <sys/file.h>
>   
> +#include <support/xunistd.h>
>   
>   static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
>   
> @@ -56,7 +57,7 @@ do_test (void)
>   
>     unlink (tmp);
>   
> -  write (fd, "foobar xyzzy", 12);
> +  xwrite (fd, "foobar xyzzy", 12);
>   
>     if (flock (fd, LOCK_EX | LOCK_NB) != 0)
>       {
> diff --git a/sysdeps/pthread/tst-flock2.c b/sysdeps/pthread/tst-flock2.c
> index 8762e93b52..952b79e5db 100644
> --- a/sysdeps/pthread/tst-flock2.c
> +++ b/sysdeps/pthread/tst-flock2.c
> @@ -24,6 +24,7 @@
>   #include <sys/mman.h>
>   #include <sys/wait.h>
>   
> +#include <support/xunistd.h>
>   
>   static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
>   static pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;
> @@ -70,7 +71,7 @@ do_test (void)
>   
>     int i;
>     for (i = 0; i < 20; ++i)
> -    write (fd, "foobar xyzzy", 12);
> +    xwrite (fd, "foobar xyzzy", 12);
>   
>     pthread_barrier_t *b;
>     b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
> diff --git a/sysdeps/pthread/tst-key1.c b/sysdeps/pthread/tst-key1.c
> index 933edafef8..60245c4e47 100644
> --- a/sysdeps/pthread/tst-key1.c
> +++ b/sysdeps/pthread/tst-key1.c
> @@ -21,6 +21,7 @@
>   #include <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
>   
>   static int do_test (void);
>   
> @@ -51,7 +52,7 @@ do_test (void)
>   
>   	if (pthread_setspecific (keys[i], (const void *) (i + 100l)) != 0)
>   	  {
> -	    write (2, "setspecific failed\n", 19);
> +	    xwrite (2, "setspecific failed\n", 19);
>   	    _exit (1);
>   	  }
>         }
> @@ -60,13 +61,13 @@ do_test (void)
>       {
>         if (pthread_getspecific (keys[i]) != (void *) (i + 100l))
>   	{
> -	  write (2, "getspecific failed\n", 19);
> +	  xwrite (2, "getspecific failed\n", 19);
>   	  _exit (1);
>   	}
>   
>         if (pthread_key_delete (keys[i]) != 0)
>   	{
> -	  write (2, "key_delete failed\n", 18);
> +	  xwrite (2, "key_delete failed\n", 18);
>   	  _exit (1);
>   	}
>       }
> @@ -74,13 +75,13 @@ do_test (void)
>     /* Now it must be once again possible to allocate keys.  */
>     if (pthread_key_create (&keys[0], NULL) != 0)
>       {
> -      write (2, "2nd key_create failed\n", 22);
> +      xwrite (2, "2nd key_create failed\n", 22);
>         _exit (1);
>       }
>   
>     if (pthread_key_delete (keys[0]) != 0)
>       {
> -      write (2, "2nd key_delete failed\n", 22);
> +      xwrite (2, "2nd key_delete failed\n", 22);
>         _exit (1);
>       }
>   
> diff --git a/sysdeps/pthread/tst-signal1.c b/sysdeps/pthread/tst-signal1.c
> index d1073e8459..d1082027ca 100644
> --- a/sysdeps/pthread/tst-signal1.c
> +++ b/sysdeps/pthread/tst-signal1.c
> @@ -25,6 +25,7 @@
>   #include <sys/mman.h>
>   #include <sys/wait.h>
>   
> +#include <support/xunistd.h>
>   
>   static sigset_t ss;
>   static pthread_barrier_t *b;
> @@ -105,7 +106,7 @@ do_test (void)
>   
>     int i;
>     for (i = 0; i < 20; ++i)
> -    write (fd, "foobar xyzzy", 12);
> +    xwrite (fd, "foobar xyzzy", 12);
>   
>     b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
>   	    MAP_SHARED, fd, 0);
> diff --git a/sysdeps/pthread/tst-signal2.c b/sysdeps/pthread/tst-signal2.c
> index dfe7d9f64a..15b7747877 100644
> --- a/sysdeps/pthread/tst-signal2.c
> +++ b/sysdeps/pthread/tst-signal2.c
> @@ -25,6 +25,7 @@
>   #include <sys/wait.h>
>   #include <string.h>
>   
> +#include <support/xunistd.h>
>   
>   static sigset_t ss;
>   static pthread_barrier_t *b;
> @@ -111,7 +112,7 @@ do_test (void)
>   
>     int i;
>     for (i = 0; i < 20; ++i)
> -    write (fd, "foobar xyzzy", 12);
> +    xwrite (fd, "foobar xyzzy", 12);
>   
>     b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
>   	    MAP_SHARED, fd, 0);
> diff --git a/sysdeps/pthread/tst-timer.c b/sysdeps/pthread/tst-timer.c
> index 47472ab8e1..4cfe0b67dc 100644
> --- a/sysdeps/pthread/tst-timer.c
> +++ b/sysdeps/pthread/tst-timer.c
> @@ -24,6 +24,7 @@
>   #include <stdlib.h>
>   #include <stdint.h>
>   
> +#include <support/xunistd.h>
>   
>   static void
>   notify_func1 (union sigval sigval)
> @@ -44,7 +45,7 @@ signal_func (int sig)
>   {
>     static const char text[] = "signal_func\n";
>     signal (sig, signal_func);
> -  write (STDOUT_FILENO, text, sizeof text - 1);
> +  xwrite (STDOUT_FILENO, text, sizeof text - 1);
>   }
>   
>   static void
> diff --git a/time/tst-cpuclock1.c b/time/tst-cpuclock1.c
> index 6f2e70a58a..6a793e06df 100644
> --- a/time/tst-cpuclock1.c
> +++ b/time/tst-cpuclock1.c
> @@ -27,6 +27,8 @@
>   #include <stdint.h>
>   #include <sys/wait.h>
>   
> +#include <support/xunistd.h>
> +
>   /* This function is intended to rack up both user and system time.  */
>   static void
>   chew_cpu (void)
> @@ -41,7 +43,7 @@ chew_cpu (void)
>         for (int i = 0; i < 100; ++i)
>   	for (size_t j = 0; j < sizeof buf; ++j)
>   	  buf[j] = 0xbb;
> -      write (nullfd, (char *) buf, sizeof buf);
> +      xwrite (nullfd, (char *) buf, sizeof buf);
>         close (nullfd);
>         if (getppid () == 1)
>   	_exit (2);

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 10/15] tests: replace system by xsystem
  2023-04-28 12:21   ` [PATCH v4 10/15] tests: replace system by xsystem Frédéric Bérat
@ 2023-05-25  1:22     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  1:22 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-04-28 08:21, Frédéric Bérat wrote:
> With fortification enabled, system calls return result needs to be checked,
> has it gets the __wur macro enabled.
> ---
>   elf/tst-stackguard1.c     |  2 +-
>   libio/bug-mmap-fflush.c   |  7 +++++--
>   nptl/tst-cancel7.c        |  3 ++-
>   nptl/tst-stackguard1.c    |  2 +-
>   nss/tst-nss-db-endpwent.c |  3 ++-
>   support/Makefile          |  1 +
>   support/xstdlib.h         | 31 +++++++++++++++++++++++++++++++
>   support/xsystem.c         | 29 +++++++++++++++++++++++++++++
>   8 files changed, 72 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..7dbde6568c 100644
> --- a/elf/tst-stackguard1.c
> +++ b/elf/tst-stackguard1.c
> @@ -108,7 +108,7 @@ do_test (void)
>   	  dup2 (fds[1], 2);
>   	  close (fds[1]);
>   
> -	  system (command);
> +	  if (system (command)) {/* We aren't interested on the result */}
>   	  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 <stdlib.h>
>   #include <string.h>
>   
> +#include <support/xstdlib.h>
>   
>   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 <support/support.h>
>   #include <support/temp_file.h>
>   #include <support/xstdio.h>
> +#include <support/xstdlib.h>
>   #include <support/xunistd.h>
>   #include <support/xthread.h>
>   
> @@ -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..9ec9e8f982 100644
> --- a/nptl/tst-stackguard1.c
> +++ b/nptl/tst-stackguard1.c
> @@ -140,7 +140,7 @@ do_test (void)
>   	  dup2 (fds[1], 2);
>   	  close (fds[1]);
>   
> -	  system (command);
> +	  if (system (command)) {/* We aren't interested in the result */}

Why not?

>   	  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 <support/support.h>
>   #include <support/check.h>
> +#include <support/xstdlib.h>
>   
>   /* 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 a4ac86544c..4c8eb3dedd 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..58642e3542
> --- /dev/null
> +++ b/support/xstdlib.h
> @@ -0,0 +1,31 @@
> +/* Error-checking wrappers for stdlib functions.
> +   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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#ifndef SUPPORT_XSTDLIB_H
> +#define SUPPORT_XSTDLIB_H
> +
> +#include <stdlib.h>
> +#include <sys/cdefs.h>
> +
> +__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..8ceaad1531
> --- /dev/null
> +++ b/support/xsystem.c
> @@ -0,0 +1,29 @@
> +/* Error-checking replacement for "system".
> +   Copyright (C) 2018-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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/support.h>
> +#include <support/check.h>
> +
> +#include <support/xstdlib.h>
> +
> +void
> +xsystem (const char *cmd)
> +{
> +  if (system (cmd) < 0)
> +    FAIL_EXIT1 ("system (\"%s\")", cmd);

This would succeed if system exits with status 127, when it's unable to 
spawn a child shell.  It will also not behave correctly with a NULL cmd, 
where I reckon we'd want to fail if no shell is available, i.e. return 
value of 0.

> +}

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 12/15] tests: replace ftruncate by xftruncate
  2023-04-28 12:21   ` [PATCH v4 12/15] tests: replace ftruncate by xftruncate Frédéric Bérat
@ 2023-05-25  1:25     ` Siddhesh Poyarekar
  2023-06-01 16:42       ` Siddhesh Poyarekar
  0 siblings, 1 reply; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  1:25 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-04-28 08:21, Frédéric Bérat wrote:
> With fortification enabled, ftruncate calls return result needs to be
> checked, has it gets the __wur macro enabled.
> ---
>   io/tst-copy_file_range.c  | 2 +-
>   posix/tst-getopt-cancel.c | 3 ++-
>   stdio-common/tst-perror.c | 3 ++-
>   3 files changed, 5 insertions(+), 3 deletions(-)
> 
> diff --git a/io/tst-copy_file_range.c b/io/tst-copy_file_range.c
> index d1f3aaa5a9..bd64e9c42b 100644
> --- a/io/tst-copy_file_range.c
> +++ b/io/tst-copy_file_range.c
> @@ -166,7 +166,7 @@ short_copy (void)
>             inoff = 3;
>             xlseek (infd, shift, SEEK_SET);
>           }
> -      ftruncate (outfd, 0);
> +      xftruncate (outfd, 0);
>         xlseek (outfd, 0, SEEK_SET);
>         outoff = 0;
>   
> diff --git a/posix/tst-getopt-cancel.c b/posix/tst-getopt-cancel.c
> index 7167d1a914..6f49391690 100644
> --- a/posix/tst-getopt-cancel.c
> +++ b/posix/tst-getopt-cancel.c
> @@ -33,6 +33,7 @@
>   #include <support/support.h>
>   #include <support/temp_file.h>
>   #include <support/xthread.h>
> +#include <support/xunistd.h>
>   
>   static bool
>   check_stderr (bool expect_errmsg, FILE *stderr_trapped)
> @@ -48,7 +49,7 @@ check_stderr (bool expect_errmsg, FILE *stderr_trapped)
>         fputs (lineptr, stdout);
>       }
>     rewind (stderr_trapped);
> -  ftruncate (fileno (stderr_trapped), 0);
> +  xftruncate (fileno (stderr_trapped), 0);
>     return got_errmsg == expect_errmsg;
>   }
>   
> diff --git a/stdio-common/tst-perror.c b/stdio-common/tst-perror.c
> index 57835e0c59..b4ab583462 100644
> --- a/stdio-common/tst-perror.c
> +++ b/stdio-common/tst-perror.c

This one needs to be ported to use support/test-driver.  The patch is OK 
otherwise.

> @@ -9,6 +9,7 @@
>   #include <unistd.h>
>   #include <wchar.h>
>   
> +#include <support/xunistd.h>
>   
>   #define MB_EXP \
>     "null mode test 1: Invalid or incomplete multibyte or wide character\n" \
> @@ -94,7 +95,7 @@ do_test (void)
>       puts ("multibyte test succeeded");
>   
>     lseek (fd, 0, SEEK_SET);
> -  ftruncate (fd, 0);
> +  xftruncate (fd, 0);
>   
>     if (dup2 (fd, 2) == -1)
>       {

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 14/15] tests: Replace various function calls with their x variant
  2023-04-28 12:21   ` [PATCH v4 14/15] tests: Replace various function calls with their x variant Frédéric Bérat
@ 2023-05-25  1:29     ` Siddhesh Poyarekar
  2023-05-25 16:10       ` Frederic Berat
  0 siblings, 1 reply; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  1:29 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-04-28 08:21, Frédéric Bérat wrote:
> With fortification enabled, few function calls return result need to be
> checked, has they get the __wur macro enabled.
> ---
>   misc/tst-error1.c    | 2 +-
>   nss/tst-reload2.c    | 6 +++++-
>   posix/tst-chmod.c    | 9 +++++++--
>   posix/wordexp-test.c | 6 +++++-
>   stdlib/test-canon.c  | 7 ++++++-
>   5 files changed, 24 insertions(+), 6 deletions(-)
> 
> diff --git a/misc/tst-error1.c b/misc/tst-error1.c
> index 9c4a62fbd0..65e3fd0c0e 100644
> --- a/misc/tst-error1.c
> +++ b/misc/tst-error1.c
> @@ -9,7 +9,7 @@ static int
>   do_test (int argc, char *argv[])
>   {
>     mtrace ();
> -  (void) freopen (argc == 1 ? "/dev/stdout" : argv[1], "a", stderr);
> +  if (freopen (argc == 1 ? "/dev/stdout" : argv[1], "a", stderr)) {}

There is an xfreopen in support/

>     /* 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..b25e5e3528 100644
> --- a/nss/tst-reload2.c
> +++ b/nss/tst-reload2.c
> @@ -121,7 +121,11 @@ do_test (void)
>     /* Change the root dir.  */
>   
>     TEST_VERIFY (chroot ("/subdir") == 0);
> -  chdir ("/");
> +  if (chdir ("/") < 0)

There is an xchdir in support/

> +    {
> +      printf("Failed to change directory: %m");
> +      return 1;
> +    }
>   
>     /* Check we're NOT using the "inner" nsswitch.conf.  */
>   
> diff --git a/posix/tst-chmod.c b/posix/tst-chmod.c
> index b98a05a265..bec2d2b8eb 100644
> --- a/posix/tst-chmod.c
> +++ b/posix/tst-chmod.c
> @@ -229,7 +229,12 @@ do_test (int argc, char *argv[])
>     close (fd);
>   
>     snprintf (buf, buflen, "%s/..", testdir);
> -  chdir (buf);
> +  if (chdir (buf))
> +    {
> +      printf ("cannot change directory: %m\n");
> +      result = 1;
> +      goto fail;
> +    }
>     /* We are now in the directory above the one we create the test
>        directory in.  */
>   
> @@ -349,7 +354,7 @@ do_test (int argc, char *argv[])
>       }
>   
>    fail:
> -  chdir (startdir);
> +  if (chdir (startdir)) {}
>   
>     /* Remove all the files.  */
>     chmod (testdir, 0700);
> diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c
> index bae27d6cee..87d537c931 100644
> --- a/posix/wordexp-test.c
> +++ b/posix/wordexp-test.c
> @@ -332,7 +332,11 @@ do_test (int argc, char *argv[])
>     if (cwd == NULL)
>       cwd = "..";
>   
> -  chdir (cwd);
> +  if (chdir (cwd) < 0)
> +    {
> +      printf ("failed to change dir: %m");
> +      return 1;
> +    }
>     rmdir (tmpdir);
>   
>     return 0;
> diff --git a/stdlib/test-canon.c b/stdlib/test-canon.c
> index 4edee73dd8..5a2e7e1e6e 100644
> --- a/stdlib/test-canon.c
> +++ b/stdlib/test-canon.c
> @@ -154,7 +154,12 @@ do_test (int argc, char ** argv)
>       }
>   
>     for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
> -    symlink (symlinks[i].value, symlinks[i].name);
> +    if (symlink (symlinks[i].value, symlinks[i].name))

There is an xsymlink in support/

> +      {
> +        printf ("%s: Unable to create symlink for %s -> %s\n",
> +		argv[0], symlinks[i].name, symlinks[i].value);
> +        ++errors;
> +      }
>   
>     int has_dir = mkdir ("doesExist", 0777) == 0;
>   

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 15/15] tests: fix warn unused results
  2023-04-28 12:21   ` [PATCH v4 15/15] tests: fix warn unused results Frédéric Bérat
@ 2023-05-25  1:35     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  1:35 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha

A very tiny nit in this one (unnecessary paren), but OK otherwise.

On 2023-04-28 08:21, Frédéric Bérat wrote:
> With fortification enabled, few function calls return result need to be
> checked, has they get the __wur macro enabled.
> ---
>   crypt/cert.c                   |  6 +++++-
>   misc/tst-efgcvt-template.c     |  4 ++--
>   posix/tst-nice.c               |  3 +--
>   posix/wordexp-test.c           |  6 +++++-
>   stdio-common/bug19.c           |  9 +++++++--
>   stdio-common/bug6.c            |  8 ++++----
>   stdio-common/tstscanf.c        | 14 ++++++++++++--
>   stdlib/test-canon.c            | 18 +++++++++++++++---
>   support/test-container.c       |  4 ++--
>   sysdeps/pthread/tst-cancel16.c |  6 +++++-
>   sysdeps/pthread/tst-cancel4.c  |  6 ++++--
>   11 files changed, 62 insertions(+), 22 deletions(-)
> 
> diff --git a/crypt/cert.c b/crypt/cert.c
> index 32c4386caf..5b4277f76d 100644
> --- a/crypt/cert.c
> +++ b/crypt/cert.c
> @@ -99,7 +99,11 @@ get8 (char *cp)
>   	int i,j,t;
>   
>   	for(i=0;i<8;i++){
> -		scanf("%2x",&t);
> +		if (scanf("%2x",&t) < 1)
> +		  {
> +		    if(ferror(stdin))
> +		      totfails++;
> +		  }
>   		if(feof(stdin))
>   		  good_bye();
>   		for(j=0; j<8 ; j++) {
> diff --git a/misc/tst-efgcvt-template.c b/misc/tst-efgcvt-template.c
> index b924659a3d..87e3ebe4fa 100644
> --- a/misc/tst-efgcvt-template.c
> +++ b/misc/tst-efgcvt-template.c
> @@ -200,8 +200,8 @@ special (void)
>       output_error (NAME (ECVT), INFINITY, 10, "inf", 0, 0, p, decpt, sign);
>   
>     /* Simply make sure these calls with large NDIGITs don't crash.  */
> -  (void) ECVT (123.456, 10000, &decpt, &sign);
> -  (void) FCVT (123.456, 10000, &decpt, &sign);
> +  p = ECVT (123.456, 10000, &decpt, &sign);
> +  p = FCVT (123.456, 10000, &decpt, &sign);
>   
>     /* Some tests for the reentrant functions.  */
>     /* Use a too small buffer.  */
> diff --git a/posix/tst-nice.c b/posix/tst-nice.c
> index fe9888b3f6..59cf953e27 100644
> --- a/posix/tst-nice.c
> +++ b/posix/tst-nice.c
> @@ -58,8 +58,7 @@ do_test (void)
>   
>     /* BZ #18086. Make sure we don't reset errno.  */
>     errno = EBADF;
> -  nice (0);
> -  if (errno != EBADF)
> +  if (nice (0) == -1 || errno != EBADF)
>       {
>         printf ("FAIL: errno = %i, but wanted EBADF (%i)\n", errno, EBADF);
>         return 1;
> diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c
> index 87d537c931..057c89dd3c 100644
> --- a/posix/wordexp-test.c
> +++ b/posix/wordexp-test.c
> @@ -253,7 +253,11 @@ do_test (int argc, char *argv[])
>     cwd = getcwd (NULL, 0);
>   
>     /* Set up arena for pathname expansion */
> -  tmpnam (tmpdir);
> +  if (!tmpnam (tmpdir))
> +    {
> +      printf ("Failed to create a temporary directory with a unique name: %m");
> +      return 1;
> +    }
>     xmkdir (tmpdir, S_IRWXU);
>     TEST_VERIFY_EXIT (chdir (tmpdir) == 0);
>   
> diff --git a/stdio-common/bug19.c b/stdio-common/bug19.c
> index e083304bda..9a3deac3fc 100644
> --- a/stdio-common/bug19.c
> +++ b/stdio-common/bug19.c
> @@ -29,12 +29,17 @@ do_test (void)
>     printf("checking sscanf\n");
>   
>     int i, j, n;
> +  int result = 0;
>   
>     i = j = n = 0;
> -  FSCANF (fp, L(" %i - %i %n"), &i, &j, &n);
> +  if (FSCANF (fp, L(" %i - %i %n"), &i, &j, &n) < 2)
> +    {
> +      printf ("FSCANF couldn't read all parameters %d\n", errno);
> +      result = 1;
> +    }
> +
>     printf ("found %i-%i (length=%i)\n", i, j, n);
>   
> -  int result = 0;
>     if (i != 7)
>       {
>         printf ("i is %d, expected 7\n", i);
> diff --git a/stdio-common/bug6.c b/stdio-common/bug6.c
> index 0db63a3b44..50098bf3f2 100644
> --- a/stdio-common/bug6.c
> +++ b/stdio-common/bug6.c
> @@ -7,16 +7,16 @@ main (void)
>     int i;
>     int lost = 0;
>   
> -  scanf ("%2s", buf);
> +  lost = (scanf ("%2s", buf) < 0);
>     lost |= (buf[0] != 'X' || buf[1] != 'Y' || buf[2] != '\0');
>     if (lost)
>       puts ("test of %2s failed.");
> -  scanf (" ");
> -  scanf ("%d", &i);
> +  lost |= (scanf (" ") < 0);
> +  lost |= (scanf ("%d", &i) < 0);
>     lost |= (i != 1234);
>     if (lost)
>       puts ("test of %d failed.");
> -  scanf ("%c", buf);
> +  lost |= (scanf ("%c", buf) < 0);
>     lost |= (buf[0] != 'L');
>     if (lost)
>       puts ("test of %c failed.\n");
> diff --git a/stdio-common/tstscanf.c b/stdio-common/tstscanf.c
> index 3a4ebf7524..7e92df4720 100644
> --- a/stdio-common/tstscanf.c
> +++ b/stdio-common/tstscanf.c
> @@ -120,7 +120,12 @@ main (int argc, char **argv)
>       int i;
>       float x;
>       char name[50];
> -    (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
> +    if (fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name) < 3)
> +      {
> +	fputs ("test failed!\n", stdout);
> +	result = 1;
> +      }
> +
>       fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
>       if (i != 56 || x != 789.0F || strcmp (name, "56"))
>         {
> @@ -164,7 +169,12 @@ main (int argc, char **argv)
>   	quant = 0.0;
>   	units[0] = item[0] = '\0';
>   	count = fscanf (in, "%f%20s of %20s", &quant, units, item);
> -	(void) fscanf (in, "%*[^\n]");
> +	if (fscanf (in, "%*[^\n]") < 0 && ferror (in))
> +	  {
> +	    fputs ("test failed!\n", stdout);
> +	    result = 1;
> +	  }
> +
>   	fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
>   		 count, quant, item, units);
>   	if (count != ok[rounds-1].count || quant != ok[rounds-1].quant
> diff --git a/stdlib/test-canon.c b/stdlib/test-canon.c
> index 5a2e7e1e6e..61fe893609 100644
> --- a/stdlib/test-canon.c
> +++ b/stdlib/test-canon.c
> @@ -123,8 +123,15 @@ do_test (int argc, char ** argv)
>     int i, errors = 0;
>     char buf[PATH_MAX];
>   
> -  getcwd (cwd, sizeof (buf));
> -  cwd_len = strlen (cwd);
> +  if (getcwd (cwd, sizeof (buf)))
> +    {

Unnecessary paren.

> +      cwd_len = strlen (cwd);
> +    }
> +  else
> +    {
> +      printf ("%s: current working directory couldn't be retrieved\n", argv[0]);
> +      ++errors;
> +    }
>   
>     errno = 0;
>     if (realpath (NULL, buf) != NULL || errno != EINVAL)
> @@ -210,7 +217,12 @@ do_test (int argc, char ** argv)
>         free (result2);
>       }
>   
> -  getcwd (buf, sizeof (buf));
> +  if (!getcwd (buf, sizeof (buf)))
> +    {
> +      printf ("%s: current working directory couldn't be retrieved\n", argv[0]);
> +      ++errors;
> +    }
> +
>     if (strcmp (buf, cwd))
>       {
>         printf ("%s: current working directory changed from %s to %s\n",
> diff --git a/support/test-container.c b/support/test-container.c
> index 37beb778d6..f51afefedb 100644
> --- a/support/test-container.c
> +++ b/support/test-container.c
> @@ -714,8 +714,8 @@ check_for_unshare_hints (int require_pidns)
>           continue;
>   
>         val = -1; /* Sentinel.  */
> -      fscanf (f, "%d", &val);
> -      if (val != files[i].bad_value)
> +      int cnt = fscanf (f, "%d", &val);
> +      if (cnt == 1 && val != files[i].bad_value)
>   	continue;
>   
>         printf ("To enable test-container, please run this as root:\n");
> diff --git a/sysdeps/pthread/tst-cancel16.c b/sysdeps/pthread/tst-cancel16.c
> index 511b9e1e91..d47c7c68cb 100644
> --- a/sysdeps/pthread/tst-cancel16.c
> +++ b/sysdeps/pthread/tst-cancel16.c
> @@ -50,7 +50,11 @@ tf (void *arg)
>     pthread_cleanup_push (cl, NULL);
>   
>     /* This call should never return.  */
> -  (void) lockf (fd, F_LOCK, 0);
> +  if (lockf (fd, F_LOCK, 0))
> +    {
> +      puts ("child thread: lockf failed");
> +      exit (1);
> +    }
>   
>     pthread_cleanup_pop (0);
>   
> diff --git a/sysdeps/pthread/tst-cancel4.c b/sysdeps/pthread/tst-cancel4.c
> index 4f5c89314c..4c9e8670ca 100644
> --- a/sysdeps/pthread/tst-cancel4.c
> +++ b/sysdeps/pthread/tst-cancel4.c
> @@ -1009,7 +1009,8 @@ tf_pread (void *arg)
>     pthread_cleanup_push (cl, NULL);
>   
>     char mem[10];
> -  pread (tempfd, mem, sizeof (mem), 0);
> +  if (pread (tempfd, mem, sizeof (mem), 0) < 0)
> +    FAIL_EXIT1 ("pread failed: %m");
>   
>     pthread_cleanup_pop (0);
>   
> @@ -1038,7 +1039,8 @@ tf_pwrite (void *arg)
>     pthread_cleanup_push (cl, NULL);
>   
>     char mem[10];
> -  pwrite (tempfd, mem, sizeof (mem), 0);
> +  if (pwrite (tempfd, mem, sizeof (mem), 0) <0)
> +    FAIL_EXIT1 ("pwrite failed: %m");
>   
>     pthread_cleanup_pop (0);
>   

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 04/15] nptl_db/thread_dbP.h: fix warn unused result
  2023-05-25  0:51     ` Siddhesh Poyarekar
@ 2023-05-25  1:52       ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  1:52 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha

On 2023-05-24 20:51, Siddhesh Poyarekar wrote:
> 
> 
> On 2023-04-28 08:21, Frédéric Bérat wrote:
>> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
>> glibc.
>> ---
>>   nptl_db/thread_dbP.h | 2 +-
>>   1 file changed, 1 insertion(+), 1 deletion(-)
> 
> LGTM.
> 
> Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

Actually...

>>
>> diff --git a/nptl_db/thread_dbP.h b/nptl_db/thread_dbP.h
>> index 8845722109..4a9333658b 100644
>> --- a/nptl_db/thread_dbP.h
>> +++ b/nptl_db/thread_dbP.h
>> @@ -61,7 +61,7 @@ enum
>>   /* Comment out the following for less verbose output.  */
>>   #ifndef NDEBUG
>> -# define LOG(c) if (__td_debug) write (2, c "\n", strlen (c "\n"))
>> +# define LOG(c) if (__td_debug) assert (write (2, c "\n", strlen (c 
>> "\n")) == strlen (c "\n"))

This crossed 79 chars and needs to be split.

>>   extern int __td_debug attribute_hidden;
>>   #else
>>   # define LOG(c)
> 

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 00/15] Fix warn unused result
  2023-04-28 12:21 ` [PATCH v4 00/15] Fix " Frédéric Bérat
@ 2023-05-25  1:53   ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-05-25  1:53 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha

On 2023-04-28 08:21, Frédéric Bérat wrote:
> Hello,
> 
> Enabling _FORTIFY_SOURCE on Glibc itself implies that some function get the
> __wur macro enabed.
> This leads to build failures when -Werror is set.
> 
> I went for multiple patches for these fixes, a big one that cover the tests,
> and then separate one for the other ones, assuming they may need to get a
> closer look at.
> 
> While most of the changes look trivial, decision I made on how to fix them may
> be open to debate, e.g. the additional checks may lead to failures while they
> weren't earlier, some other have no impact while it may be useful to have one.
> 
> Feedback is therefore highly appreciated.

Thank you for your continued work on this; 3 down, 12 (and a bit more 
;)) to go.

I have pushed 3 of the patches I get R-b for.  Overall for the remaining 
patches, there's one review note that's pertinent; the tests that you've 
added the x* function calls for need to use the support.h system, i.e. 
replace test-skeleton.c with support/test-driver.c and then replace the 
non-zero returns from do_test with the check.h macros.

Thanks,
Sid

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 14/15] tests: Replace various function calls with their x variant
  2023-05-25  1:29     ` Siddhesh Poyarekar
@ 2023-05-25 16:10       ` Frederic Berat
  0 siblings, 0 replies; 109+ messages in thread
From: Frederic Berat @ 2023-05-25 16:10 UTC (permalink / raw)
  To: Siddhesh Poyarekar; +Cc: libc-alpha

It looks like I didn't actually send the changes mentioned in the
title, I may have messed with my patches on this one.

On Thu, May 25, 2023 at 3:29 AM Siddhesh Poyarekar <siddhesh@gotplt.org> wrote:
>
>
>
> On 2023-04-28 08:21, Frédéric Bérat wrote:
> > With fortification enabled, few function calls return result need to be
> > checked, has they get the __wur macro enabled.
> > ---
> >   misc/tst-error1.c    | 2 +-
> >   nss/tst-reload2.c    | 6 +++++-
> >   posix/tst-chmod.c    | 9 +++++++--
> >   posix/wordexp-test.c | 6 +++++-
> >   stdlib/test-canon.c  | 7 ++++++-
> >   5 files changed, 24 insertions(+), 6 deletions(-)
> >
> > diff --git a/misc/tst-error1.c b/misc/tst-error1.c
> > index 9c4a62fbd0..65e3fd0c0e 100644
> > --- a/misc/tst-error1.c
> > +++ b/misc/tst-error1.c
> > @@ -9,7 +9,7 @@ static int
> >   do_test (int argc, char *argv[])
> >   {
> >     mtrace ();
> > -  (void) freopen (argc == 1 ? "/dev/stdout" : argv[1], "a", stderr);
> > +  if (freopen (argc == 1 ? "/dev/stdout" : argv[1], "a", stderr)) {}
>
> There is an xfreopen in support/
>
> >     /* 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..b25e5e3528 100644
> > --- a/nss/tst-reload2.c
> > +++ b/nss/tst-reload2.c
> > @@ -121,7 +121,11 @@ do_test (void)
> >     /* Change the root dir.  */
> >
> >     TEST_VERIFY (chroot ("/subdir") == 0);
> > -  chdir ("/");
> > +  if (chdir ("/") < 0)
>
> There is an xchdir in support/
>
> > +    {
> > +      printf("Failed to change directory: %m");
> > +      return 1;
> > +    }
> >
> >     /* Check we're NOT using the "inner" nsswitch.conf.  */
> >
> > diff --git a/posix/tst-chmod.c b/posix/tst-chmod.c
> > index b98a05a265..bec2d2b8eb 100644
> > --- a/posix/tst-chmod.c
> > +++ b/posix/tst-chmod.c
> > @@ -229,7 +229,12 @@ do_test (int argc, char *argv[])
> >     close (fd);
> >
> >     snprintf (buf, buflen, "%s/..", testdir);
> > -  chdir (buf);
> > +  if (chdir (buf))
> > +    {
> > +      printf ("cannot change directory: %m\n");
> > +      result = 1;
> > +      goto fail;
> > +    }
> >     /* We are now in the directory above the one we create the test
> >        directory in.  */
> >
> > @@ -349,7 +354,7 @@ do_test (int argc, char *argv[])
> >       }
> >
> >    fail:
> > -  chdir (startdir);
> > +  if (chdir (startdir)) {}
> >
> >     /* Remove all the files.  */
> >     chmod (testdir, 0700);
> > diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c
> > index bae27d6cee..87d537c931 100644
> > --- a/posix/wordexp-test.c
> > +++ b/posix/wordexp-test.c
> > @@ -332,7 +332,11 @@ do_test (int argc, char *argv[])
> >     if (cwd == NULL)
> >       cwd = "..";
> >
> > -  chdir (cwd);
> > +  if (chdir (cwd) < 0)
> > +    {
> > +      printf ("failed to change dir: %m");
> > +      return 1;
> > +    }
> >     rmdir (tmpdir);
> >
> >     return 0;
> > diff --git a/stdlib/test-canon.c b/stdlib/test-canon.c
> > index 4edee73dd8..5a2e7e1e6e 100644
> > --- a/stdlib/test-canon.c
> > +++ b/stdlib/test-canon.c
> > @@ -154,7 +154,12 @@ do_test (int argc, char ** argv)
> >       }
> >
> >     for (i = 0; i < (int) (sizeof (symlinks) / sizeof (symlinks[0])); ++i)
> > -    symlink (symlinks[i].value, symlinks[i].name);
> > +    if (symlink (symlinks[i].value, symlinks[i].name))
>
> There is an xsymlink in support/
>
> > +      {
> > +        printf ("%s: Unable to create symlink for %s -> %s\n",
> > +             argv[0], symlinks[i].name, symlinks[i].value);
> > +        ++errors;
> > +      }
> >
> >     int has_dir = mkdir ("doesExist", 0777) == 0;
> >
>


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 07/15] tests: fix warn unused result on asprintf calls
  2023-05-25  1:07     ` Siddhesh Poyarekar
@ 2023-05-31 14:36       ` Frederic Berat
  0 siblings, 0 replies; 109+ messages in thread
From: Frederic Berat @ 2023-05-31 14:36 UTC (permalink / raw)
  To: Siddhesh Poyarekar; +Cc: libc-alpha

On Thu, May 25, 2023 at 3:07 AM Siddhesh Poyarekar <siddhesh@gotplt.org> wrote:
>
> On 2023-04-28 08:21, Frédéric Bérat wrote:
> > When enabling _FORTIFY_SOURCE, some functions now lead to warnings when
> > their result is not checked.
> > ---
> >   argp/argp-test.c           |  8 +++++---
> >   posix/tst-execl2.c         |  8 ++------
> >   posix/tst-execle2.c        |  8 ++------
> >   posix/tst-execlp2.c        | 11 +++--------
> >   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, 28 insertions(+), 53 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 <string.h>
> >   #include <argp.h>
> >
> > +#include <support/support.h>
> > +
> >   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..8f10d4b7f8 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");
> > @@ -59,8 +55,7 @@ do_test (void)
> >         return 1;
> >       }
> >     char *path;
> > -  asprintf (&path, "%s:../libio:../elf", bindir);
> > -  if (path == NULL)
> > +  if (asprintf (&path, "%s:../libio:../elf", bindir) < 0 || path == NULL)
>
> Couldn't this be:
>
>    path = xasprintf (...);
>    TEST_VERIFY (path != NULL);
>
> Also this is an old style test that uses test-skeleton.c, it needs to be
> ported over to using support.h.  That's probably true for other tests
> you're touching too (sorry!)
>

Although I agree the test needs porting, I'd prefer to keep the scope
of the change focused on the wur fix if you don´t mind.
The porting may be done in a separate patchset.

> >       {
> >         puts ("asprintf  failed");
> >         return 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 <stdlib.h>
> >   #include <unistd.h>
> >
> > +#include <support/support.h>
> > +
> >   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 <stdio.h>
> >   #include <string.h>
> >
> > +#include <support/support.h>
> > +
> >   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 <time.h>
> >   #include <sys/stat.h>
> >
> > +#include <support/support.h>
> >
> >   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);
>


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v5 01/12] catgets/gencat.c: fix warn unused result
  2023-04-18 12:11 ` [PATCH 2/8] catgets/gencat.c: fix warn unused result Frédéric Bérat
  2023-04-18 12:36   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 01/15] " Frédéric Bérat
@ 2023-06-01 14:27   ` Frédéric Bérat
  2023-06-01 16:47     ` Siddhesh Poyarekar
  2 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-01 14:27 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
Changes since v4:
  - Mark strings for translation with gettext.
  - Fixed typo in (ret == 0) case

 catgets/gencat.c | 41 ++++++++++++++++++++++++++++++-----------
 1 file changed, 30 insertions(+), 11 deletions(-)

diff --git a/catgets/gencat.c b/catgets/gencat.c
index 61ac797349..826596c2e4 100644
--- a/catgets/gencat.c
+++ b/catgets/gencat.c
@@ -838,6 +838,26 @@ 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,
@@ -927,12 +947,11 @@ write_out (struct catalog *catalog, const char *output_name,
   obj.plane_size = best_size;
   obj.plane_depth = best_depth;
 
+  uint32_t array_size = best_size * best_depth * sizeof (uint32_t) * 3;
   /* Allocate room for all needed arrays.  */
-  array1 =
-    (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
-  memset (array1, '\0', best_size * best_depth * sizeof (uint32_t) * 3);
-  array2
-    = (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
+  array1 = (uint32_t *) alloca (array_size);
+  memset (array1, '\0', array_size);
+  array2 = (uint32_t *) alloca (array_size);
   obstack_init (&string_pool);
 
   set_run = catalog->all_sets;
@@ -985,22 +1004,22 @@ write_out (struct catalog *catalog, const char *output_name,
     }
 
   /* Write out header.  */
-  write (fd, &obj, sizeof (obj));
+  write_all(fd, &obj, sizeof (obj));
 
   /* We always write out the little endian version of the index
      arrays.  */
 #if __BYTE_ORDER == __LITTLE_ENDIAN
-  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
-  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
+  write_all(fd, array1, array_size);
+  write_all(fd, array2, array_size);
 #elif __BYTE_ORDER == __BIG_ENDIAN
-  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
-  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
+  write_all(fd, array2, array_size);
+  write_all(fd, array1, array_size);
 #else
 # error Cannot handle __BYTE_ORDER byte order
 #endif
 
   /* Finally write the strings.  */
-  write (fd, strings, strings_size);
+  write_all(fd, strings, strings_size);
 
   if (fd != STDOUT_FILENO)
     close (fd);
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v5 02/12] malloc/{memusage.c,memusagestat.c}: fix warn unused result
  2023-04-18 12:11 ` [PATCH 4/8] locale/programs/locarchive.c: " Frédéric Bérat
  2023-04-18 12:43   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 02/15] " Frédéric Bérat
@ 2023-06-01 14:27   ` Frédéric Bérat
  2023-06-01 16:49     ` Siddhesh Poyarekar
  2 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-01 14:27 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
Changes since v4:
  - Mark strings for translation with gettext.
  - Fixed typo in (ret == 0) case

 malloc/memusage.c     | 54 +++++++++++++++++++++++++++++++++----------
 malloc/memusagestat.c | 48 ++++++++++++++++++++++++++++++++++----
 2 files changed, 86 insertions(+), 16 deletions(-)

diff --git a/malloc/memusage.c b/malloc/memusage.c
index 2a3a508557..53a3630651 100644
--- a/malloc/memusage.c
+++ b/malloc/memusage.c
@@ -18,7 +18,10 @@
 
 #include <assert.h>
 #include <dlfcn.h>
+#include <errno.h>
+#include <error.h>
 #include <fcntl.h>
+#include <libintl.h>
 #include <stdatomic.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -142,6 +145,27 @@ 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)
@@ -210,10 +234,11 @@ update_data (struct header *result, size_t len, size_t old_len)
       gettime (&buffer[idx]);
 
       /* Write out buffer if it is full.  */
-      if (idx + 1 == buffer_size)
-        write (fd, buffer, buffer_size * sizeof (struct entry));
-      else if (idx + 1 == 2 * buffer_size)
-        write (fd, &buffer[buffer_size], buffer_size * sizeof (struct entry));
+      if (idx + 1 == buffer_size || idx + 1 == 2 * buffer_size)
+        {
+	  uint32_t write_size = buffer_size * sizeof (buffer[0]);
+	  write_all (fd, &buffer[idx + 1 - buffer_size], write_size);
+        }
     }
 }
 
@@ -299,8 +324,8 @@ me (void)
               first.stack = 0;
               gettime (&first);
               /* Write it two times since we need the starting and end time. */
-              write (fd, &first, sizeof (first));
-              write (fd, &first, sizeof (first));
+	      write_all (fd, &first, sizeof (first));
+	      write_all (fd, &first, sizeof (first));
 
               /* Determine the buffer size.  We use the default if the
                  environment variable is not present.  */
@@ -850,24 +875,29 @@ dest (void)
   if (fd != -1)
     {
       /* Write the partially filled buffer.  */
+      struct entry *start = buffer;
+      uint32_t write_cnt = buffer_cnt;
+
       if (buffer_cnt > buffer_size)
-        write (fd, buffer + buffer_size,
-               (buffer_cnt - buffer_size) * sizeof (struct entry));
-      else
-        write (fd, buffer, buffer_cnt * sizeof (struct entry));
+        {
+          start = buffer + buffer_size;
+          write_cnt = buffer_cnt - buffer_size;
+        }
+
+      write_all (fd, start, write_cnt * sizeof (buffer[0]));
 
       /* Go back to the beginning of the file.  We allocated two records
          here when we opened the file.  */
       lseek (fd, 0, SEEK_SET);
       /* Write out a record containing the total size.  */
       first.stack = peak_total;
-      write (fd, &first, sizeof (struct entry));
+      write_all (fd, &first, sizeof (first));
       /* Write out another record containing the maximum for heap and
          stack.  */
       first.heap = peak_heap;
       first.stack = peak_stack;
       gettime (&first);
-      write (fd, &first, sizeof (struct entry));
+      write_all (fd, &first, sizeof (first));
 
       /* Close the file.  */
       close (fd);
diff --git a/malloc/memusagestat.c b/malloc/memusagestat.c
index 67c5131f79..cc066d4af9 100644
--- a/malloc/memusagestat.c
+++ b/malloc/memusagestat.c
@@ -114,6 +114,45 @@ 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[])
 {
@@ -188,7 +227,7 @@ main (int argc, char *argv[])
   total = st.st_size / sizeof (struct entry) - 2;
 
   /* Read the administrative information.  */
-  read (fd, headent, sizeof (headent));
+  read_all (fd, headent, sizeof (headent));
   maxsize_heap = headent[1].heap;
   maxsize_stack = headent[1].stack;
   maxsize_total = headent[0].stack;
@@ -220,7 +259,8 @@ main (int argc, char *argv[])
 
       /* Write the computed values in the file.  */
       lseek (fd, 0, SEEK_SET);
-      write (fd, headent, 2 * sizeof (struct entry));
+      write_all (fd, headent, sizeof (headent));
+
     }
 
   if (also_total)
@@ -372,7 +412,7 @@ main (int argc, char *argv[])
           size_t new[2];
           uint64_t now;
 
-          read (fd, &entry, sizeof (entry));
+          read_all (fd, &entry, sizeof (entry));
 
           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
 
@@ -455,7 +495,7 @@ main (int argc, char *argv[])
           size_t xpos;
           uint64_t now;
 
-          read (fd, &entry, sizeof (entry));
+          read_all (fd, &entry, sizeof (entry));
 
           now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
           xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v5 03/12] nptl_db/thread_dbP.h: fix warn unused result
  2023-04-18 12:11 ` [PATCH 6/8] nptl_db/thread_dbP.h: fix warn unused result Frédéric Bérat
  2023-04-18 12:49   ` Siddhesh Poyarekar
  2023-04-28 12:21   ` [PATCH v4 04/15] " Frédéric Bérat
@ 2023-06-01 14:27   ` Frédéric Bérat
  2023-06-01 16:49     ` Siddhesh Poyarekar
  2 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-01 14:27 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
glibc.
---
Changes since v5:
 - Wrapped line since it exceeded 79 characters

 nptl_db/thread_dbP.h | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/nptl_db/thread_dbP.h b/nptl_db/thread_dbP.h
index 8845722109..b52c254300 100644
--- a/nptl_db/thread_dbP.h
+++ b/nptl_db/thread_dbP.h
@@ -61,7 +61,9 @@ enum
 
 /* Comment out the following for less verbose output.  */
 #ifndef NDEBUG
-# define LOG(c) if (__td_debug) write (2, c "\n", strlen (c "\n"))
+# define LOG(c) \
+  if (__td_debug) \
+    assert (write (2, c "\n", strlen (c "\n")) == strlen (c "\n"))
 extern int __td_debug attribute_hidden;
 #else
 # define LOG(c)
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v5 04/12] tests: fix warn unused result on asprintf calls
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
                     ` (9 preceding siblings ...)
  2023-04-28 12:21   ` [PATCH v4 15/15] tests: fix warn unused results Frédéric Bérat
@ 2023-06-01 14:27   ` Frédéric Bérat
  2023-06-01 16:52     ` Siddhesh Poyarekar
  2023-06-01 14:27   ` [PATCH v5 07/12] tests: replace system by xsystem Frédéric Bérat
  2023-06-01 14:27   ` [PATCH v5 12/12] tests: fix warn unused results Frédéric Bérat
  12 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-01 14:27 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

When enabling _FORTIFY_SOURCE, some functions now lead to warnings when
their result is not checked.
---
Changes since v4:
 - Properly use xasprintf in all cases in posix/tst-execlp2.c

 argp/argp-test.c           |  8 +++++---
 posix/tst-execl2.c         |  8 ++------
 posix/tst-execle2.c        |  8 ++------
 posix/tst-execlp2.c        | 10 +++-------
 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, 28 insertions(+), 52 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 <string.h>
 #include <argp.h>
 
+#include <support/support.h>
+
 const char *argp_program_version = "argp-test 1.0";
 \f
 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..b42037babd 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");
@@ -59,7 +55,7 @@ do_test (void)
       return 1;
     }
   char *path;
-  asprintf (&path, "%s:../libio:../elf", bindir);
+  path = xasprintf ("%s:../libio:../elf", bindir);
   if (path == NULL)
     {
       puts ("asprintf  failed");
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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/support.h>
+
 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 <stdio.h>
 #include <string.h>
 
+#include <support/support.h>
+
 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 <time.h>
 #include <sys/stat.h>
 
+#include <support/support.h>
 
 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);
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v5 07/12] tests: replace system by xsystem
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
                     ` (10 preceding siblings ...)
  2023-06-01 14:27   ` [PATCH v5 04/12] tests: fix warn unused result on asprintf calls Frédéric Bérat
@ 2023-06-01 14:27   ` Frédéric Bérat
  2023-06-01 16:56     ` Siddhesh Poyarekar
  2023-06-01 14:27   ` [PATCH v5 12/12] tests: fix warn unused results Frédéric Bérat
  12 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-01 14:27 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

With fortification enabled, system calls return result needs to be checked,
has it gets the __wur macro enabled.
---
Changes since v4:
 - Added more fail case in xsystem
 - Clarify why some system calls should not use xsystem

 elf/tst-stackguard1.c     |  3 ++-
 libio/bug-mmap-fflush.c   |  7 +++++--
 nptl/tst-cancel7.c        |  3 ++-
 nptl/tst-stackguard1.c    |  3 ++-
 nss/tst-nss-db-endpwent.c |  3 ++-
 support/Makefile          |  1 +
 support/xstdlib.h         | 31 +++++++++++++++++++++++++++++++
 support/xsystem.c         | 37 +++++++++++++++++++++++++++++++++++++
 8 files changed, 82 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..1919ae8682 100644
--- a/elf/tst-stackguard1.c
+++ b/elf/tst-stackguard1.c
@@ -108,7 +108,8 @@ do_test (void)
 	  dup2 (fds[1], 2);
 	  close (fds[1]);
 
-	  system (command);
+	  if (system (command)) {/* Should actually fail */}
+
 	  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 <stdlib.h>
 #include <string.h>
 
+#include <support/xstdlib.h>
 
 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 <support/support.h>
 #include <support/temp_file.h>
 #include <support/xstdio.h>
+#include <support/xstdlib.h>
 #include <support/xunistd.h>
 #include <support/xthread.h>
 
@@ -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..5ca719ed32 100644
--- a/nptl/tst-stackguard1.c
+++ b/nptl/tst-stackguard1.c
@@ -140,7 +140,8 @@ do_test (void)
 	  dup2 (fds[1], 2);
 	  close (fds[1]);
 
-	  system (command);
+	  if (system (command)) {/* Should actually fail */}
+
 	  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 <support/support.h>
 #include <support/check.h>
+#include <support/xstdlib.h>
 
 /* 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 c3d16d25b3..f51c2e03a8 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..58642e3542
--- /dev/null
+++ b/support/xstdlib.h
@@ -0,0 +1,31 @@
+/* Error-checking wrappers for stdlib functions.
+   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
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef SUPPORT_XSTDLIB_H
+#define SUPPORT_XSTDLIB_H
+
+#include <stdlib.h>
+#include <sys/cdefs.h>
+
+__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..efa26ed772
--- /dev/null
+++ b/support/xsystem.c
@@ -0,0 +1,37 @@
+/* Error-checking replacement for "system".
+   Copyright (C) 2018-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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/support.h>
+#include <support/check.h>
+
+#include <support/xstdlib.h>
+
+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);
+}
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v5 12/12] tests: fix warn unused results
  2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
                     ` (11 preceding siblings ...)
  2023-06-01 14:27   ` [PATCH v5 07/12] tests: replace system by xsystem Frédéric Bérat
@ 2023-06-01 14:27   ` Frédéric Bérat
  2023-06-01 16:57     ` Siddhesh Poyarekar
  12 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-01 14:27 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

With fortification enabled, few function calls return result need to be
checked, has they get the __wur macro enabled.
---
Changes since v4:
 - Removed extra curly brackets

 crypt/cert.c                   |  6 +++++-
 misc/tst-efgcvt-template.c     |  4 ++--
 posix/tst-nice.c               |  3 +--
 posix/wordexp-test.c           |  6 +++++-
 stdio-common/bug19.c           |  9 +++++++--
 stdio-common/bug6.c            |  8 ++++----
 stdio-common/tstscanf.c        | 14 ++++++++++++--
 stdlib/test-canon.c            | 16 +++++++++++++---
 support/test-container.c       |  4 ++--
 sysdeps/pthread/tst-cancel16.c |  6 +++++-
 sysdeps/pthread/tst-cancel4.c  |  6 ++++--
 11 files changed, 60 insertions(+), 22 deletions(-)

diff --git a/crypt/cert.c b/crypt/cert.c
index 32c4386caf..5b4277f76d 100644
--- a/crypt/cert.c
+++ b/crypt/cert.c
@@ -99,7 +99,11 @@ get8 (char *cp)
 	int i,j,t;
 
 	for(i=0;i<8;i++){
-		scanf("%2x",&t);
+		if (scanf("%2x",&t) < 1)
+		  {
+		    if(ferror(stdin))
+		      totfails++;
+		  }
 		if(feof(stdin))
 		  good_bye();
 		for(j=0; j<8 ; j++) {
diff --git a/misc/tst-efgcvt-template.c b/misc/tst-efgcvt-template.c
index b924659a3d..87e3ebe4fa 100644
--- a/misc/tst-efgcvt-template.c
+++ b/misc/tst-efgcvt-template.c
@@ -200,8 +200,8 @@ special (void)
     output_error (NAME (ECVT), INFINITY, 10, "inf", 0, 0, p, decpt, sign);
 
   /* Simply make sure these calls with large NDIGITs don't crash.  */
-  (void) ECVT (123.456, 10000, &decpt, &sign);
-  (void) FCVT (123.456, 10000, &decpt, &sign);
+  p = ECVT (123.456, 10000, &decpt, &sign);
+  p = FCVT (123.456, 10000, &decpt, &sign);
 
   /* Some tests for the reentrant functions.  */
   /* Use a too small buffer.  */
diff --git a/posix/tst-nice.c b/posix/tst-nice.c
index fe9888b3f6..59cf953e27 100644
--- a/posix/tst-nice.c
+++ b/posix/tst-nice.c
@@ -58,8 +58,7 @@ do_test (void)
 
   /* BZ #18086. Make sure we don't reset errno.  */
   errno = EBADF;
-  nice (0);
-  if (errno != EBADF)
+  if (nice (0) == -1 || errno != EBADF)
     {
       printf ("FAIL: errno = %i, but wanted EBADF (%i)\n", errno, EBADF);
       return 1;
diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c
index 87d537c931..057c89dd3c 100644
--- a/posix/wordexp-test.c
+++ b/posix/wordexp-test.c
@@ -253,7 +253,11 @@ do_test (int argc, char *argv[])
   cwd = getcwd (NULL, 0);
 
   /* Set up arena for pathname expansion */
-  tmpnam (tmpdir);
+  if (!tmpnam (tmpdir))
+    {
+      printf ("Failed to create a temporary directory with a unique name: %m");
+      return 1;
+    }
   xmkdir (tmpdir, S_IRWXU);
   TEST_VERIFY_EXIT (chdir (tmpdir) == 0);
 
diff --git a/stdio-common/bug19.c b/stdio-common/bug19.c
index e083304bda..9a3deac3fc 100644
--- a/stdio-common/bug19.c
+++ b/stdio-common/bug19.c
@@ -29,12 +29,17 @@ do_test (void)
   printf("checking sscanf\n");
 
   int i, j, n;
+  int result = 0;
 
   i = j = n = 0;
-  FSCANF (fp, L(" %i - %i %n"), &i, &j, &n);
+  if (FSCANF (fp, L(" %i - %i %n"), &i, &j, &n) < 2)
+    {
+      printf ("FSCANF couldn't read all parameters %d\n", errno);
+      result = 1;
+    }
+
   printf ("found %i-%i (length=%i)\n", i, j, n);
 
-  int result = 0;
   if (i != 7)
     {
       printf ("i is %d, expected 7\n", i);
diff --git a/stdio-common/bug6.c b/stdio-common/bug6.c
index 0db63a3b44..50098bf3f2 100644
--- a/stdio-common/bug6.c
+++ b/stdio-common/bug6.c
@@ -7,16 +7,16 @@ main (void)
   int i;
   int lost = 0;
 
-  scanf ("%2s", buf);
+  lost = (scanf ("%2s", buf) < 0);
   lost |= (buf[0] != 'X' || buf[1] != 'Y' || buf[2] != '\0');
   if (lost)
     puts ("test of %2s failed.");
-  scanf (" ");
-  scanf ("%d", &i);
+  lost |= (scanf (" ") < 0);
+  lost |= (scanf ("%d", &i) < 0);
   lost |= (i != 1234);
   if (lost)
     puts ("test of %d failed.");
-  scanf ("%c", buf);
+  lost |= (scanf ("%c", buf) < 0);
   lost |= (buf[0] != 'L');
   if (lost)
     puts ("test of %c failed.\n");
diff --git a/stdio-common/tstscanf.c b/stdio-common/tstscanf.c
index 3a4ebf7524..7e92df4720 100644
--- a/stdio-common/tstscanf.c
+++ b/stdio-common/tstscanf.c
@@ -120,7 +120,12 @@ main (int argc, char **argv)
     int i;
     float x;
     char name[50];
-    (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
+    if (fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name) < 3)
+      {
+	fputs ("test failed!\n", stdout);
+	result = 1;
+      }
+
     fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
     if (i != 56 || x != 789.0F || strcmp (name, "56"))
       {
@@ -164,7 +169,12 @@ main (int argc, char **argv)
 	quant = 0.0;
 	units[0] = item[0] = '\0';
 	count = fscanf (in, "%f%20s of %20s", &quant, units, item);
-	(void) fscanf (in, "%*[^\n]");
+	if (fscanf (in, "%*[^\n]") < 0 && ferror (in))
+	  {
+	    fputs ("test failed!\n", stdout);
+	    result = 1;
+	  }
+
 	fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
 		 count, quant, item, units);
 	if (count != ok[rounds-1].count || quant != ok[rounds-1].quant
diff --git a/stdlib/test-canon.c b/stdlib/test-canon.c
index 5a2e7e1e6e..407ad5c4a2 100644
--- a/stdlib/test-canon.c
+++ b/stdlib/test-canon.c
@@ -123,8 +123,13 @@ do_test (int argc, char ** argv)
   int i, errors = 0;
   char buf[PATH_MAX];
 
-  getcwd (cwd, sizeof (buf));
-  cwd_len = strlen (cwd);
+  if (getcwd (cwd, sizeof (buf)))
+    cwd_len = strlen (cwd);
+  else
+    {
+      printf ("%s: current working directory couldn't be retrieved\n", argv[0]);
+      ++errors;
+    }
 
   errno = 0;
   if (realpath (NULL, buf) != NULL || errno != EINVAL)
@@ -210,7 +215,12 @@ do_test (int argc, char ** argv)
       free (result2);
     }
 
-  getcwd (buf, sizeof (buf));
+  if (!getcwd (buf, sizeof (buf)))
+    {
+      printf ("%s: current working directory couldn't be retrieved\n", argv[0]);
+      ++errors;
+    }
+
   if (strcmp (buf, cwd))
     {
       printf ("%s: current working directory changed from %s to %s\n",
diff --git a/support/test-container.c b/support/test-container.c
index 37beb778d6..f51afefedb 100644
--- a/support/test-container.c
+++ b/support/test-container.c
@@ -714,8 +714,8 @@ check_for_unshare_hints (int require_pidns)
         continue;
 
       val = -1; /* Sentinel.  */
-      fscanf (f, "%d", &val);
-      if (val != files[i].bad_value)
+      int cnt = fscanf (f, "%d", &val);
+      if (cnt == 1 && val != files[i].bad_value)
 	continue;
 
       printf ("To enable test-container, please run this as root:\n");
diff --git a/sysdeps/pthread/tst-cancel16.c b/sysdeps/pthread/tst-cancel16.c
index 511b9e1e91..d47c7c68cb 100644
--- a/sysdeps/pthread/tst-cancel16.c
+++ b/sysdeps/pthread/tst-cancel16.c
@@ -50,7 +50,11 @@ tf (void *arg)
   pthread_cleanup_push (cl, NULL);
 
   /* This call should never return.  */
-  (void) lockf (fd, F_LOCK, 0);
+  if (lockf (fd, F_LOCK, 0))
+    {
+      puts ("child thread: lockf failed");
+      exit (1);
+    }
 
   pthread_cleanup_pop (0);
 
diff --git a/sysdeps/pthread/tst-cancel4.c b/sysdeps/pthread/tst-cancel4.c
index 4f5c89314c..4c9e8670ca 100644
--- a/sysdeps/pthread/tst-cancel4.c
+++ b/sysdeps/pthread/tst-cancel4.c
@@ -1009,7 +1009,8 @@ tf_pread (void *arg)
   pthread_cleanup_push (cl, NULL);
 
   char mem[10];
-  pread (tempfd, mem, sizeof (mem), 0);
+  if (pread (tempfd, mem, sizeof (mem), 0) < 0)
+    FAIL_EXIT1 ("pread failed: %m");
 
   pthread_cleanup_pop (0);
 
@@ -1038,7 +1039,8 @@ tf_pwrite (void *arg)
   pthread_cleanup_push (cl, NULL);
 
   char mem[10];
-  pwrite (tempfd, mem, sizeof (mem), 0);
+  if (pwrite (tempfd, mem, sizeof (mem), 0) <0)
+    FAIL_EXIT1 ("pwrite failed: %m");
 
   pthread_cleanup_pop (0);
 
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH] Move {read,write}_all functions to a dedicated header
  2023-05-25  0:50     ` Siddhesh Poyarekar
@ 2023-06-01 15:55       ` Frédéric Bérat
  2023-06-01 17:07         ` Siddhesh Poyarekar
  0 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-01 15:55 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

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.
---
 catgets/gencat.c      | 22 +-------------------
 include/unistd_ext.h  | 48 +++++++++++++++++++++++++++++++++++++++++++
 malloc/memusage.c     | 25 +---------------------
 malloc/memusagestat.c | 40 +-----------------------------------
 4 files changed, 51 insertions(+), 84 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 <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <unistd_ext.h>
 #include <wchar.h>
 
 #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..b6ce067efb
--- /dev/null
+++ b/include/unistd_ext.h
@@ -0,0 +1,48 @@
+#ifndef _UNISTD_EXT_H
+#define _UNISTD_EXT_H
+
+#include <error.h>
+#include <errno.h>
+#include <libintl.h>
+#include <unistd.h>
+
+/* 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..1c6930188c 100644
--- a/malloc/memusage.c
+++ b/malloc/memusage.c
@@ -18,10 +18,7 @@
 
 #include <assert.h>
 #include <dlfcn.h>
-#include <errno.h>
-#include <error.h>
 #include <fcntl.h>
-#include <libintl.h>
 #include <stdatomic.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -30,6 +27,7 @@
 #include <sys/mman.h>
 #include <sys/time.h>
 #include <unistd.h>
+#include <unistd_ext.h>
 
 #include <hp-timing.h>
 #include <machine-sp.h>
@@ -145,27 +143,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 <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <unistd_ext.h>
 #include <stdint.h>
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -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[])
 {
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 08/15] tests: replace write by xwrite
  2023-05-25  1:16     ` Siddhesh Poyarekar
@ 2023-06-01 16:39       ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-01 16:39 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

Some tests need to be ported to test-driver.c but that could become a 
separate exercise.

Thanks,
Sid

On 2023-05-24 21:16, Siddhesh Poyarekar wrote:
> 
> 
> On 2023-04-28 08:21, Frédéric Bérat wrote:
>> Using write without cheks leads to warn unused result when __wur is
>> enabled.
>> ---
>>   dirent/tst-fdopendir.c        |  4 +++-
>>   io/tst-faccessat.c            |  3 ++-
>>   io/tst-fchmodat.c             |  3 ++-
>>   io/tst-fchownat.c             |  3 ++-
>>   io/tst-fstatat.c              |  3 ++-
>>   io/tst-futimesat.c            |  3 ++-
>>   io/tst-linkat.c               |  3 ++-
>>   io/tst-openat.c               |  3 ++-
>>   io/tst-renameat.c             |  3 ++-
>>   io/tst-symlinkat.c            |  3 ++-
>>   io/tst-unlinkat.c             |  3 ++-
>>   libio/bug-ungetc.c            |  4 +++-
>>   libio/bug-ungetc3.c           |  4 +++-
>>   libio/bug-ungetc4.c           |  4 +++-
>>   libio/bug-wfflush.c           |  4 +++-
>>   libio/bug-wsetpos.c           |  4 +++-
>>   nptl/tst-stackguard1.c        |  4 +++-
>>   nptl/tst-tls3.c               |  2 ++
>>   nptl/tst-tls3mod.c            |  5 +++--
>>   rt/tst-cpuclock2.c            |  4 +++-
>>   rt/tst-cputimer1.c            |  4 +++-
>>   rt/tst-cputimer2.c            |  4 +++-
>>   rt/tst-cputimer3.c            |  4 +++-
>>   support/test-container.c      |  8 ++++----
>>   sysdeps/pthread/tst-cond18.c  |  4 +++-
>>   sysdeps/pthread/tst-flock1.c  |  3 ++-
>>   sysdeps/pthread/tst-flock2.c  |  3 ++-
>>   sysdeps/pthread/tst-key1.c    | 11 ++++++-----
>>   sysdeps/pthread/tst-signal1.c |  3 ++-
>>   sysdeps/pthread/tst-signal2.c |  3 ++-
>>   sysdeps/pthread/tst-timer.c   |  3 ++-
>>   time/tst-cpuclock1.c          |  4 +++-
>>   32 files changed, 84 insertions(+), 39 deletions(-)
>>
>> diff --git a/dirent/tst-fdopendir.c b/dirent/tst-fdopendir.c
>> index 6321af1daa..2c9520574d 100644
>> --- a/dirent/tst-fdopendir.c
>> +++ b/dirent/tst-fdopendir.c
> 
> The test needs to be ported to support.h, that's probably true for other 
> tests in here too I'm afraid.  The change otherwise looks OK, so it just 
> needs an additional patch that ports the tests to support.h.
> 
> Thanks,
> Sid
> 
>> @@ -7,6 +7,8 @@
>>   #include <string.h>
>>   #include <sys/stat.h>
>> +#include <support/xunistd.h>
>> +
>>   #ifndef O_NOATIME
>>   # define O_NOATIME    0
>>   #endif
>> @@ -22,7 +24,7 @@ do_test (void)
>>         return 1;
>>       }
>> -  write (fd, "hello", 5);
>> +  xwrite (fd, "hello", 5);
>>     close (fd);
>>     struct stat64 st;
>> diff --git a/io/tst-faccessat.c b/io/tst-faccessat.c
>> index 7bdeed008c..b90954e318 100644
>> --- a/io/tst-faccessat.c
>> +++ b/io/tst-faccessat.c
>> @@ -8,6 +8,7 @@
>>   #include <unistd.h>
>>   #include <sys/stat.h>
>> +#include <support/xunistd.h>
>>   static void prepare (void);
>>   #define PREPARE(argc, argv) prepare ()
>> @@ -96,7 +97,7 @@ do_test (void)
>>         puts ("file creation failed");
>>         return 1;
>>       }
>> -  write (fd, "hello", 5);
>> +  xwrite (fd, "hello", 5);
>>     puts ("file created");
>>     /* Before closing the file, try using this file descriptor to open
>> diff --git a/io/tst-fchmodat.c b/io/tst-fchmodat.c
>> index 7d4a8717ff..83003e2f21 100644
>> --- a/io/tst-fchmodat.c
>> +++ b/io/tst-fchmodat.c
>> @@ -8,6 +8,7 @@
>>   #include <unistd.h>
>>   #include <sys/stat.h>
>> +#include <support/xunistd.h>
>>   static void prepare (void);
>>   #define PREPARE(argc, argv) prepare ()
>> @@ -98,7 +99,7 @@ do_test (void)
>>         puts ("file creation failed");
>>         return 1;
>>       }
>> -  write (fd, "hello", 5);
>> +  xwrite (fd, "hello", 5);
>>     puts ("file created");
>>     struct stat64 st1;
>> diff --git a/io/tst-fchownat.c b/io/tst-fchownat.c
>> index e8adf6229f..c0b87cda8f 100644
>> --- a/io/tst-fchownat.c
>> +++ b/io/tst-fchownat.c
>> @@ -6,6 +6,7 @@
>>   #include <unistd.h>
>>   #include <sys/stat.h>
>> +#include <support/xunistd.h>
>>   static void prepare (void);
>>   #define PREPARE(argc, argv) prepare ()
>> @@ -106,7 +107,7 @@ do_test (void)
>>         puts ("file creation failed");
>>         return 1;
>>       }
>> -  write (fd, "hello", 5);
>> +  xwrite (fd, "hello", 5);
>>     puts ("file created");
>>     struct stat64 st1;
>> diff --git a/io/tst-fstatat.c b/io/tst-fstatat.c
>> index 4766bb2e71..6a60024b63 100644
>> --- a/io/tst-fstatat.c
>> +++ b/io/tst-fstatat.c
>> @@ -6,6 +6,7 @@
>>   #include <unistd.h>
>>   #include <sys/stat.h>
>> +#include <support/xunistd.h>
>>   static void prepare (void);
>>   #define PREPARE(argc, argv) prepare ()
>> @@ -94,7 +95,7 @@ do_test (void)
>>         puts ("file creation failed");
>>         return 1;
>>       }
>> -  write (fd, "hello", 5);
>> +  xwrite (fd, "hello", 5);
>>     puts ("file created");
>>     struct stat64 st1;
>> diff --git a/io/tst-futimesat.c b/io/tst-futimesat.c
>> index 3d41721f42..b7ef386e06 100644
>> --- a/io/tst-futimesat.c
>> +++ b/io/tst-futimesat.c
>> @@ -28,6 +28,7 @@
>>   #include <support/test-driver.h>
>>   #include <support/temp_file.h>
>> +#include <support/xunistd.h>
>>   #ifndef struct_stat
>>   # define struct_stat struct stat64
>> @@ -114,7 +115,7 @@ do_test (void)
>>         puts ("file creation failed");
>>         return 1;
>>       }
>> -  write (fd, "hello", 5);
>> +  xwrite (fd, "hello", 5);
>>     puts ("file created");
>>     struct_stat st1;
>> diff --git a/io/tst-linkat.c b/io/tst-linkat.c
>> index 97445b7954..6b22a01c88 100644
>> --- a/io/tst-linkat.c
>> +++ b/io/tst-linkat.c
>> @@ -6,6 +6,7 @@
>>   #include <unistd.h>
>>   #include <sys/stat.h>
>> +#include <support/xunistd.h>
>>   static void prepare (void);
>>   #define PREPARE(argc, argv) prepare ()
>> @@ -94,7 +95,7 @@ do_test (void)
>>         puts ("file creation failed");
>>         return 1;
>>       }
>> -  write (fd, "hello", 5);
>> +  xwrite (fd, "hello", 5);
>>     puts ("file created");
>>     struct stat64 st1;
>> diff --git a/io/tst-openat.c b/io/tst-openat.c
>> index 741b8d0ad2..2ce89e3db1 100644
>> --- a/io/tst-openat.c
>> +++ b/io/tst-openat.c
>> @@ -6,6 +6,7 @@
>>   #include <string.h>
>>   #include <unistd.h>
>> +#include <support/xunistd.h>
>>   static void prepare (void);
>>   #define PREPARE(argc, argv) prepare ()
>> @@ -94,7 +95,7 @@ do_test (void)
>>         puts ("file creation failed");
>>         return 1;
>>       }
>> -  write (fd, "hello", 5);
>> +  xwrite (fd, "hello", 5);
>>     /* Before closing the file, try using this file descriptor to open
>>        another file.  This must fail.  */
>> diff --git a/io/tst-renameat.c b/io/tst-renameat.c
>> index 435302b52b..0b9da5fd6d 100644
>> --- a/io/tst-renameat.c
>> +++ b/io/tst-renameat.c
>> @@ -6,6 +6,7 @@
>>   #include <unistd.h>
>>   #include <sys/stat.h>
>> +#include <support/xunistd.h>
>>   static void prepare (void);
>>   #define PREPARE(argc, argv) prepare ()
>> @@ -94,7 +95,7 @@ do_test (void)
>>         puts ("file creation failed");
>>         return 1;
>>       }
>> -  write (fd, "hello", 5);
>> +  xwrite (fd, "hello", 5);
>>     puts ("file created");
>>     struct stat64 st1;
>> diff --git a/io/tst-symlinkat.c b/io/tst-symlinkat.c
>> index 214a8e348e..4a34994df7 100644
>> --- a/io/tst-symlinkat.c
>> +++ b/io/tst-symlinkat.c
>> @@ -6,6 +6,7 @@
>>   #include <unistd.h>
>>   #include <sys/stat.h>
>> +#include <support/xunistd.h>
>>   static void prepare (void);
>>   #define PREPARE(argc, argv) prepare ()
>> @@ -94,7 +95,7 @@ do_test (void)
>>         puts ("file creation failed");
>>         return 1;
>>       }
>> -  write (fd, "hello", 5);
>> +  xwrite (fd, "hello", 5);
>>     puts ("file created");
>>     struct stat64 st1;
>> diff --git a/io/tst-unlinkat.c b/io/tst-unlinkat.c
>> index e21d56f9f7..21a2dbaf57 100644
>> --- a/io/tst-unlinkat.c
>> +++ b/io/tst-unlinkat.c
>> @@ -6,6 +6,7 @@
>>   #include <string.h>
>>   #include <unistd.h>
>> +#include <support/xunistd.h>
>>   static void prepare (void);
>>   #define PREPARE(argc, argv) prepare ()
>> @@ -94,7 +95,7 @@ do_test (void)
>>         puts ("file creation failed");
>>         return 1;
>>       }
>> -  write (fd, "hello", 5);
>> +  xwrite (fd, "hello", 5);
>>     close (fd);
>>     puts ("file created");
>> diff --git a/libio/bug-ungetc.c b/libio/bug-ungetc.c
>> index 51940b68f5..4ea2d14ed6 100644
>> --- a/libio/bug-ungetc.c
>> +++ b/libio/bug-ungetc.c
>> @@ -2,6 +2,8 @@
>>   #include <stdio.h>
>> +#include <support/xunistd.h>
>> +
>>   static void do_prepare (void);
>>   #define PREPARE(argc, argv) do_prepare ()
>>   static int do_test (void);
>> @@ -20,7 +22,7 @@ do_prepare (void)
>>         printf ("cannot create temporary file: %m\n");
>>         exit (1);
>>       }
>> -  write (fd, pattern, sizeof (pattern));
>> +  xwrite (fd, pattern, sizeof (pattern));
>>     close (fd);
>>   }
>> diff --git a/libio/bug-ungetc3.c b/libio/bug-ungetc3.c
>> index 0c83c1161e..6100d7a936 100644
>> --- a/libio/bug-ungetc3.c
>> +++ b/libio/bug-ungetc3.c
>> @@ -2,6 +2,8 @@
>>   #include <stdio.h>
>> +#include <support/xunistd.h>
>> +
>>   static void do_prepare (void);
>>   #define PREPARE(argc, argv) do_prepare ()
>>   static int do_test (void);
>> @@ -20,7 +22,7 @@ do_prepare (void)
>>         printf ("cannot create temporary file: %m\n");
>>         exit (1);
>>       }
>> -  write (fd, pattern, sizeof (pattern));
>> +  xwrite (fd, pattern, sizeof (pattern));
>>     close (fd);
>>   }
>> diff --git a/libio/bug-ungetc4.c b/libio/bug-ungetc4.c
>> index 0bd02a570d..8a05def686 100644
>> --- a/libio/bug-ungetc4.c
>> +++ b/libio/bug-ungetc4.c
>> @@ -18,6 +18,8 @@
>>   #include <stdio.h>
>> +#include <support/xunistd.h>
>> +
>>   static void do_prepare (void);
>>   #define PREPARE(argc, argv) do_prepare ()
>>   static int do_test (void);
>> @@ -36,7 +38,7 @@ do_prepare (void)
>>         printf ("cannot create temporary file: %m\n");
>>         exit (1);
>>       }
>> -  write (fd, pattern, sizeof (pattern) - 1);
>> +  xwrite (fd, pattern, sizeof (pattern) - 1);
>>     close (fd);
>>   }
>> diff --git a/libio/bug-wfflush.c b/libio/bug-wfflush.c
>> index a8fd61e997..d1b9d8e9de 100644
>> --- a/libio/bug-wfflush.c
>> +++ b/libio/bug-wfflush.c
>> @@ -3,6 +3,8 @@
>>   #include <stdio.h>
>>   #include <wchar.h>
>> +#include <support/xunistd.h>
>> +
>>   static void do_prepare (void);
>>   #define PREPARE(argc, argv) do_prepare ()
>>   static int do_test (void);
>> @@ -20,7 +22,7 @@ do_prepare (void)
>>         printf ("cannot create temporary file: %m\n");
>>         exit (1);
>>       }
>> -  write (fd, "1!", 2);
>> +  xwrite (fd, "1!", 2);
>>     close (fd);
>>   }
>> diff --git a/libio/bug-wsetpos.c b/libio/bug-wsetpos.c
>> index ccb22a4b62..0fc373ba49 100644
>> --- a/libio/bug-wsetpos.c
>> +++ b/libio/bug-wsetpos.c
>> @@ -4,6 +4,8 @@
>>   #include <stdio.h>
>>   #include <wchar.h>
>> +#include <support/xunistd.h>
>> +
>>   static void do_prepare (void);
>>   #define PREPARE(argc, argv) do_prepare ()
>>   static int do_test (void);
>> @@ -22,7 +24,7 @@ do_prepare (void)
>>         printf ("cannot create temporary file: %m\n");
>>         exit (1);
>>       }
>> -  write (fd, pattern, sizeof (pattern));
>> +  xwrite (fd, pattern, sizeof (pattern));
>>     close (fd);
>>   }
>> diff --git a/nptl/tst-stackguard1.c b/nptl/tst-stackguard1.c
>> index b9cf6844de..4ac57157e9 100644
>> --- a/nptl/tst-stackguard1.c
>> +++ b/nptl/tst-stackguard1.c
>> @@ -27,6 +27,8 @@
>>   #include <tls.h>
>>   #include <unistd.h>
>> +#include <support/xunistd.h>
>> +
>>   static const char *command;
>>   static bool child;
>>   static uintptr_t stack_chk_guard_copy;
>> @@ -96,7 +98,7 @@ do_test (void)
>>       else if (ret != NULL)
>>         return 1;
>> -      write (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy));
>> +      xwrite (2, &stack_chk_guard_copy, sizeof (stack_chk_guard_copy));
>>         return 0;
>>       }
>> diff --git a/nptl/tst-tls3.c b/nptl/tst-tls3.c
>> index b1a40c624a..33d94c8cc5 100644
>> --- a/nptl/tst-tls3.c
>> +++ b/nptl/tst-tls3.c
>> @@ -26,6 +26,8 @@
>>   #include <unistd.h>
>>   #include <pthreaddef.h>
>> +#include <support/xunistd.h>
>> +
>>   #define THE_SIG SIGUSR1
>>   /* The stack size can be overriden.  With a sufficiently large stack
>> diff --git a/nptl/tst-tls3mod.c b/nptl/tst-tls3mod.c
>> index c6e8910b1e..345a48e1c7 100644
>> --- a/nptl/tst-tls3mod.c
>> +++ b/nptl/tst-tls3mod.c
>> @@ -25,6 +25,7 @@
>>   #include <pthreaddef.h>
>>   #include <descr.h>
>> +#include <support/xunistd.h>
>>   extern pthread_barrier_t b;
>> @@ -43,7 +44,7 @@ handler (int sig)
>>   {
>>     if (sig != THE_SIG)
>>       {
>> -      write (STDOUT_FILENO, "wrong signal\n", 13);
>> +      xwrite (STDOUT_FILENO, "wrong signal\n", 13);
>>         _exit (1);
>>       }
>> @@ -51,7 +52,7 @@ handler (int sig)
>>     if (sem_post (&s) != 0)
>>       {
>> -      write (STDOUT_FILENO, "sem_post failed\n", 16);
>> +      xwrite (STDOUT_FILENO, "sem_post failed\n", 16);
>>         _exit (1);
>>       }
>>   }
>> diff --git a/rt/tst-cpuclock2.c b/rt/tst-cpuclock2.c
>> index e4584d8791..8afd34ed9c 100644
>> --- a/rt/tst-cpuclock2.c
>> +++ b/rt/tst-cpuclock2.c
>> @@ -37,6 +37,8 @@ do_test ()
>>   #include <errno.h>
>>   #include <pthread.h>
>> +#include <support/xunistd.h>
>> +
>>   static pthread_barrier_t barrier;
>>   /* This function is intended to rack up both user and system time.  */
>> @@ -55,7 +57,7 @@ chew_cpu (void *arg)
>>         for (int i = 0; i < 100; ++i)
>>       for (size_t j = 0; j < sizeof buf; ++j)
>>         buf[j] = 0xbb;
>> -      write (nullfd, (char *) buf, sizeof buf);
>> +      xwrite (nullfd, (char *) buf, sizeof buf);
>>         close (nullfd);
>>       }
>> diff --git a/rt/tst-cputimer1.c b/rt/tst-cputimer1.c
>> index 8f5dd76cf2..18d8b195a2 100644
>> --- a/rt/tst-cputimer1.c
>> +++ b/rt/tst-cputimer1.c
>> @@ -11,6 +11,8 @@
>>   #include <time.h>
>>   #include <pthread.h>
>> +#include <support/xunistd.h>
>> +
>>   #define TEST_CLOCK CLOCK_PROCESS_CPUTIME_ID
>>   #define TEST_CLOCK_MISSING(clock) \
>>     (setup_test () ? "process CPU clock timer support" : NULL)
>> @@ -29,7 +31,7 @@ chew_cpu (void *arg)
>>         for (int i = 0; i < 100; ++i)
>>       for (size_t j = 0; j < sizeof buf; ++j)
>>         buf[j] = 0xbb;
>> -      write (nullfd, (char *) buf, sizeof buf);
>> +      xwrite (nullfd, (char *) buf, sizeof buf);
>>         close (nullfd);
>>       }
>> diff --git a/rt/tst-cputimer2.c b/rt/tst-cputimer2.c
>> index 397d7998c0..a5700d4bac 100644
>> --- a/rt/tst-cputimer2.c
>> +++ b/rt/tst-cputimer2.c
>> @@ -12,6 +12,8 @@
>>   #include <time.h>
>>   #include <pthread.h>
>> +#include <support/xunistd.h>
>> +
>>   static clockid_t worker_thread_clock;
>>   #define TEST_CLOCK worker_thread_clock
>> @@ -32,7 +34,7 @@ chew_cpu (void *arg)
>>         for (int i = 0; i < 100; ++i)
>>       for (size_t j = 0; j < sizeof buf; ++j)
>>         buf[j] = 0xbb;
>> -      write (nullfd, (char *) buf, sizeof buf);
>> +      xwrite (nullfd, (char *) buf, sizeof buf);
>>         close (nullfd);
>>       }
>> diff --git a/rt/tst-cputimer3.c b/rt/tst-cputimer3.c
>> index 056766a377..786de93a02 100644
>> --- a/rt/tst-cputimer3.c
>> +++ b/rt/tst-cputimer3.c
>> @@ -13,6 +13,8 @@
>>   #include <signal.h>
>>   #include <sys/wait.h>
>> +#include <support/xunistd.h>
>> +
>>   static clockid_t child_clock;
>>   #define TEST_CLOCK child_clock
>> @@ -33,7 +35,7 @@ chew_cpu (void)
>>         for (int i = 0; i < 100; ++i)
>>       for (size_t j = 0; j < sizeof buf; ++j)
>>         buf[j] = 0xbb;
>> -      write (nullfd, (char *) buf, sizeof buf);
>> +      xwrite (nullfd, (char *) buf, sizeof buf);
>>         close (nullfd);
>>         if (getppid () == 1)
>>       _exit (2);
>> diff --git a/support/test-container.c b/support/test-container.c
>> index e68f16eecf..d4ca41fe7c 100644
>> --- a/support/test-container.c
>> +++ b/support/test-container.c
>> @@ -1186,7 +1186,7 @@ main (int argc, char **argv)
>>         int status;
>>         /* Send the child's "outside" pid to it.  */
>> -      write (pipes[1], &child, sizeof(child));
>> +      xwrite (pipes[1], &child, sizeof(child));
>>         close (pipes[0]);
>>         close (pipes[1]);
>> @@ -1255,7 +1255,7 @@ main (int argc, char **argv)
>>         sprintf (tmp, "%lld %lld 1\n",
>>              (long long) (be_su ? 0 : original_uid), (long long) 
>> original_uid);
>> -      write (UMAP, tmp, strlen (tmp));
>> +      xwrite (UMAP, tmp, strlen (tmp));
>>         xclose (UMAP);
>>         /* We must disable setgroups () before we can map our groups, 
>> else we
>> @@ -1264,7 +1264,7 @@ main (int argc, char **argv)
>>         if (GMAP >= 0)
>>       {
>>         /* We support kernels old enough to not have this.  */
>> -      write (GMAP, "deny\n", 5);
>> +      xwrite (GMAP, "deny\n", 5);
>>         xclose (GMAP);
>>       }
>> @@ -1276,7 +1276,7 @@ main (int argc, char **argv)
>>         sprintf (tmp, "%lld %lld 1\n",
>>              (long long) (be_su ? 0 : original_gid), (long long) 
>> original_gid);
>> -      write (GMAP, tmp, strlen (tmp));
>> +      xwrite (GMAP, tmp, strlen (tmp));
>>         xclose (GMAP);
>>       }
>> diff --git a/sysdeps/pthread/tst-cond18.c b/sysdeps/pthread/tst-cond18.c
>> index edac4fa4ff..ffae356c04 100644
>> --- a/sysdeps/pthread/tst-cond18.c
>> +++ b/sysdeps/pthread/tst-cond18.c
>> @@ -24,6 +24,8 @@
>>   #include <stdio.h>
>>   #include <unistd.h>
>> +#include <support/xunistd.h>
>> +
>>   pthread_cond_t cv = PTHREAD_COND_INITIALIZER;
>>   pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
>>   bool exiting;
>> @@ -40,7 +42,7 @@ tf (void *id)
>>         while (!exiting)
>>       {
>>         if ((spins++ % 1000) == 0)
>> -        write (fd, ".", 1);
>> +        xwrite (fd, ".", 1);
>>         pthread_mutex_unlock (&lock);
>>         pthread_mutex_lock (&lock);
>> diff --git a/sysdeps/pthread/tst-flock1.c b/sysdeps/pthread/tst-flock1.c
>> index 7eef9070ab..9de148afd3 100644
>> --- a/sysdeps/pthread/tst-flock1.c
>> +++ b/sysdeps/pthread/tst-flock1.c
>> @@ -21,6 +21,7 @@
>>   #include <unistd.h>
>>   #include <sys/file.h>
>> +#include <support/xunistd.h>
>>   static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
>> @@ -56,7 +57,7 @@ do_test (void)
>>     unlink (tmp);
>> -  write (fd, "foobar xyzzy", 12);
>> +  xwrite (fd, "foobar xyzzy", 12);
>>     if (flock (fd, LOCK_EX | LOCK_NB) != 0)
>>       {
>> diff --git a/sysdeps/pthread/tst-flock2.c b/sysdeps/pthread/tst-flock2.c
>> index 8762e93b52..952b79e5db 100644
>> --- a/sysdeps/pthread/tst-flock2.c
>> +++ b/sysdeps/pthread/tst-flock2.c
>> @@ -24,6 +24,7 @@
>>   #include <sys/mman.h>
>>   #include <sys/wait.h>
>> +#include <support/xunistd.h>
>>   static pthread_mutex_t lock = PTHREAD_MUTEX_INITIALIZER;
>>   static pthread_mutex_t lock2 = PTHREAD_MUTEX_INITIALIZER;
>> @@ -70,7 +71,7 @@ do_test (void)
>>     int i;
>>     for (i = 0; i < 20; ++i)
>> -    write (fd, "foobar xyzzy", 12);
>> +    xwrite (fd, "foobar xyzzy", 12);
>>     pthread_barrier_t *b;
>>     b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
>> diff --git a/sysdeps/pthread/tst-key1.c b/sysdeps/pthread/tst-key1.c
>> index 933edafef8..60245c4e47 100644
>> --- a/sysdeps/pthread/tst-key1.c
>> +++ b/sysdeps/pthread/tst-key1.c
>> @@ -21,6 +21,7 @@
>>   #include <stdlib.h>
>>   #include <unistd.h>
>> +#include <support/xunistd.h>
>>   static int do_test (void);
>> @@ -51,7 +52,7 @@ do_test (void)
>>       if (pthread_setspecific (keys[i], (const void *) (i + 100l)) != 0)
>>         {
>> -        write (2, "setspecific failed\n", 19);
>> +        xwrite (2, "setspecific failed\n", 19);
>>           _exit (1);
>>         }
>>         }
>> @@ -60,13 +61,13 @@ do_test (void)
>>       {
>>         if (pthread_getspecific (keys[i]) != (void *) (i + 100l))
>>       {
>> -      write (2, "getspecific failed\n", 19);
>> +      xwrite (2, "getspecific failed\n", 19);
>>         _exit (1);
>>       }
>>         if (pthread_key_delete (keys[i]) != 0)
>>       {
>> -      write (2, "key_delete failed\n", 18);
>> +      xwrite (2, "key_delete failed\n", 18);
>>         _exit (1);
>>       }
>>       }
>> @@ -74,13 +75,13 @@ do_test (void)
>>     /* Now it must be once again possible to allocate keys.  */
>>     if (pthread_key_create (&keys[0], NULL) != 0)
>>       {
>> -      write (2, "2nd key_create failed\n", 22);
>> +      xwrite (2, "2nd key_create failed\n", 22);
>>         _exit (1);
>>       }
>>     if (pthread_key_delete (keys[0]) != 0)
>>       {
>> -      write (2, "2nd key_delete failed\n", 22);
>> +      xwrite (2, "2nd key_delete failed\n", 22);
>>         _exit (1);
>>       }
>> diff --git a/sysdeps/pthread/tst-signal1.c 
>> b/sysdeps/pthread/tst-signal1.c
>> index d1073e8459..d1082027ca 100644
>> --- a/sysdeps/pthread/tst-signal1.c
>> +++ b/sysdeps/pthread/tst-signal1.c
>> @@ -25,6 +25,7 @@
>>   #include <sys/mman.h>
>>   #include <sys/wait.h>
>> +#include <support/xunistd.h>
>>   static sigset_t ss;
>>   static pthread_barrier_t *b;
>> @@ -105,7 +106,7 @@ do_test (void)
>>     int i;
>>     for (i = 0; i < 20; ++i)
>> -    write (fd, "foobar xyzzy", 12);
>> +    xwrite (fd, "foobar xyzzy", 12);
>>     b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
>>           MAP_SHARED, fd, 0);
>> diff --git a/sysdeps/pthread/tst-signal2.c 
>> b/sysdeps/pthread/tst-signal2.c
>> index dfe7d9f64a..15b7747877 100644
>> --- a/sysdeps/pthread/tst-signal2.c
>> +++ b/sysdeps/pthread/tst-signal2.c
>> @@ -25,6 +25,7 @@
>>   #include <sys/wait.h>
>>   #include <string.h>
>> +#include <support/xunistd.h>
>>   static sigset_t ss;
>>   static pthread_barrier_t *b;
>> @@ -111,7 +112,7 @@ do_test (void)
>>     int i;
>>     for (i = 0; i < 20; ++i)
>> -    write (fd, "foobar xyzzy", 12);
>> +    xwrite (fd, "foobar xyzzy", 12);
>>     b = mmap (NULL, sizeof (pthread_barrier_t), PROT_READ | PROT_WRITE,
>>           MAP_SHARED, fd, 0);
>> diff --git a/sysdeps/pthread/tst-timer.c b/sysdeps/pthread/tst-timer.c
>> index 47472ab8e1..4cfe0b67dc 100644
>> --- a/sysdeps/pthread/tst-timer.c
>> +++ b/sysdeps/pthread/tst-timer.c
>> @@ -24,6 +24,7 @@
>>   #include <stdlib.h>
>>   #include <stdint.h>
>> +#include <support/xunistd.h>
>>   static void
>>   notify_func1 (union sigval sigval)
>> @@ -44,7 +45,7 @@ signal_func (int sig)
>>   {
>>     static const char text[] = "signal_func\n";
>>     signal (sig, signal_func);
>> -  write (STDOUT_FILENO, text, sizeof text - 1);
>> +  xwrite (STDOUT_FILENO, text, sizeof text - 1);
>>   }
>>   static void
>> diff --git a/time/tst-cpuclock1.c b/time/tst-cpuclock1.c
>> index 6f2e70a58a..6a793e06df 100644
>> --- a/time/tst-cpuclock1.c
>> +++ b/time/tst-cpuclock1.c
>> @@ -27,6 +27,8 @@
>>   #include <stdint.h>
>>   #include <sys/wait.h>
>> +#include <support/xunistd.h>
>> +
>>   /* This function is intended to rack up both user and system time.  */
>>   static void
>>   chew_cpu (void)
>> @@ -41,7 +43,7 @@ chew_cpu (void)
>>         for (int i = 0; i < 100; ++i)
>>       for (size_t j = 0; j < sizeof buf; ++j)
>>         buf[j] = 0xbb;
>> -      write (nullfd, (char *) buf, sizeof buf);
>> +      xwrite (nullfd, (char *) buf, sizeof buf);
>>         close (nullfd);
>>         if (getppid () == 1)
>>       _exit (2);
> 

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v4 12/15] tests: replace ftruncate by xftruncate
  2023-05-25  1:25     ` Siddhesh Poyarekar
@ 2023-06-01 16:42       ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-01 16:42 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-05-24 21:25, Siddhesh Poyarekar wrote:
> 
> 
> On 2023-04-28 08:21, Frédéric Bérat wrote:
>> With fortification enabled, ftruncate calls return result needs to be
>> checked, has it gets the __wur macro enabled.
>> ---
>>   io/tst-copy_file_range.c  | 2 +-
>>   posix/tst-getopt-cancel.c | 3 ++-
>>   stdio-common/tst-perror.c | 3 ++-
>>   3 files changed, 5 insertions(+), 3 deletions(-)

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

Some tests need to be ported to test-driver.c but that could become a 
separate exercise.

Thanks,
Sid

>>
>> diff --git a/io/tst-copy_file_range.c b/io/tst-copy_file_range.c
>> index d1f3aaa5a9..bd64e9c42b 100644
>> --- a/io/tst-copy_file_range.c
>> +++ b/io/tst-copy_file_range.c
>> @@ -166,7 +166,7 @@ short_copy (void)
>>             inoff = 3;
>>             xlseek (infd, shift, SEEK_SET);
>>           }
>> -      ftruncate (outfd, 0);
>> +      xftruncate (outfd, 0);
>>         xlseek (outfd, 0, SEEK_SET);
>>         outoff = 0;
>> diff --git a/posix/tst-getopt-cancel.c b/posix/tst-getopt-cancel.c
>> index 7167d1a914..6f49391690 100644
>> --- a/posix/tst-getopt-cancel.c
>> +++ b/posix/tst-getopt-cancel.c
>> @@ -33,6 +33,7 @@
>>   #include <support/support.h>
>>   #include <support/temp_file.h>
>>   #include <support/xthread.h>
>> +#include <support/xunistd.h>
>>   static bool
>>   check_stderr (bool expect_errmsg, FILE *stderr_trapped)
>> @@ -48,7 +49,7 @@ check_stderr (bool expect_errmsg, FILE *stderr_trapped)
>>         fputs (lineptr, stdout);
>>       }
>>     rewind (stderr_trapped);
>> -  ftruncate (fileno (stderr_trapped), 0);
>> +  xftruncate (fileno (stderr_trapped), 0);
>>     return got_errmsg == expect_errmsg;
>>   }
>> diff --git a/stdio-common/tst-perror.c b/stdio-common/tst-perror.c
>> index 57835e0c59..b4ab583462 100644
>> --- a/stdio-common/tst-perror.c
>> +++ b/stdio-common/tst-perror.c
> 
> This one needs to be ported to use support/test-driver.  The patch is OK 
> otherwise.
> 
>> @@ -9,6 +9,7 @@
>>   #include <unistd.h>
>>   #include <wchar.h>
>> +#include <support/xunistd.h>
>>   #define MB_EXP \
>>     "null mode test 1: Invalid or incomplete multibyte or wide 
>> character\n" \
>> @@ -94,7 +95,7 @@ do_test (void)
>>       puts ("multibyte test succeeded");
>>     lseek (fd, 0, SEEK_SET);
>> -  ftruncate (fd, 0);
>> +  xftruncate (fd, 0);
>>     if (dup2 (fd, 2) == -1)
>>       {
> 

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v5 01/12] catgets/gencat.c: fix warn unused result
  2023-06-01 14:27   ` [PATCH v5 01/12] " Frédéric Bérat
@ 2023-06-01 16:47     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-01 16:47 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-01 10:27, Frédéric Bérat wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
> Changes since v4:
>    - Mark strings for translation with gettext.
>    - Fixed typo in (ret == 0) case
> 
>   catgets/gencat.c | 41 ++++++++++++++++++++++++++++++-----------
>   1 file changed, 30 insertions(+), 11 deletions(-)

LGTM.  You're moving write_all into a separate file later, so OK.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
> diff --git a/catgets/gencat.c b/catgets/gencat.c
> index 61ac797349..826596c2e4 100644
> --- a/catgets/gencat.c
> +++ b/catgets/gencat.c
> @@ -838,6 +838,26 @@ 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,
> @@ -927,12 +947,11 @@ write_out (struct catalog *catalog, const char *output_name,
>     obj.plane_size = best_size;
>     obj.plane_depth = best_depth;
>   
> +  uint32_t array_size = best_size * best_depth * sizeof (uint32_t) * 3;
>     /* Allocate room for all needed arrays.  */
> -  array1 =
> -    (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
> -  memset (array1, '\0', best_size * best_depth * sizeof (uint32_t) * 3);
> -  array2
> -    = (uint32_t *) alloca (best_size * best_depth * sizeof (uint32_t) * 3);
> +  array1 = (uint32_t *) alloca (array_size);
> +  memset (array1, '\0', array_size);
> +  array2 = (uint32_t *) alloca (array_size);
>     obstack_init (&string_pool);
>   
>     set_run = catalog->all_sets;
> @@ -985,22 +1004,22 @@ write_out (struct catalog *catalog, const char *output_name,
>       }
>   
>     /* Write out header.  */
> -  write (fd, &obj, sizeof (obj));
> +  write_all(fd, &obj, sizeof (obj));
>   
>     /* We always write out the little endian version of the index
>        arrays.  */
>   #if __BYTE_ORDER == __LITTLE_ENDIAN
> -  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
> -  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
> +  write_all(fd, array1, array_size);
> +  write_all(fd, array2, array_size);
>   #elif __BYTE_ORDER == __BIG_ENDIAN
> -  write (fd, array2, best_size * best_depth * sizeof (uint32_t) * 3);
> -  write (fd, array1, best_size * best_depth * sizeof (uint32_t) * 3);
> +  write_all(fd, array2, array_size);
> +  write_all(fd, array1, array_size);
>   #else
>   # error Cannot handle __BYTE_ORDER byte order
>   #endif
>   
>     /* Finally write the strings.  */
> -  write (fd, strings, strings_size);
> +  write_all(fd, strings, strings_size);
>   
>     if (fd != STDOUT_FILENO)
>       close (fd);

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v5 02/12] malloc/{memusage.c,memusagestat.c}: fix warn unused result
  2023-06-01 14:27   ` [PATCH v5 02/12] malloc/{memusage.c,memusagestat.c}: " Frédéric Bérat
@ 2023-06-01 16:49     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-01 16:49 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-01 10:27, Frédéric Bérat wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
> Changes since v4:
>    - Mark strings for translation with gettext.
>    - Fixed typo in (ret == 0) case
> 
>   malloc/memusage.c     | 54 +++++++++++++++++++++++++++++++++----------
>   malloc/memusagestat.c | 48 ++++++++++++++++++++++++++++++++++----
>   2 files changed, 86 insertions(+), 16 deletions(-)
> 

LGTM.  You're moving write_all and read_all into a separate file later, 
so OK.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> diff --git a/malloc/memusage.c b/malloc/memusage.c
> index 2a3a508557..53a3630651 100644
> --- a/malloc/memusage.c
> +++ b/malloc/memusage.c
> @@ -18,7 +18,10 @@
>   
>   #include <assert.h>
>   #include <dlfcn.h>
> +#include <errno.h>
> +#include <error.h>
>   #include <fcntl.h>
> +#include <libintl.h>
>   #include <stdatomic.h>
>   #include <stdbool.h>
>   #include <stdio.h>
> @@ -142,6 +145,27 @@ 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)
> @@ -210,10 +234,11 @@ update_data (struct header *result, size_t len, size_t old_len)
>         gettime (&buffer[idx]);
>   
>         /* Write out buffer if it is full.  */
> -      if (idx + 1 == buffer_size)
> -        write (fd, buffer, buffer_size * sizeof (struct entry));
> -      else if (idx + 1 == 2 * buffer_size)
> -        write (fd, &buffer[buffer_size], buffer_size * sizeof (struct entry));
> +      if (idx + 1 == buffer_size || idx + 1 == 2 * buffer_size)
> +        {
> +	  uint32_t write_size = buffer_size * sizeof (buffer[0]);
> +	  write_all (fd, &buffer[idx + 1 - buffer_size], write_size);
> +        }
>       }
>   }
>   
> @@ -299,8 +324,8 @@ me (void)
>                 first.stack = 0;
>                 gettime (&first);
>                 /* Write it two times since we need the starting and end time. */
> -              write (fd, &first, sizeof (first));
> -              write (fd, &first, sizeof (first));
> +	      write_all (fd, &first, sizeof (first));
> +	      write_all (fd, &first, sizeof (first));
>   
>                 /* Determine the buffer size.  We use the default if the
>                    environment variable is not present.  */
> @@ -850,24 +875,29 @@ dest (void)
>     if (fd != -1)
>       {
>         /* Write the partially filled buffer.  */
> +      struct entry *start = buffer;
> +      uint32_t write_cnt = buffer_cnt;
> +
>         if (buffer_cnt > buffer_size)
> -        write (fd, buffer + buffer_size,
> -               (buffer_cnt - buffer_size) * sizeof (struct entry));
> -      else
> -        write (fd, buffer, buffer_cnt * sizeof (struct entry));
> +        {
> +          start = buffer + buffer_size;
> +          write_cnt = buffer_cnt - buffer_size;
> +        }
> +
> +      write_all (fd, start, write_cnt * sizeof (buffer[0]));
>   
>         /* Go back to the beginning of the file.  We allocated two records
>            here when we opened the file.  */
>         lseek (fd, 0, SEEK_SET);
>         /* Write out a record containing the total size.  */
>         first.stack = peak_total;
> -      write (fd, &first, sizeof (struct entry));
> +      write_all (fd, &first, sizeof (first));
>         /* Write out another record containing the maximum for heap and
>            stack.  */
>         first.heap = peak_heap;
>         first.stack = peak_stack;
>         gettime (&first);
> -      write (fd, &first, sizeof (struct entry));
> +      write_all (fd, &first, sizeof (first));
>   
>         /* Close the file.  */
>         close (fd);
> diff --git a/malloc/memusagestat.c b/malloc/memusagestat.c
> index 67c5131f79..cc066d4af9 100644
> --- a/malloc/memusagestat.c
> +++ b/malloc/memusagestat.c
> @@ -114,6 +114,45 @@ 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[])
>   {
> @@ -188,7 +227,7 @@ main (int argc, char *argv[])
>     total = st.st_size / sizeof (struct entry) - 2;
>   
>     /* Read the administrative information.  */
> -  read (fd, headent, sizeof (headent));
> +  read_all (fd, headent, sizeof (headent));
>     maxsize_heap = headent[1].heap;
>     maxsize_stack = headent[1].stack;
>     maxsize_total = headent[0].stack;
> @@ -220,7 +259,8 @@ main (int argc, char *argv[])
>   
>         /* Write the computed values in the file.  */
>         lseek (fd, 0, SEEK_SET);
> -      write (fd, headent, 2 * sizeof (struct entry));
> +      write_all (fd, headent, sizeof (headent));
> +
>       }
>   
>     if (also_total)
> @@ -372,7 +412,7 @@ main (int argc, char *argv[])
>             size_t new[2];
>             uint64_t now;
>   
> -          read (fd, &entry, sizeof (entry));
> +          read_all (fd, &entry, sizeof (entry));
>   
>             now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
>   
> @@ -455,7 +495,7 @@ main (int argc, char *argv[])
>             size_t xpos;
>             uint64_t now;
>   
> -          read (fd, &entry, sizeof (entry));
> +          read_all (fd, &entry, sizeof (entry));
>   
>             now = ((uint64_t) entry.time_high) << 32 | entry.time_low;
>             xpos = 40 + ((xsize - 80) * (now - start_time)) / total_time;

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v5 03/12] nptl_db/thread_dbP.h: fix warn unused result
  2023-06-01 14:27   ` [PATCH v5 03/12] " Frédéric Bérat
@ 2023-06-01 16:49     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-01 16:49 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-01 10:27, Frédéric Bérat wrote:
> Fix unused result warnings, detected when _FORTIFY_SOURCE is enabled in
> glibc.
> ---
> Changes since v5:
>   - Wrapped line since it exceeded 79 characters
> 
>   nptl_db/thread_dbP.h | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
> diff --git a/nptl_db/thread_dbP.h b/nptl_db/thread_dbP.h
> index 8845722109..b52c254300 100644
> --- a/nptl_db/thread_dbP.h
> +++ b/nptl_db/thread_dbP.h
> @@ -61,7 +61,9 @@ enum
>   
>   /* Comment out the following for less verbose output.  */
>   #ifndef NDEBUG
> -# define LOG(c) if (__td_debug) write (2, c "\n", strlen (c "\n"))
> +# define LOG(c) \
> +  if (__td_debug) \
> +    assert (write (2, c "\n", strlen (c "\n")) == strlen (c "\n"))
>   extern int __td_debug attribute_hidden;
>   #else
>   # define LOG(c)

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v5 04/12] tests: fix warn unused result on asprintf calls
  2023-06-01 14:27   ` [PATCH v5 04/12] tests: fix warn unused result on asprintf calls Frédéric Bérat
@ 2023-06-01 16:52     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-01 16:52 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-01 10:27, Frédéric Bérat wrote:
> When enabling _FORTIFY_SOURCE, some functions now lead to warnings when
> their result is not checked.
> ---
> Changes since v4:
>   - Properly use xasprintf in all cases in posix/tst-execlp2.c
> 
>   argp/argp-test.c           |  8 +++++---
>   posix/tst-execl2.c         |  8 ++------
>   posix/tst-execle2.c        |  8 ++------
>   posix/tst-execlp2.c        | 10 +++-------
>   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, 28 insertions(+), 52 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 <string.h>
>   #include <argp.h>
>   
> +#include <support/support.h>
> +
>   const char *argp_program_version = "argp-test 1.0";
>   \f
>   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..b42037babd 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");
> @@ -59,7 +55,7 @@ do_test (void)
>         return 1;
>       }
>     char *path;
> -  asprintf (&path, "%s:../libio:../elf", bindir);
> +  path = xasprintf ("%s:../libio:../elf", bindir);
>     if (path == NULL)

You don't need this check anymore, do you?

>       {
>         puts ("asprintf  failed");
> 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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/support.h>
> +
>   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);

Btw, given the number of times this pattern is repeating, I wonder if 
there's utility in wrapping this xasprintf + system pattern.  Anyway, 
it's for a different patch if you or anybody else ever feels motivated 
to do this cleanup.

>     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 <stdio.h>
>   #include <string.h>
>   
> +#include <support/support.h>
> +
>   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 <time.h>
>   #include <sys/stat.h>
>   
> +#include <support/support.h>
>   
>   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);

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v5 07/12] tests: replace system by xsystem
  2023-06-01 14:27   ` [PATCH v5 07/12] tests: replace system by xsystem Frédéric Bérat
@ 2023-06-01 16:56     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-01 16:56 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-01 10:27, Frédéric Bérat wrote:
> With fortification enabled, system calls return result needs to be checked,
> has it gets the __wur macro enabled.
> ---
> Changes since v4:
>   - Added more fail case in xsystem
>   - Clarify why some system calls should not use xsystem
> 
>   elf/tst-stackguard1.c     |  3 ++-
>   libio/bug-mmap-fflush.c   |  7 +++++--
>   nptl/tst-cancel7.c        |  3 ++-
>   nptl/tst-stackguard1.c    |  3 ++-
>   nss/tst-nss-db-endpwent.c |  3 ++-
>   support/Makefile          |  1 +
>   support/xstdlib.h         | 31 +++++++++++++++++++++++++++++++
>   support/xsystem.c         | 37 +++++++++++++++++++++++++++++++++++++
>   8 files changed, 82 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..1919ae8682 100644
> --- a/elf/tst-stackguard1.c
> +++ b/elf/tst-stackguard1.c
> @@ -108,7 +108,8 @@ do_test (void)
>   	  dup2 (fds[1], 2);
>   	  close (fds[1]);
>   
> -	  system (command);
> +	  if (system (command)) {/* Should actually fail */}

Please elaborate on why this should fail:

         if (system (command))
           {
             /* This should actually fail because... */
           }

> +
>   	  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 <stdlib.h>
>   #include <string.h>
>   
> +#include <support/xstdlib.h>
>   
>   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 <support/support.h>
>   #include <support/temp_file.h>
>   #include <support/xstdio.h>
> +#include <support/xstdlib.h>
>   #include <support/xunistd.h>
>   #include <support/xthread.h>
>   
> @@ -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..5ca719ed32 100644
> --- a/nptl/tst-stackguard1.c
> +++ b/nptl/tst-stackguard1.c
> @@ -140,7 +140,8 @@ do_test (void)
>   	  dup2 (fds[1], 2);
>   	  close (fds[1]);
>   
> -	  system (command);
> +	  if (system (command)) {/* Should actually fail */}
> +
>   	  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 <support/support.h>
>   #include <support/check.h>
> +#include <support/xstdlib.h>
>   
>   /* 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 c3d16d25b3..f51c2e03a8 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..58642e3542
> --- /dev/null
> +++ b/support/xstdlib.h
> @@ -0,0 +1,31 @@
> +/* Error-checking wrappers for stdlib functions.
> +   Copyright (C) 2016-2023 Free Software Foundation, Inc.

Only 2023 here.

> +   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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#ifndef SUPPORT_XSTDLIB_H
> +#define SUPPORT_XSTDLIB_H
> +
> +#include <stdlib.h>
> +#include <sys/cdefs.h>
> +
> +__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..efa26ed772
> --- /dev/null
> +++ b/support/xsystem.c
> @@ -0,0 +1,37 @@
> +/* Error-checking replacement for "system".
> +   Copyright (C) 2018-2023 Free Software Foundation, Inc.

Likewise, only 2023.

> +   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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/support.h>
> +#include <support/check.h>
> +
> +#include <support/xstdlib.h>
> +
> +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);
> +}

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v5 12/12] tests: fix warn unused results
  2023-06-01 14:27   ` [PATCH v5 12/12] tests: fix warn unused results Frédéric Bérat
@ 2023-06-01 16:57     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-01 16:57 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-01 10:27, Frédéric Bérat wrote:
> With fortification enabled, few function calls return result need to be
> checked, has they get the __wur macro enabled.
> ---
> Changes since v4:
>   - Removed extra curly brackets

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
>   crypt/cert.c                   |  6 +++++-
>   misc/tst-efgcvt-template.c     |  4 ++--
>   posix/tst-nice.c               |  3 +--
>   posix/wordexp-test.c           |  6 +++++-
>   stdio-common/bug19.c           |  9 +++++++--
>   stdio-common/bug6.c            |  8 ++++----
>   stdio-common/tstscanf.c        | 14 ++++++++++++--
>   stdlib/test-canon.c            | 16 +++++++++++++---
>   support/test-container.c       |  4 ++--
>   sysdeps/pthread/tst-cancel16.c |  6 +++++-
>   sysdeps/pthread/tst-cancel4.c  |  6 ++++--
>   11 files changed, 60 insertions(+), 22 deletions(-)
> 
> diff --git a/crypt/cert.c b/crypt/cert.c
> index 32c4386caf..5b4277f76d 100644
> --- a/crypt/cert.c
> +++ b/crypt/cert.c
> @@ -99,7 +99,11 @@ get8 (char *cp)
>   	int i,j,t;
>   
>   	for(i=0;i<8;i++){
> -		scanf("%2x",&t);
> +		if (scanf("%2x",&t) < 1)
> +		  {
> +		    if(ferror(stdin))
> +		      totfails++;
> +		  }
>   		if(feof(stdin))
>   		  good_bye();
>   		for(j=0; j<8 ; j++) {
> diff --git a/misc/tst-efgcvt-template.c b/misc/tst-efgcvt-template.c
> index b924659a3d..87e3ebe4fa 100644
> --- a/misc/tst-efgcvt-template.c
> +++ b/misc/tst-efgcvt-template.c
> @@ -200,8 +200,8 @@ special (void)
>       output_error (NAME (ECVT), INFINITY, 10, "inf", 0, 0, p, decpt, sign);
>   
>     /* Simply make sure these calls with large NDIGITs don't crash.  */
> -  (void) ECVT (123.456, 10000, &decpt, &sign);
> -  (void) FCVT (123.456, 10000, &decpt, &sign);
> +  p = ECVT (123.456, 10000, &decpt, &sign);
> +  p = FCVT (123.456, 10000, &decpt, &sign);
>   
>     /* Some tests for the reentrant functions.  */
>     /* Use a too small buffer.  */
> diff --git a/posix/tst-nice.c b/posix/tst-nice.c
> index fe9888b3f6..59cf953e27 100644
> --- a/posix/tst-nice.c
> +++ b/posix/tst-nice.c
> @@ -58,8 +58,7 @@ do_test (void)
>   
>     /* BZ #18086. Make sure we don't reset errno.  */
>     errno = EBADF;
> -  nice (0);
> -  if (errno != EBADF)
> +  if (nice (0) == -1 || errno != EBADF)
>       {
>         printf ("FAIL: errno = %i, but wanted EBADF (%i)\n", errno, EBADF);
>         return 1;
> diff --git a/posix/wordexp-test.c b/posix/wordexp-test.c
> index 87d537c931..057c89dd3c 100644
> --- a/posix/wordexp-test.c
> +++ b/posix/wordexp-test.c
> @@ -253,7 +253,11 @@ do_test (int argc, char *argv[])
>     cwd = getcwd (NULL, 0);
>   
>     /* Set up arena for pathname expansion */
> -  tmpnam (tmpdir);
> +  if (!tmpnam (tmpdir))
> +    {
> +      printf ("Failed to create a temporary directory with a unique name: %m");
> +      return 1;
> +    }
>     xmkdir (tmpdir, S_IRWXU);
>     TEST_VERIFY_EXIT (chdir (tmpdir) == 0);
>   
> diff --git a/stdio-common/bug19.c b/stdio-common/bug19.c
> index e083304bda..9a3deac3fc 100644
> --- a/stdio-common/bug19.c
> +++ b/stdio-common/bug19.c
> @@ -29,12 +29,17 @@ do_test (void)
>     printf("checking sscanf\n");
>   
>     int i, j, n;
> +  int result = 0;
>   
>     i = j = n = 0;
> -  FSCANF (fp, L(" %i - %i %n"), &i, &j, &n);
> +  if (FSCANF (fp, L(" %i - %i %n"), &i, &j, &n) < 2)
> +    {
> +      printf ("FSCANF couldn't read all parameters %d\n", errno);
> +      result = 1;
> +    }
> +
>     printf ("found %i-%i (length=%i)\n", i, j, n);
>   
> -  int result = 0;
>     if (i != 7)
>       {
>         printf ("i is %d, expected 7\n", i);
> diff --git a/stdio-common/bug6.c b/stdio-common/bug6.c
> index 0db63a3b44..50098bf3f2 100644
> --- a/stdio-common/bug6.c
> +++ b/stdio-common/bug6.c
> @@ -7,16 +7,16 @@ main (void)
>     int i;
>     int lost = 0;
>   
> -  scanf ("%2s", buf);
> +  lost = (scanf ("%2s", buf) < 0);
>     lost |= (buf[0] != 'X' || buf[1] != 'Y' || buf[2] != '\0');
>     if (lost)
>       puts ("test of %2s failed.");
> -  scanf (" ");
> -  scanf ("%d", &i);
> +  lost |= (scanf (" ") < 0);
> +  lost |= (scanf ("%d", &i) < 0);
>     lost |= (i != 1234);
>     if (lost)
>       puts ("test of %d failed.");
> -  scanf ("%c", buf);
> +  lost |= (scanf ("%c", buf) < 0);
>     lost |= (buf[0] != 'L');
>     if (lost)
>       puts ("test of %c failed.\n");
> diff --git a/stdio-common/tstscanf.c b/stdio-common/tstscanf.c
> index 3a4ebf7524..7e92df4720 100644
> --- a/stdio-common/tstscanf.c
> +++ b/stdio-common/tstscanf.c
> @@ -120,7 +120,12 @@ main (int argc, char **argv)
>       int i;
>       float x;
>       char name[50];
> -    (void) fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name);
> +    if (fscanf (in, "%2d%f%*d %[0123456789]", &i, &x, name) < 3)
> +      {
> +	fputs ("test failed!\n", stdout);
> +	result = 1;
> +      }
> +
>       fprintf (out, "i = %d, x = %f, name = \"%.50s\"\n", i, x, name);
>       if (i != 56 || x != 789.0F || strcmp (name, "56"))
>         {
> @@ -164,7 +169,12 @@ main (int argc, char **argv)
>   	quant = 0.0;
>   	units[0] = item[0] = '\0';
>   	count = fscanf (in, "%f%20s of %20s", &quant, units, item);
> -	(void) fscanf (in, "%*[^\n]");
> +	if (fscanf (in, "%*[^\n]") < 0 && ferror (in))
> +	  {
> +	    fputs ("test failed!\n", stdout);
> +	    result = 1;
> +	  }
> +
>   	fprintf (out, "count = %d, quant = %f, item = %.21s, units = %.21s\n",
>   		 count, quant, item, units);
>   	if (count != ok[rounds-1].count || quant != ok[rounds-1].quant
> diff --git a/stdlib/test-canon.c b/stdlib/test-canon.c
> index 5a2e7e1e6e..407ad5c4a2 100644
> --- a/stdlib/test-canon.c
> +++ b/stdlib/test-canon.c
> @@ -123,8 +123,13 @@ do_test (int argc, char ** argv)
>     int i, errors = 0;
>     char buf[PATH_MAX];
>   
> -  getcwd (cwd, sizeof (buf));
> -  cwd_len = strlen (cwd);
> +  if (getcwd (cwd, sizeof (buf)))
> +    cwd_len = strlen (cwd);
> +  else
> +    {
> +      printf ("%s: current working directory couldn't be retrieved\n", argv[0]);
> +      ++errors;
> +    }
>   
>     errno = 0;
>     if (realpath (NULL, buf) != NULL || errno != EINVAL)
> @@ -210,7 +215,12 @@ do_test (int argc, char ** argv)
>         free (result2);
>       }
>   
> -  getcwd (buf, sizeof (buf));
> +  if (!getcwd (buf, sizeof (buf)))
> +    {
> +      printf ("%s: current working directory couldn't be retrieved\n", argv[0]);
> +      ++errors;
> +    }
> +
>     if (strcmp (buf, cwd))
>       {
>         printf ("%s: current working directory changed from %s to %s\n",
> diff --git a/support/test-container.c b/support/test-container.c
> index 37beb778d6..f51afefedb 100644
> --- a/support/test-container.c
> +++ b/support/test-container.c
> @@ -714,8 +714,8 @@ check_for_unshare_hints (int require_pidns)
>           continue;
>   
>         val = -1; /* Sentinel.  */
> -      fscanf (f, "%d", &val);
> -      if (val != files[i].bad_value)
> +      int cnt = fscanf (f, "%d", &val);
> +      if (cnt == 1 && val != files[i].bad_value)
>   	continue;
>   
>         printf ("To enable test-container, please run this as root:\n");
> diff --git a/sysdeps/pthread/tst-cancel16.c b/sysdeps/pthread/tst-cancel16.c
> index 511b9e1e91..d47c7c68cb 100644
> --- a/sysdeps/pthread/tst-cancel16.c
> +++ b/sysdeps/pthread/tst-cancel16.c
> @@ -50,7 +50,11 @@ tf (void *arg)
>     pthread_cleanup_push (cl, NULL);
>   
>     /* This call should never return.  */
> -  (void) lockf (fd, F_LOCK, 0);
> +  if (lockf (fd, F_LOCK, 0))
> +    {
> +      puts ("child thread: lockf failed");
> +      exit (1);
> +    }
>   
>     pthread_cleanup_pop (0);
>   
> diff --git a/sysdeps/pthread/tst-cancel4.c b/sysdeps/pthread/tst-cancel4.c
> index 4f5c89314c..4c9e8670ca 100644
> --- a/sysdeps/pthread/tst-cancel4.c
> +++ b/sysdeps/pthread/tst-cancel4.c
> @@ -1009,7 +1009,8 @@ tf_pread (void *arg)
>     pthread_cleanup_push (cl, NULL);
>   
>     char mem[10];
> -  pread (tempfd, mem, sizeof (mem), 0);
> +  if (pread (tempfd, mem, sizeof (mem), 0) < 0)
> +    FAIL_EXIT1 ("pread failed: %m");
>   
>     pthread_cleanup_pop (0);
>   
> @@ -1038,7 +1039,8 @@ tf_pwrite (void *arg)
>     pthread_cleanup_push (cl, NULL);
>   
>     char mem[10];
> -  pwrite (tempfd, mem, sizeof (mem), 0);
> +  if (pwrite (tempfd, mem, sizeof (mem), 0) <0)
> +    FAIL_EXIT1 ("pwrite failed: %m");
>   
>     pthread_cleanup_pop (0);
>   

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH] Move {read,write}_all functions to a dedicated header
  2023-06-01 15:55       ` [PATCH] Move {read,write}_all functions to a dedicated header Frédéric Bérat
@ 2023-06-01 17:07         ` Siddhesh Poyarekar
  2023-06-02  6:10           ` Frederic Berat
  0 siblings, 1 reply; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-01 17:07 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha

On 2023-06-01 11:55, Frédéric Bérat wrote:
> 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.
> ---
>   catgets/gencat.c      | 22 +-------------------
>   include/unistd_ext.h  | 48 +++++++++++++++++++++++++++++++++++++++++++
>   malloc/memusage.c     | 25 +---------------------
>   malloc/memusagestat.c | 40 +-----------------------------------
>   4 files changed, 51 insertions(+), 84 deletions(-)
>   create mode 100644 include/unistd_ext.h

This should ideally have been sent in with the rest of the series, 
otherwise it simply breaks pre-commit CI testing:

https://patchwork.sourceware.org/project/glibc/patch/20230601155556.104745-1-fberat@redhat.com/

> 
> 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 <stdlib.h>
>   #include <string.h>
>   #include <unistd.h>
> +#include <unistd_ext.h>
>   #include <wchar.h>
>   
>   #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..b6ce067efb
> --- /dev/null
> +++ b/include/unistd_ext.h

This needs a copyright header.

> @@ -0,0 +1,48 @@
> +#ifndef _UNISTD_EXT_H
> +#define _UNISTD_EXT_H
> +
> +#include <error.h>
> +#include <errno.h>
> +#include <libintl.h>
> +#include <unistd.h>
> +
> +/* 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..1c6930188c 100644
> --- a/malloc/memusage.c
> +++ b/malloc/memusage.c
> @@ -18,10 +18,7 @@
>   
>   #include <assert.h>
>   #include <dlfcn.h>
> -#include <errno.h>
> -#include <error.h>
>   #include <fcntl.h>
> -#include <libintl.h>

You shouldn't remove these headers because gettext gets used despite the 
unistd_ext.h usage, resulting in an implicit, transitive dependency. 
Does it break anything if you don't remove these includes?

>   #include <stdatomic.h>
>   #include <stdbool.h>
>   #include <stdio.h>
> @@ -30,6 +27,7 @@
>   #include <sys/mman.h>
>   #include <sys/time.h>
>   #include <unistd.h>
> +#include <unistd_ext.h>
>   
>   #include <hp-timing.h>
>   #include <machine-sp.h>
> @@ -145,27 +143,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 <stdlib.h>
>   #include <string.h>
>   #include <unistd.h>
> +#include <unistd_ext.h>
>   #include <stdint.h>
>   #include <sys/param.h>
>   #include <sys/stat.h>
> @@ -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[])
>   {

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH] Move {read,write}_all functions to a dedicated header
  2023-06-01 17:07         ` Siddhesh Poyarekar
@ 2023-06-02  6:10           ` Frederic Berat
  2023-06-02 10:01             ` Siddhesh Poyarekar
  0 siblings, 1 reply; 109+ messages in thread
From: Frederic Berat @ 2023-06-02  6:10 UTC (permalink / raw)
  To: Siddhesh Poyarekar; +Cc: libc-alpha

On Thu, Jun 1, 2023 at 7:08 PM Siddhesh Poyarekar <siddhesh@gotplt.org> wrote:
>
> On 2023-06-01 11:55, Frédéric Bérat wrote:
> > 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.
> > ---
> >   catgets/gencat.c      | 22 +-------------------
> >   include/unistd_ext.h  | 48 +++++++++++++++++++++++++++++++++++++++++++
> >   malloc/memusage.c     | 25 +---------------------
> >   malloc/memusagestat.c | 40 +-----------------------------------
> >   4 files changed, 51 insertions(+), 84 deletions(-)
> >   create mode 100644 include/unistd_ext.h
>
> This should ideally have been sent in with the rest of the series,
> otherwise it simply breaks pre-commit CI testing:
>
> https://patchwork.sourceware.org/project/glibc/patch/20230601155556.104745-1-fberat@redhat.com/
>

Arf, I will take note for later :(

> >
> > 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 <stdlib.h>
> >   #include <string.h>
> >   #include <unistd.h>
> > +#include <unistd_ext.h>
> >   #include <wchar.h>
> >
> >   #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..b6ce067efb
> > --- /dev/null
> > +++ b/include/unistd_ext.h
>
> This needs a copyright header.
>

What's the rule for that ? It's a bit confusing since there isn't a
copyright header in most of the headers in "include", so I assumed it
didn't need it.

> > @@ -0,0 +1,48 @@
> > +#ifndef _UNISTD_EXT_H
> > +#define _UNISTD_EXT_H
> > +
> > +#include <error.h>
> > +#include <errno.h>
> > +#include <libintl.h>
> > +#include <unistd.h>
> > +
> > +/* 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..1c6930188c 100644
> > --- a/malloc/memusage.c
> > +++ b/malloc/memusage.c
> > @@ -18,10 +18,7 @@
> >
> >   #include <assert.h>
> >   #include <dlfcn.h>
> > -#include <errno.h>
> > -#include <error.h>
> >   #include <fcntl.h>
> > -#include <libintl.h>
>
> You shouldn't remove these headers because gettext gets used despite the
> unistd_ext.h usage, resulting in an implicit, transitive dependency.
> Does it break anything if you don't remove these includes?
>

Nothing breaks if I keep these headers.
I actually removed them because they were added in the patch that
introduced write_all/read_all.
Since I moved write/read_all in the header, I removed the headers that
were introduced with them, but I admit I didn't check if they were
actually missing before that.

> >   #include <stdatomic.h>
> >   #include <stdbool.h>
> >   #include <stdio.h>
> > @@ -30,6 +27,7 @@
> >   #include <sys/mman.h>
> >   #include <sys/time.h>
> >   #include <unistd.h>
> > +#include <unistd_ext.h>
> >
> >   #include <hp-timing.h>
> >   #include <machine-sp.h>
> > @@ -145,27 +143,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 <stdlib.h>
> >   #include <string.h>
> >   #include <unistd.h>
> > +#include <unistd_ext.h>
> >   #include <stdint.h>
> >   #include <sys/param.h>
> >   #include <sys/stat.h>
> > @@ -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[])
> >   {
>


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH] Move {read,write}_all functions to a dedicated header
  2023-06-02  6:10           ` Frederic Berat
@ 2023-06-02 10:01             ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-02 10:01 UTC (permalink / raw)
  To: Frederic Berat; +Cc: libc-alpha

On 2023-06-02 02:10, Frederic Berat wrote:
>>
>> This needs a copyright header.
>>
> 
> What's the rule for that ? It's a bit confusing since there isn't a
> copyright header in most of the headers in "include", so I assumed it
> didn't need it.

I don't know if there's a rule for it, but AFAICT, most recently added 
files appear to have the copyright header.

> 
>>> @@ -0,0 +1,48 @@
>>> +#ifndef _UNISTD_EXT_H
>>> +#define _UNISTD_EXT_H
>>> +
>>> +#include <error.h>
>>> +#include <errno.h>
>>> +#include <libintl.h>
>>> +#include <unistd.h>
>>> +
>>> +/* 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..1c6930188c 100644
>>> --- a/malloc/memusage.c
>>> +++ b/malloc/memusage.c
>>> @@ -18,10 +18,7 @@
>>>
>>>    #include <assert.h>
>>>    #include <dlfcn.h>
>>> -#include <errno.h>
>>> -#include <error.h>
>>>    #include <fcntl.h>
>>> -#include <libintl.h>
>>
>> You shouldn't remove these headers because gettext gets used despite the
>> unistd_ext.h usage, resulting in an implicit, transitive dependency.
>> Does it break anything if you don't remove these includes?
>>
> 
> Nothing breaks if I keep these headers.
> I actually removed them because they were added in the patch that
> introduced write_all/read_all.
> Since I moved write/read_all in the header, I removed the headers that
> were introduced with them, but I admit I didn't check if they were
> actually missing before that.

OK, then it makes sense to just leave them in I think, since other code 
in this file needs them.

Thanks,
Sid

^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v6 0/7] Fix warn unused result
  2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
                   ` (8 preceding siblings ...)
  2023-04-28 12:21 ` [PATCH v4 00/15] Fix " Frédéric Bérat
@ 2023-06-02 15:28 ` Frédéric Bérat
  2023-06-02 15:28   ` [PATCH v6 1/7] tests: fix warn unused result on asprintf calls Frédéric Bérat
                     ` (6 more replies)
  2023-06-12 15:18 ` [PATCH v7 0/4] Fix warn unused result Frédéric Bérat
  10 siblings, 7 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-02 15:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Hello,

Enabling _FORTIFY_SOURCE on Glibc itself implies that some function get the
__wur macro enabed.
This leads to build failures when -Werror is set.

I went for multiple patches for these fixes, a big one that cover the tests,
and then separate one for the other ones, assuming they may need to get a
closer look at.

While most of the changes look trivial, decision I made on how to fix them may
be open to debate, e.g. the additional checks may lead to failures while they
weren't earlier, some other have no impact while it may be useful to have one.

Feedback is therefore highly appreciated.

Fred.

Changes since v1:
 - "tests: fix warn unused result" has been split into several sub-patches. New
   x-variant of functions have been created when necessary.
 - "inet/rcmd.c" is removed has it has been merged upstream.
 - catgets/gencat.c:
   - introduced a static write_all routine, that mimics support/xwrite
   - fixed review findings (declaration of array_size where defined)
 - locale/programs/locarchive.c: untouched (I forgot it)
 - malloc/{memusage.c,memusagestat.c}: introduced write_all/read_all functions
 - nptl_db/thread_dbP.h: fixed missing white space.
 - sunrpc/netname.c: fixed missing whitespace, and "return 0;" indent.
 - sysdeps/pthread/eintr.c: Added comment on why the if statement is used.

Changes since v2:
 - Rebased on master
 - catgets/gencat.c: Don't use errno if write returns 0.
 - locale/programs/locarchive.c: Freeing normalized_codeset before returning.
 - malloc/{memusage.c,memusagestat.c}: Don't use errno if write returns 0.
 - tests: replace read by xread:
   - Add missing xunistd.h includes
   - Add libsupport to tst-fini1mod.so
 - tests: replace fread by xfread:
   - Fixed xfread prototype
   - Add missing xstdio.h include

Changes since v3:
 - Rebased on master

Changes since v4:
  gencat/memusage*:
  - Mark strings for translation with gettext.
  - Fixed typo in (ret == 0) case
  nptl_db/thread_dbP.h: Wrapped line since it exceeded 79 characters
 tests:
 - Properly use xasprintf in all cases in posix/tst-execlp2.c
 - Added more fail case in xsystem
 - Clarify why some system calls should not use xsystem
 - Removed extra curly brackets
 Added a patch to move {read,write}_all functions to a dedicated header

Changes since v5:
 tests:
 - Fixed missing headers in {nptl,elf}/tst-stackguard1.c
 - Fixed date in copyrights
 - Removed unnecessary check after xasprintf call
 - Rebasing patches
 - Actually replace functions with their x variant as described
 Adding copyright to unistd_ext.h

---
Frédéric Bérat (7):
  tests: fix warn unused result on asprintf calls
  tests: replace read by xread
  tests: replace system by xsystem
  tests: replace fread by xfread
  tests: replace fgets by xfgets
  tests: Replace various function calls with their x variant
  Move {read,write}_all functions to a dedicated header

 argp/argp-test.c               |  8 +++--
 assert/test-assert-perr.c      |  8 +++--
 assert/test-assert.c           |  8 +++--
 catgets/gencat.c               | 22 +-----------
 dirent/tst-fdopendir.c         |  3 +-
 elf/tst-stackguard1.c          |  5 ++-
 include/unistd_ext.h           | 66 ++++++++++++++++++++++++++++++++++
 libio/bug-fseek.c              |  7 ++--
 libio/bug-mmap-fflush.c        |  7 ++--
 malloc/memusage.c              | 22 +-----------
 malloc/memusagestat.c          | 40 +--------------------
 misc/tst-error1.c              |  4 ++-
 nptl/tst-cancel7.c             |  3 +-
 nptl/tst-cleanup4.c            |  4 ++-
 nptl/tst-stackguard1.c         |  4 ++-
 nss/tst-nss-db-endpwent.c      |  3 +-
 nss/tst-reload2.c              |  2 +-
 posix/tst-chmod.c              |  7 ++--
 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 +++------
 posix/wordexp-test.c           |  2 +-
 stdio-common/bug12.c           | 12 ++++---
 stdio-common/bug3.c            |  4 ++-
 stdio-common/bug4.c            |  4 ++-
 stdio-common/bug5.c            |  4 ++-
 stdio-common/test-fwrite.c     |  4 ++-
 stdio-common/test_rdwr.c       | 11 +++---
 stdio-common/tst-cookie.c      |  5 ++-
 stdio-common/tst-fmemopen3.c   |  4 ++-
 stdio-common/tst-fseek.c       |  5 ++-
 stdlib/test-canon.c            |  4 ++-
 support/Makefile               |  4 +++
 support/test-container.c       |  3 +-
 support/xfgets.c               | 32 +++++++++++++++++
 support/xfread.c               | 39 ++++++++++++++++++++
 support/xread.c                | 36 +++++++++++++++++++
 support/xstdio.h               |  2 ++
 support/xstdlib.h              | 31 ++++++++++++++++
 support/xsystem.c              | 37 +++++++++++++++++++
 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-cancel6.c  |  3 +-
 sysdeps/pthread/tst-fini1mod.c |  4 ++-
 50 files changed, 370 insertions(+), 197 deletions(-)
 create mode 100644 include/unistd_ext.h
 create mode 100644 support/xfgets.c
 create mode 100644 support/xfread.c
 create mode 100644 support/xread.c
 create mode 100644 support/xstdlib.h
 create mode 100644 support/xsystem.c

-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v6 1/7] tests: fix warn unused result on asprintf calls
  2023-06-02 15:28 ` [PATCH v6 0/7] " Frédéric Bérat
@ 2023-06-02 15:28   ` Frédéric Bérat
  2023-06-06  6:18     ` Siddhesh Poyarekar
  2023-06-02 15:28   ` [PATCH v6 2/7] tests: replace read by xread Frédéric Bérat
                     ` (5 subsequent siblings)
  6 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-02 15:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

When enabling _FORTIFY_SOURCE, some functions now lead to warnings when
their result is not checked.
---
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 <string.h>
 #include <argp.h>
 
+#include <support/support.h>
+
 const char *argp_program_version = "argp-test 1.0";
 \f
 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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/support.h>
+
 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 <stdio.h>
 #include <string.h>
 
+#include <support/support.h>
+
 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 <time.h>
 #include <sys/stat.h>
 
+#include <support/support.h>
 
 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);
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v6 2/7] tests: replace read by xread
  2023-06-02 15:28 ` [PATCH v6 0/7] " Frédéric Bérat
  2023-06-02 15:28   ` [PATCH v6 1/7] tests: fix warn unused result on asprintf calls Frédéric Bérat
@ 2023-06-02 15:28   ` Frédéric Bérat
  2023-06-06  6:21     ` Siddhesh Poyarekar
  2023-06-07 19:04     ` Frederic Berat
  2023-06-02 15:28   ` [PATCH v6 3/7] tests: replace system by xsystem Frédéric Bérat
                     ` (4 subsequent siblings)
  6 siblings, 2 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-02 15:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 /* 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xunistd.h>
+
+#include <support/check.h>
+
+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 <string.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 
 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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 
 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 <sys/wait.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
 
 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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 
 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);
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v6 3/7] tests: replace system by xsystem
  2023-06-02 15:28 ` [PATCH v6 0/7] " Frédéric Bérat
  2023-06-02 15:28   ` [PATCH v6 1/7] tests: fix warn unused result on asprintf calls Frédéric Bérat
  2023-06-02 15:28   ` [PATCH v6 2/7] tests: replace read by xread Frédéric Bérat
@ 2023-06-02 15:28   ` Frédéric Bérat
  2023-06-06 12:17     ` Siddhesh Poyarekar
  2023-06-02 15:28   ` [PATCH v6 4/7] tests: replace fread by xfread Frédéric Bérat
                     ` (3 subsequent siblings)
  6 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-02 15:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

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 <tls.h>
 #include <unistd.h>
 
+#include <support/xstdlib.h>
+
 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 <stdlib.h>
 #include <string.h>
 
+#include <support/xstdlib.h>
 
 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 <support/support.h>
 #include <support/temp_file.h>
 #include <support/xstdio.h>
+#include <support/xstdlib.h>
 #include <support/xunistd.h>
 #include <support/xthread.h>
 
@@ -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 <unistd.h>
 
 #include <support/xunistd.h>
+#include <support/xstdlib.h>
 
 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 <support/support.h>
 #include <support/check.h>
+#include <support/xstdlib.h>
 
 /* 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
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef SUPPORT_XSTDLIB_H
+#define SUPPORT_XSTDLIB_H
+
+#include <stdlib.h>
+#include <sys/cdefs.h>
+
+__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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/support.h>
+#include <support/check.h>
+
+#include <support/xstdlib.h>
+
+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);
+}
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v6 4/7] tests: replace fread by xfread
  2023-06-02 15:28 ` [PATCH v6 0/7] " Frédéric Bérat
                     ` (2 preceding siblings ...)
  2023-06-02 15:28   ` [PATCH v6 3/7] tests: replace system by xsystem Frédéric Bérat
@ 2023-06-02 15:28   ` Frédéric Bérat
  2023-06-06 12:18     ` Siddhesh Poyarekar
  2023-06-07 19:03     ` Frederic Berat
  2023-06-02 15:28   ` [PATCH v6 5/7] tests: replace fgets by xfgets Frédéric Bérat
                     ` (2 subsequent siblings)
  6 siblings, 2 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-02 15:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xstdio.h>
 
 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 <stdio.h>
 #include <string.h>
 
+#include <support/xstdio.h>
+
 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 <stdio.h>
 #include <string.h>
 
+#include <support/xstdio.h>
+
 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 <unistd.h>
 #include <string.h>
 
+#include <support/xstdio.h>
+
 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 <stdio.h>
 
+#include <support/xstdio.h>
+
 
 #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 <string.h>
 #include <sys/types.h>
 
+#include <support/xstdio.h>
+
 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xstdio.h>
+
+#include <support/check.h>
+#include <stdlib.h>
+
+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
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v6 5/7] tests: replace fgets by xfgets
  2023-06-02 15:28 ` [PATCH v6 0/7] " Frédéric Bérat
                     ` (3 preceding siblings ...)
  2023-06-02 15:28   ` [PATCH v6 4/7] tests: replace fread by xfread Frédéric Bérat
@ 2023-06-02 15:28   ` Frédéric Bérat
  2023-06-06 12:20     ` Siddhesh Poyarekar
  2023-06-08  5:50     ` Maxim Kuvyrkov
  2023-06-02 15:28   ` [PATCH v6 6/7] tests: Replace various function calls with their x variant Frédéric Bérat
  2023-06-02 15:28   ` [PATCH v6 7/7] Move {read,write}_all functions to a dedicated header Frédéric Bérat
  6 siblings, 2 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-02 15:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

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 <string.h>
 #include <setjmp.h>
 
+#include <support/xstdio.h>
+
 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 <string.h>
 #include <setjmp.h>
 
+#include <support/xstdio.h>
+
 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 <stdlib.h>
 #include <string.h>
 
+#include <support/xstdio.h>
 
 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xstdio.h>
+
+#include <support/check.h>
+#include <stdlib.h>
+
+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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xstdio.h>
 
 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;
 }
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v6 6/7] tests: Replace various function calls with their x variant
  2023-06-02 15:28 ` [PATCH v6 0/7] " Frédéric Bérat
                     ` (4 preceding siblings ...)
  2023-06-02 15:28   ` [PATCH v6 5/7] tests: replace fgets by xfgets Frédéric Bérat
@ 2023-06-02 15:28   ` Frédéric Bérat
  2023-06-06 12:20     ` Siddhesh Poyarekar
  2023-06-02 15:28   ` [PATCH v6 7/7] Move {read,write}_all functions to a dedicated header Frédéric Bérat
  6 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-02 15:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

With fortification enabled, few function calls return result need to be
checked, has they get the __wur macro enabled.
---
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 <wchar.h>
 #include <libc-diag.h>
 
+#include <support/xstdio.h>
+
 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 <unistd.h>
 #include <sys/stat.h>
 
+#include <support/xunistd.h>
+
 
 #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 <sys/param.h>
 #include <sys/stat.h>
 
+#include <support/xunistd.h>
+
 /* Prototype for our test function.  */
 extern int do_test (int argc, char *argv[]);
 #include <test-skeleton.c>
@@ -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;
 
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v6 7/7] Move {read,write}_all functions to a dedicated header
  2023-06-02 15:28 ` [PATCH v6 0/7] " Frédéric Bérat
                     ` (5 preceding siblings ...)
  2023-06-02 15:28   ` [PATCH v6 6/7] tests: Replace various function calls with their x variant Frédéric Bérat
@ 2023-06-02 15:28   ` Frédéric Bérat
  2023-06-06 12:21     ` Siddhesh Poyarekar
  6 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-02 15:28 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

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.
---
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 <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <unistd_ext.h>
 #include <wchar.h>
 
 #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
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef _UNISTD_EXT_H
+#define _UNISTD_EXT_H
+
+#include <error.h>
+#include <errno.h>
+#include <libintl.h>
+#include <unistd.h>
+
+/* 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 <sys/mman.h>
 #include <sys/time.h>
 #include <unistd.h>
+#include <unistd_ext.h>
 
 #include <hp-timing.h>
 #include <machine-sp.h>
@@ -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 <stdlib.h>
 #include <string.h>
 #include <unistd.h>
+#include <unistd_ext.h>
 #include <stdint.h>
 #include <sys/param.h>
 #include <sys/stat.h>
@@ -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[])
 {
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 1/7] tests: fix warn unused result on asprintf calls
  2023-06-02 15:28   ` [PATCH v6 1/7] tests: fix warn unused result on asprintf calls Frédéric Bérat
@ 2023-06-06  6:18     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-06  6:18 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-02 11:28, Frédéric Bérat wrote:
> When enabling _FORTIFY_SOURCE, some functions now lead to warnings when
> their result is not checked.
> ---
> Changes since v5:
>   - Removed unnecessary check after xasprintf call

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
>   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 <string.h>
>   #include <argp.h>
>   
> +#include <support/support.h>
> +
>   const char *argp_program_version = "argp-test 1.0";
>   \f
>   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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/support.h>
> +
>   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 <stdio.h>
>   #include <string.h>
>   
> +#include <support/support.h>
> +
>   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 <time.h>
>   #include <sys/stat.h>
>   
> +#include <support/support.h>
>   
>   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);

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 2/7] tests: replace read by xread
  2023-06-02 15:28   ` [PATCH v6 2/7] tests: replace read by xread Frédéric Bérat
@ 2023-06-06  6:21     ` Siddhesh Poyarekar
  2023-06-06  8:00       ` Frederic Berat
  2023-06-07 19:04     ` Frederic Berat
  1 sibling, 1 reply; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-06  6:21 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-02 11:28, Frédéric Bérat wrote:
> 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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
> +
>   /* 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.

Only 2023.

> +   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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/xunistd.h>
> +
> +#include <support/check.h>
> +
> +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 <string.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
> +
>   
>   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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
> +
>   
>   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);

Uhmm, isn't read success a failure here?

>   
>     pthread_cleanup_pop (0);
>   }
> @@ -84,8 +82,6 @@ tf_body (void)
>         exit (1);
>       }
>   
> -  read (fd[0], &c, 1);
> -

No replacement for this read call?

>     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 <sys/wait.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
>   
>   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);

Likewise, read success should be a failure?

>   
>     pthread_cleanup_pop (0);
>   }
> @@ -85,8 +82,6 @@ tf_body (void)
>         exit (1);
>       }
>   
> -  read (fd[0], &c, 1);
> -

Missing xread replacement.

>     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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
> +
>   
>   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);

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 2/7] tests: replace read by xread
  2023-06-06  6:21     ` Siddhesh Poyarekar
@ 2023-06-06  8:00       ` Frederic Berat
  2023-06-12 14:22         ` Siddhesh Poyarekar
  0 siblings, 1 reply; 109+ messages in thread
From: Frederic Berat @ 2023-06-06  8:00 UTC (permalink / raw)
  To: Siddhesh Poyarekar; +Cc: libc-alpha

On Tue, Jun 6, 2023 at 8:21 AM Siddhesh Poyarekar <siddhesh@gotplt.org> wrote:
>
>
>
> On 2023-06-02 11:28, Frédéric Bérat wrote:
> > 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 <stdlib.h>
> >   #include <unistd.h>
> >
> > +#include <support/xunistd.h>
> > +
> >   /* 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.
>
> Only 2023.
>
> > +   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
> > +   <https://www.gnu.org/licenses/>.  */
> > +
> > +#include <support/xunistd.h>
> > +
> > +#include <support/check.h>
> > +
> > +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 <string.h>
> >   #include <unistd.h>
> >
> > +#include <support/xunistd.h>
> > +
> >
> >   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 <stdlib.h>
> >   #include <unistd.h>
> >
> > +#include <support/xunistd.h>
> > +
> >
> >   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);
>
> Uhmm, isn't read success a failure here?

Yep, I went too far on v2 when starting to use xread.

>
> >
> >     pthread_cleanup_pop (0);
> >   }
> > @@ -84,8 +82,6 @@ tf_body (void)
> >         exit (1);
> >       }
> >
> > -  read (fd[0], &c, 1);
> > -
>
> No replacement for this read call?

On my original change, I couldn't find any reason for this call to exist.
As far as I understand, pipes are open blocking by default, which
means read(fd[2]) should block until anything is written to fd[3].
Since there is no write, "read(fd[0])" should never be executed, and
cancellation should hit on "read(fd[2])".

If the first read succeeds, the test should fail.
If the thread isn't marked as cancelled and simply continues its
execution, the test should fail.

Since the potential race between the close and the cancel got fixed by
d0e3ffb7a58854248f1d5e737610d50cd0a60f46, I assume the second read is
superfluous.

Yet, I may have misunderstood the intent on this second read, in which
case some comment would be useful.

>
> >     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 <sys/wait.h>
> >   #include <unistd.h>
> >
> > +#include <support/xunistd.h>
> >
> >   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);
>
> Likewise, read success should be a failure?
>
> >
> >     pthread_cleanup_pop (0);
> >   }
> > @@ -85,8 +82,6 @@ tf_body (void)
> >         exit (1);
> >       }
> >
> > -  read (fd[0], &c, 1);
> > -
>
> Missing xread replacement.
>
> >     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 <stdlib.h>
> >   #include <unistd.h>
> >
> > +#include <support/xunistd.h>
> > +
> >
> >   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);
>


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 3/7] tests: replace system by xsystem
  2023-06-02 15:28   ` [PATCH v6 3/7] tests: replace system by xsystem Frédéric Bérat
@ 2023-06-06 12:17     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-06 12:17 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha

On 2023-06-02 11:28, Frédéric Bérat wrote:
> 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 <tls.h>
>   #include <unistd.h>
>   
> +#include <support/xstdlib.h>
> +
>   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 <stdlib.h>
>   #include <string.h>
>   
> +#include <support/xstdlib.h>
>   
>   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 <support/support.h>
>   #include <support/temp_file.h>
>   #include <support/xstdio.h>
> +#include <support/xstdlib.h>
>   #include <support/xunistd.h>
>   #include <support/xthread.h>
>   
> @@ -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 <unistd.h>
>   
>   #include <support/xunistd.h>
> +#include <support/xstdlib.h>
>   
>   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 <support/support.h>
>   #include <support/check.h>
> +#include <support/xstdlib.h>
>   
>   /* 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 \

Incorrect sort order.

>     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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#ifndef SUPPORT_XSTDLIB_H
> +#define SUPPORT_XSTDLIB_H
> +
> +#include <stdlib.h>
> +#include <sys/cdefs.h>
> +
> +__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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/support.h>
> +#include <support/check.h>
> +
> +#include <support/xstdlib.h>
> +
> +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);
> +}

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 4/7] tests: replace fread by xfread
  2023-06-02 15:28   ` [PATCH v6 4/7] tests: replace fread by xfread Frédéric Bérat
@ 2023-06-06 12:18     ` Siddhesh Poyarekar
  2023-06-07 19:03     ` Frederic Berat
  1 sibling, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-06 12:18 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-02 11:28, Frédéric Bérat wrote:
> 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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/xstdio.h>
>   
>   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 <stdio.h>
>   #include <string.h>
>   
> +#include <support/xstdio.h>
> +
>   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 <stdio.h>
>   #include <string.h>
>   
> +#include <support/xstdio.h>
> +
>   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 <unistd.h>
>   #include <string.h>
>   
> +#include <support/xstdio.h>
> +
>   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 <stdio.h>
>   
> +#include <support/xstdio.h>
> +
>   
>   #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 <string.h>
>   #include <sys/types.h>
>   
> +#include <support/xstdio.h>
> +
>   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.

Only 2023.

> +   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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/xstdio.h>
> +
> +#include <support/check.h>
> +#include <stdlib.h>
> +
> +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

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 5/7] tests: replace fgets by xfgets
  2023-06-02 15:28   ` [PATCH v6 5/7] tests: replace fgets by xfgets Frédéric Bérat
@ 2023-06-06 12:20     ` Siddhesh Poyarekar
  2023-06-08  5:50     ` Maxim Kuvyrkov
  1 sibling, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-06 12:20 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-02 11:28, Frédéric Bérat wrote:
> 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 <string.h>
>   #include <setjmp.h>
>   
> +#include <support/xstdio.h>
> +
>   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 <string.h>
>   #include <setjmp.h>
>   
> +#include <support/xstdio.h>
> +
>   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 <stdlib.h>
>   #include <string.h>
>   
> +#include <support/xstdio.h>
>   
>   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.

Only 2023.

> +   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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/xstdio.h>
> +
> +#include <support/check.h>
> +#include <stdlib.h>
> +
> +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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/xstdio.h>
>   
>   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;
>   }

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 6/7] tests: Replace various function calls with their x variant
  2023-06-02 15:28   ` [PATCH v6 6/7] tests: Replace various function calls with their x variant Frédéric Bérat
@ 2023-06-06 12:20     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-06 12:20 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-02 11:28, Frédéric Bérat wrote:
> With fortification enabled, few function calls return result need to be
> checked, has they get the __wur macro enabled.
> ---
> Changes since v5:
>   - Actually replace functions with their x variant as described

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
>   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 <wchar.h>
>   #include <libc-diag.h>
>   
> +#include <support/xstdio.h>
> +
>   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 <unistd.h>
>   #include <sys/stat.h>
>   
> +#include <support/xunistd.h>
> +
>   
>   #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 <sys/param.h>
>   #include <sys/stat.h>
>   
> +#include <support/xunistd.h>
> +
>   /* Prototype for our test function.  */
>   extern int do_test (int argc, char *argv[]);
>   #include <test-skeleton.c>
> @@ -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;
>   

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 7/7] Move {read,write}_all functions to a dedicated header
  2023-06-02 15:28   ` [PATCH v6 7/7] Move {read,write}_all functions to a dedicated header Frédéric Bérat
@ 2023-06-06 12:21     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-06 12:21 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-02 11:28, Frédéric Bérat wrote:
> 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.
> ---
> Changes since v5:
>   - Adding copyright to unistd_ext.h
>   - Don't remove included headers in memusagestat.c

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
>   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 <stdlib.h>
>   #include <string.h>
>   #include <unistd.h>
> +#include <unistd_ext.h>
>   #include <wchar.h>
>   
>   #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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#ifndef _UNISTD_EXT_H
> +#define _UNISTD_EXT_H
> +
> +#include <error.h>
> +#include <errno.h>
> +#include <libintl.h>
> +#include <unistd.h>
> +
> +/* 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 <sys/mman.h>
>   #include <sys/time.h>
>   #include <unistd.h>
> +#include <unistd_ext.h>
>   
>   #include <hp-timing.h>
>   #include <machine-sp.h>
> @@ -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 <stdlib.h>
>   #include <string.h>
>   #include <unistd.h>
> +#include <unistd_ext.h>
>   #include <stdint.h>
>   #include <sys/param.h>
>   #include <sys/stat.h>
> @@ -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[])
>   {

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 4/7] tests: replace fread by xfread
  2023-06-02 15:28   ` [PATCH v6 4/7] tests: replace fread by xfread Frédéric Bérat
  2023-06-06 12:18     ` Siddhesh Poyarekar
@ 2023-06-07 19:03     ` Frederic Berat
  1 sibling, 0 replies; 109+ messages in thread
From: Frederic Berat @ 2023-06-07 19:03 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh

On Fri, Jun 2, 2023 at 5:28 PM Frédéric Bérat <fberat@redhat.com> wrote:
>
> 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 <stdlib.h>
>  #include <unistd.h>
>
> +#include <support/xstdio.h>
>
>  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 <stdio.h>
>  #include <string.h>
>
> +#include <support/xstdio.h>
> +
>  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 <stdio.h>
>  #include <string.h>
>
> +#include <support/xstdio.h>
> +
>  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 <unistd.h>
>  #include <string.h>
>
> +#include <support/xstdio.h>
> +
>  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 <stdio.h>
>
> +#include <support/xstdio.h>
> +
>
>  #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 <string.h>
>  #include <sys/types.h>
>
> +#include <support/xstdio.h>
> +
>  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 \

xfread must be before xfreopen

>    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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/xstdio.h>
> +
> +#include <support/check.h>
> +#include <stdlib.h>
> +
> +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
> --
> 2.40.1
>


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 2/7] tests: replace read by xread
  2023-06-02 15:28   ` [PATCH v6 2/7] tests: replace read by xread Frédéric Bérat
  2023-06-06  6:21     ` Siddhesh Poyarekar
@ 2023-06-07 19:04     ` Frederic Berat
  1 sibling, 0 replies; 109+ messages in thread
From: Frederic Berat @ 2023-06-07 19:04 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh

On Fri, Jun 2, 2023 at 5:28 PM Frédéric Bérat <fberat@redhat.com> wrote:
>
> 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 <stdlib.h>
>  #include <unistd.h>
>
> +#include <support/xunistd.h>
> +
>  /* 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 \

xread must be mefore xreadlink

>    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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/xunistd.h>
> +
> +#include <support/check.h>
> +
> +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 <string.h>
>  #include <unistd.h>
>
> +#include <support/xunistd.h>
> +
>
>  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 <stdlib.h>
>  #include <unistd.h>
>
> +#include <support/xunistd.h>
> +
>
>  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 <sys/wait.h>
>  #include <unistd.h>
>
> +#include <support/xunistd.h>
>
>  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 <stdlib.h>
>  #include <unistd.h>
>
> +#include <support/xunistd.h>
> +
>
>  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);
> --
> 2.40.1
>


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 5/7] tests: replace fgets by xfgets
  2023-06-02 15:28   ` [PATCH v6 5/7] tests: replace fgets by xfgets Frédéric Bérat
  2023-06-06 12:20     ` Siddhesh Poyarekar
@ 2023-06-08  5:50     ` Maxim Kuvyrkov
  2023-06-08  6:57       ` Frederic Berat
  1 sibling, 1 reply; 109+ messages in thread
From: Maxim Kuvyrkov @ 2023-06-08  5:50 UTC (permalink / raw)
  To: Frédéric Bérat; +Cc: Maxim Kuvyrkov via Libc-alpha, siddhesh

Hi Frédéric,

One of the patches in this series fails lint-makefile test [1].  Let me know if you need any assistance in reproducing this.

===
--- /home/maxim.kuvyrkov/tcwg_gnu/glibc/support/Makefile.expected
+++ /home/maxim.kuvyrkov/tcwg_gnu/glibc/support/Makefile	2023-06-08 05:41:24.238665318 +0000
@@ -126,8 +126,8 @@
   xfgets \
   xfopen \
   xfork \
-  xfread \
   xfreopen \
+  xfread \
   xftruncate \
   xgetline \
   xgetsockname \
@@ -197,8 +197,8 @@
   xpthread_spin_lock \
   xpthread_spin_unlock \
   xraise \
-  xread \
   xreadlink \
+  xread \
   xrealloc \
   xrecvfrom \
   xsendto \
@@ -211,8 +211,8 @@
   xstrdup \
   xstrndup \
   xsymlink \
-  xsysconf \
   xsystem \
+  xsysconf \
   xunlink \
   xuselocale \
   xwaitpid \
---
Tested 200 Makefiles and 1 were incorrectly sorted
Please use `patch -R -pN` and the output above to correct the sorting
===

[1] https://ci.linaro.org/job/tcwg_glibc_check--master-aarch64-build/321/artifact/artifacts/artifacts.precommit/07-check_regression/fails.sum/*view*/

--
Maxim Kuvyrkov
https://www.linaro.org




> On Jun 2, 2023, at 19:28, Frédéric Bérat via Libc-alpha <libc-alpha@sourceware.org> wrote:
> 
> 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 <string.h>
> #include <setjmp.h>
> 
> +#include <support/xstdio.h>
> +
> 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 <string.h>
> #include <setjmp.h>
> 
> +#include <support/xstdio.h>
> +
> 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 <stdlib.h>
> #include <string.h>
> 
> +#include <support/xstdio.h>
> 
> 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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/xstdio.h>
> +
> +#include <support/check.h>
> +#include <stdlib.h>
> +
> +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 <stdlib.h>
> #include <unistd.h>
> 
> +#include <support/xstdio.h>
> 
> 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;
> }
> -- 
> 2.40.1
> 


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 5/7] tests: replace fgets by xfgets
  2023-06-08  5:50     ` Maxim Kuvyrkov
@ 2023-06-08  6:57       ` Frederic Berat
  0 siblings, 0 replies; 109+ messages in thread
From: Frederic Berat @ 2023-06-08  6:57 UTC (permalink / raw)
  To: Maxim Kuvyrkov; +Cc: Maxim Kuvyrkov via Libc-alpha, siddhesh

On Thu, Jun 8, 2023 at 7:51 AM Maxim Kuvyrkov <maxim.kuvyrkov@linaro.org> wrote:
>
> Hi Frédéric,
>
> One of the patches in this series fails lint-makefile test [1].  Let me know if you need any assistance in reproducing this.

Thanks, yes I noticed that yesterday on patch 2 and 4. Interestingly
the patch 3 didn't but I'll fix it too.

There is another version pending already, I'll send it soon hopefully.

>
> ===
> --- /home/maxim.kuvyrkov/tcwg_gnu/glibc/support/Makefile.expected
> +++ /home/maxim.kuvyrkov/tcwg_gnu/glibc/support/Makefile        2023-06-08 05:41:24.238665318 +0000
> @@ -126,8 +126,8 @@
>    xfgets \
>    xfopen \
>    xfork \
> -  xfread \
>    xfreopen \
> +  xfread \
>    xftruncate \
>    xgetline \
>    xgetsockname \
> @@ -197,8 +197,8 @@
>    xpthread_spin_lock \
>    xpthread_spin_unlock \
>    xraise \
> -  xread \
>    xreadlink \
> +  xread \
>    xrealloc \
>    xrecvfrom \
>    xsendto \
> @@ -211,8 +211,8 @@
>    xstrdup \
>    xstrndup \
>    xsymlink \
> -  xsysconf \
>    xsystem \
> +  xsysconf \
>    xunlink \
>    xuselocale \
>    xwaitpid \
> ---
> Tested 200 Makefiles and 1 were incorrectly sorted
> Please use `patch -R -pN` and the output above to correct the sorting
> ===
>
> [1] https://ci.linaro.org/job/tcwg_glibc_check--master-aarch64-build/321/artifact/artifacts/artifacts.precommit/07-check_regression/fails.sum/*view*/
>
> --
> Maxim Kuvyrkov
> https://www.linaro.org
>
>
>
>
> > On Jun 2, 2023, at 19:28, Frédéric Bérat via Libc-alpha <libc-alpha@sourceware.org> wrote:
> >
> > 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 <string.h>
> > #include <setjmp.h>
> >
> > +#include <support/xstdio.h>
> > +
> > 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 <string.h>
> > #include <setjmp.h>
> >
> > +#include <support/xstdio.h>
> > +
> > 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 <stdlib.h>
> > #include <string.h>
> >
> > +#include <support/xstdio.h>
> >
> > 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
> > +   <https://www.gnu.org/licenses/>.  */
> > +
> > +#include <support/xstdio.h>
> > +
> > +#include <support/check.h>
> > +#include <stdlib.h>
> > +
> > +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 <stdlib.h>
> > #include <unistd.h>
> >
> > +#include <support/xstdio.h>
> >
> > 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;
> > }
> > --
> > 2.40.1
> >
>


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v6 2/7] tests: replace read by xread
  2023-06-06  8:00       ` Frederic Berat
@ 2023-06-12 14:22         ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-12 14:22 UTC (permalink / raw)
  To: Frederic Berat; +Cc: libc-alpha

On 2023-06-06 04:00, Frederic Berat wrote:
>>> -  read (fd[0], &c, 1);
>>> -
>>
>> No replacement for this read call?
> 
> On my original change, I couldn't find any reason for this call to exist.
> As far as I understand, pipes are open blocking by default, which
> means read(fd[2]) should block until anything is written to fd[3].
> Since there is no write, "read(fd[0])" should never be executed, and
> cancellation should hit on "read(fd[2])".
> 
> If the first read succeeds, the test should fail.
> If the thread isn't marked as cancelled and simply continues its
> execution, the test should fail.
> 
> Since the potential race between the close and the cancel got fixed by
> d0e3ffb7a58854248f1d5e737610d50cd0a60f46, I assume the second read is
> superfluous.
> 
> Yet, I may have misunderstood the intent on this second read, in which
> case some comment would be useful.

Sounds reasonable.  Please note this in your git commit log in the next 
version.

Thanks,
Sid

^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v7 0/4] Fix warn unused result
  2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
                   ` (9 preceding siblings ...)
  2023-06-02 15:28 ` [PATCH v6 0/7] " Frédéric Bérat
@ 2023-06-12 15:18 ` Frédéric Bérat
  2023-06-12 15:18   ` [PATCH v7 1/4] tests: replace read by xread Frédéric Bérat
                     ` (3 more replies)
  10 siblings, 4 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-12 15:18 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

Hello,

Enabling _FORTIFY_SOURCE on Glibc itself implies that some function get the
__wur macro enabed.
This leads to build failures when -Werror is set.

I went for multiple patches for these fixes, a big one that cover the tests,
and then separate one for the other ones, assuming they may need to get a
closer look at.

While most of the changes look trivial, decision I made on how to fix them may
be open to debate, e.g. the additional checks may lead to failures while they
weren't earlier, some other have no impact while it may be useful to have one.

Feedback is therefore highly appreciated.

Fred.

Changes since v1:
 - "tests: fix warn unused result" has been split into several sub-patches. New
   x-variant of functions have been created when necessary.
 - "inet/rcmd.c" is removed has it has been merged upstream.
 - catgets/gencat.c:
   - introduced a static write_all routine, that mimics support/xwrite
   - fixed review findings (declaration of array_size where defined)
 - locale/programs/locarchive.c: untouched (I forgot it)
 - malloc/{memusage.c,memusagestat.c}: introduced write_all/read_all functions
 - nptl_db/thread_dbP.h: fixed missing white space.
 - sunrpc/netname.c: fixed missing whitespace, and "return 0;" indent.
 - sysdeps/pthread/eintr.c: Added comment on why the if statement is used.

Changes since v2:
 - Rebased on master
 - catgets/gencat.c: Don't use errno if write returns 0.
 - locale/programs/locarchive.c: Freeing normalized_codeset before returning.
 - malloc/{memusage.c,memusagestat.c}: Don't use errno if write returns 0.
 - tests: replace read by xread:
   - Add missing xunistd.h includes
   - Add libsupport to tst-fini1mod.so
 - tests: replace fread by xfread:
   - Fixed xfread prototype
   - Add missing xstdio.h include

Changes since v3:
 - Rebased on master

Changes since v4:
  gencat/memusage*:
  - Mark strings for translation with gettext.
  - Fixed typo in (ret == 0) case
  nptl_db/thread_dbP.h: Wrapped line since it exceeded 79 characters
 tests:
 - Properly use xasprintf in all cases in posix/tst-execlp2.c
 - Added more fail case in xsystem
 - Clarify why some system calls should not use xsystem
 - Removed extra curly brackets
 Added a patch to move {read,write}_all functions to a dedicated header

Changes since v5:
 tests:
 - Fixed missing headers in {nptl,elf}/tst-stackguard1.c
 - Fixed date in copyrights
 - Removed unnecessary check after xasprintf call
 - Rebasing patches
 - Actually replace functions with their x variant as described
 Adding copyright to unistd_ext.h

Changes since v6:
 - Rebased on master
 - Fixed ordering in support/Makefile
 - Add a comment in "tests: replace read by xread" commit message that
   explicits why some read call is removed.
 - Revert unexpected read call change of behavior in
   sysdeps/pthread/tst-cancel{20,21}.c
 - Fixed copyrights dates in headers

---
Frédéric Bérat (4):
  tests: replace read by xread
  tests: replace system by xsystem
  tests: replace fread by xfread
  tests: replace fgets by xfgets

 assert/test-assert-perr.c      |  8 ++++---
 assert/test-assert.c           |  8 ++++---
 dirent/tst-fdopendir.c         |  3 ++-
 elf/tst-stackguard1.c          |  5 ++++-
 libio/bug-fseek.c              |  7 +++---
 libio/bug-mmap-fflush.c        |  7 ++++--
 nptl/tst-cancel7.c             |  3 ++-
 nptl/tst-cleanup4.c            |  4 +++-
 nptl/tst-stackguard1.c         |  4 +++-
 nss/tst-nss-db-endpwent.c      |  3 ++-
 stdio-common/bug12.c           | 12 ++++++-----
 stdio-common/bug3.c            |  4 +++-
 stdio-common/bug4.c            |  4 +++-
 stdio-common/test_rdwr.c       | 11 ++++------
 stdio-common/tst-cookie.c      |  5 ++++-
 stdio-common/tst-fmemopen3.c   |  4 +++-
 support/Makefile               |  4 ++++
 support/test-container.c       |  3 ++-
 support/xfgets.c               | 32 ++++++++++++++++++++++++++++
 support/xfread.c               | 39 ++++++++++++++++++++++++++++++++++
 support/xread.c                | 36 +++++++++++++++++++++++++++++++
 support/xstdio.h               |  2 ++
 support/xstdlib.h              | 31 +++++++++++++++++++++++++++
 support/xsystem.c              | 37 ++++++++++++++++++++++++++++++++
 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-cancel6.c  |  3 ++-
 sysdeps/pthread/tst-fini1mod.c |  4 +++-
 31 files changed, 254 insertions(+), 42 deletions(-)
 create mode 100644 support/xfgets.c
 create mode 100644 support/xfread.c
 create mode 100644 support/xread.c
 create mode 100644 support/xstdlib.h
 create mode 100644 support/xsystem.c

-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v7 1/4] tests: replace read by xread
  2023-06-12 15:18 ` [PATCH v7 0/4] Fix warn unused result Frédéric Bérat
@ 2023-06-12 15:18   ` Frédéric Bérat
  2023-06-12 16:57     ` Joseph Myers
  2023-06-14  8:52     ` [PATCH v8 1/2] " Frédéric Bérat
  2023-06-12 15:18   ` [PATCH v7 2/4] tests: replace system by xsystem Frédéric Bérat
                     ` (2 subsequent siblings)
  3 siblings, 2 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-12 15:18 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 /* 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xunistd.h>
+
+#include <support/check.h>
+
+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 <string.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 
 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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 
 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);
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v7 2/4] tests: replace system by xsystem
  2023-06-12 15:18 ` [PATCH v7 0/4] Fix warn unused result Frédéric Bérat
  2023-06-12 15:18   ` [PATCH v7 1/4] tests: replace read by xread Frédéric Bérat
@ 2023-06-12 15:18   ` Frédéric Bérat
  2023-06-13 14:10     ` Siddhesh Poyarekar
                       ` (2 more replies)
  2023-06-12 15:18   ` [PATCH v7 3/4] tests: replace fread by xfread Frédéric Bérat
  2023-06-12 15:18   ` [PATCH v7 4/4] tests: replace fgets by xfgets Frédéric Bérat
  3 siblings, 3 replies; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-12 15:18 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

With fortification enabled, system calls return result needs to be checked,
has it gets the __wur macro enabled.
---
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 <tls.h>
 #include <unistd.h>
 
+#include <support/xstdlib.h>
+
 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 <stdlib.h>
 #include <string.h>
 
+#include <support/xstdlib.h>
 
 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 <support/support.h>
 #include <support/temp_file.h>
 #include <support/xstdio.h>
+#include <support/xstdlib.h>
 #include <support/xunistd.h>
 #include <support/xthread.h>
 
@@ -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 <unistd.h>
 
 #include <support/xunistd.h>
+#include <support/xstdlib.h>
 
 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 <support/support.h>
 #include <support/check.h>
+#include <support/xstdlib.h>
 
 /* 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
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef SUPPORT_XSTDLIB_H
+#define SUPPORT_XSTDLIB_H
+
+#include <stdlib.h>
+#include <sys/cdefs.h>
+
+__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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/support.h>
+#include <support/check.h>
+
+#include <support/xstdlib.h>
+
+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);
+}
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v7 3/4] tests: replace fread by xfread
  2023-06-12 15:18 ` [PATCH v7 0/4] Fix warn unused result Frédéric Bérat
  2023-06-12 15:18   ` [PATCH v7 1/4] tests: replace read by xread Frédéric Bérat
  2023-06-12 15:18   ` [PATCH v7 2/4] tests: replace system by xsystem Frédéric Bérat
@ 2023-06-12 15:18   ` Frédéric Bérat
  2023-06-13 23:57     ` Siddhesh Poyarekar
  2023-06-12 15:18   ` [PATCH v7 4/4] tests: replace fgets by xfgets Frédéric Bérat
  3 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-12 15:18 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

With fortification enabled, fread calls return result needs to be checked,
has it gets the __wur macro enabled.
---
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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xstdio.h>
 
 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 <stdio.h>
 #include <string.h>
 
+#include <support/xstdio.h>
+
 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 <stdio.h>
 #include <string.h>
 
+#include <support/xstdio.h>
+
 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 <unistd.h>
 #include <string.h>
 
+#include <support/xstdio.h>
+
 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 <stdio.h>
 
+#include <support/xstdio.h>
+
 
 #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 <string.h>
 #include <sys/types.h>
 
+#include <support/xstdio.h>
+
 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xstdio.h>
+
+#include <support/check.h>
+#include <stdlib.h>
+
+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
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v7 4/4] tests: replace fgets by xfgets
  2023-06-12 15:18 ` [PATCH v7 0/4] Fix warn unused result Frédéric Bérat
                     ` (2 preceding siblings ...)
  2023-06-12 15:18   ` [PATCH v7 3/4] tests: replace fread by xfread Frédéric Bérat
@ 2023-06-12 15:18   ` Frédéric Bérat
  2023-06-13 12:23     ` Siddhesh Poyarekar
  3 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-12 15:18 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

With fortification enabled, fgets calls return result needs to be checked,
has it gets the __wur macro enabled.
---
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 <string.h>
 #include <setjmp.h>
 
+#include <support/xstdio.h>
+
 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 <string.h>
 #include <setjmp.h>
 
+#include <support/xstdio.h>
+
 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 <stdlib.h>
 #include <string.h>
 
+#include <support/xstdio.h>
 
 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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xstdio.h>
+
+#include <support/check.h>
+#include <stdlib.h>
+
+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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xstdio.h>
 
 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;
 }
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v7 1/4] tests: replace read by xread
  2023-06-12 15:18   ` [PATCH v7 1/4] tests: replace read by xread Frédéric Bérat
@ 2023-06-12 16:57     ` Joseph Myers
  2023-06-13 14:22       ` Frederic Berat
  2023-06-14  8:52     ` [PATCH v8 1/2] " Frédéric Bérat
  1 sibling, 1 reply; 109+ messages in thread
From: Joseph Myers @ 2023-06-12 16:57 UTC (permalink / raw)
  To: Frédéric Bérat; +Cc: libc-alpha, siddhesh

[-- Attachment #1: Type: text/plain, Size: 1044 bytes --]

On Mon, 12 Jun 2023, Frédéric Bérat via Libc-alpha wrote:

> @@ -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));

Since the comment suggests this code is actually testing a property of the 
read function, it seems doubtful whether it's appropriate to change it to 
call xread - which would impose an API requirement on xread that it calls 
read in exactly the way under test.  Calling x* functions is fine in 
testcases when the function in question is not under test - for example, 
when the point of calling xread is just to read some data, with the 
details of how that's done being irrelevant - but questionable when the 
test is a test of read itself.

> @@ -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");

The same might apply here.

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v7 4/4] tests: replace fgets by xfgets
  2023-06-12 15:18   ` [PATCH v7 4/4] tests: replace fgets by xfgets Frédéric Bérat
@ 2023-06-13 12:23     ` Siddhesh Poyarekar
  2023-06-13 18:25       ` Joseph Myers
  0 siblings, 1 reply; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-13 12:23 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha; +Cc: joseph



On 2023-06-12 11:18, Frédéric Bérat wrote:
> With fortification enabled, fgets calls return result needs to be checked,
> has it gets the __wur macro enabled.
> ---
> 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 <string.h>
>   #include <setjmp.h>
>   
> +#include <support/xstdio.h>
> +
>   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);

Joseph, would you apply the same rationale for these tests too, i.e. 
since the test involves interaction with stdio and signals, would you 
avoid adding an xstdio abstraction here to test stdio directly?

Likewise for the stdio-common test here and in patch 3/4, what do you think?

Thanks,
Sid

>     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 <string.h>
>   #include <setjmp.h>
>   
> +#include <support/xstdio.h>
> +
>   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 <stdlib.h>
>   #include <string.h>
>   
> +#include <support/xstdio.h>
>   
>   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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/xstdio.h>
> +
> +#include <support/check.h>
> +#include <stdlib.h>
> +
> +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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/xstdio.h>
>   
>   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;
>   }

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v7 2/4] tests: replace system by xsystem
  2023-06-12 15:18   ` [PATCH v7 2/4] tests: replace system by xsystem Frédéric Bérat
@ 2023-06-13 14:10     ` Siddhesh Poyarekar
  2023-06-13 14:13     ` Adhemerval Zanella Netto
  2023-06-14  8:52     ` [PATCH 2/2] " Frédéric Bérat
  2 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-13 14:10 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-12 11:18, Frédéric Bérat wrote:
> With fortification enabled, system calls return result needs to be checked,
> has it gets the __wur macro enabled.
> ---
> Changes since v6:
>   - Fixed support/Makefile ordering

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
>   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 <tls.h>
>   #include <unistd.h>
>   
> +#include <support/xstdlib.h>
> +
>   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 <stdlib.h>
>   #include <string.h>
>   
> +#include <support/xstdlib.h>
>   
>   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 <support/support.h>
>   #include <support/temp_file.h>
>   #include <support/xstdio.h>
> +#include <support/xstdlib.h>
>   #include <support/xunistd.h>
>   #include <support/xthread.h>
>   
> @@ -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 <unistd.h>
>   
>   #include <support/xunistd.h>
> +#include <support/xstdlib.h>
>   
>   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 <support/support.h>
>   #include <support/check.h>
> +#include <support/xstdlib.h>
>   
>   /* 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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#ifndef SUPPORT_XSTDLIB_H
> +#define SUPPORT_XSTDLIB_H
> +
> +#include <stdlib.h>
> +#include <sys/cdefs.h>
> +
> +__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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/support.h>
> +#include <support/check.h>
> +
> +#include <support/xstdlib.h>
> +
> +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);
> +}

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v7 2/4] tests: replace system by xsystem
  2023-06-12 15:18   ` [PATCH v7 2/4] tests: replace system by xsystem Frédéric Bérat
  2023-06-13 14:10     ` Siddhesh Poyarekar
@ 2023-06-13 14:13     ` Adhemerval Zanella Netto
  2023-06-13 14:16       ` Siddhesh Poyarekar
  2023-06-14  8:52     ` [PATCH 2/2] " Frédéric Bérat
  2 siblings, 1 reply; 109+ messages in thread
From: Adhemerval Zanella Netto @ 2023-06-13 14:13 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha; +Cc: siddhesh



On 12/06/23 12:18, Frédéric Bérat via Libc-alpha wrote:
> With fortification enabled, system calls return result needs to be checked,
> has it gets the __wur macro enabled.
> ---
> 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 <tls.h>
>  #include <unistd.h>
>  
> +#include <support/xstdlib.h>
> +
>  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 <stdlib.h>
>  #include <string.h>
>  
> +#include <support/xstdlib.h>
>  
>  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 <support/support.h>
>  #include <support/temp_file.h>
>  #include <support/xstdio.h>
> +#include <support/xstdlib.h>
>  #include <support/xunistd.h>
>  #include <support/xthread.h>
>  
> @@ -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;
>  }

Joseph's comments for tests that area actually checking for the interface
[1] also applies here.  I think we should keep using 'system' rather than
any wrapper here.


[1] https://sourceware.org/pipermail/libc-alpha/2023-June/148978.html

> 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 <unistd.h>
>  
>  #include <support/xunistd.h>
> +#include <support/xstdlib.h>
>  
>  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 <support/support.h>
>  #include <support/check.h>
> +#include <support/xstdlib.h>
>  
>  /* 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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#ifndef SUPPORT_XSTDLIB_H
> +#define SUPPORT_XSTDLIB_H
> +
> +#include <stdlib.h>
> +#include <sys/cdefs.h>
> +
> +__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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/support.h>
> +#include <support/check.h>
> +
> +#include <support/xstdlib.h>
> +
> +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);
> +}

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v7 2/4] tests: replace system by xsystem
  2023-06-13 14:13     ` Adhemerval Zanella Netto
@ 2023-06-13 14:16       ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-13 14:16 UTC (permalink / raw)
  To: Adhemerval Zanella Netto, Frédéric Bérat, libc-alpha

On 2023-06-13 10:13, Adhemerval Zanella Netto wrote:
> 
> 
> On 12/06/23 12:18, Frédéric Bérat via Libc-alpha wrote:
>> With fortification enabled, system calls return result needs to be checked,
>> has it gets the __wur macro enabled.
>> ---
>> 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 <tls.h>
>>   #include <unistd.h>
>>   
>> +#include <support/xstdlib.h>
>> +
>>   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 <stdlib.h>
>>   #include <string.h>
>>   
>> +#include <support/xstdlib.h>
>>   
>>   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 <support/support.h>
>>   #include <support/temp_file.h>
>>   #include <support/xstdio.h>
>> +#include <support/xstdlib.h>
>>   #include <support/xunistd.h>
>>   #include <support/xthread.h>
>>   
>> @@ -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;
>>   }
> 
> Joseph's comments for tests that area actually checking for the interface
> [1] also applies here.  I think we should keep using 'system' rather than
> any wrapper here.
> 
> 
> [1] https://sourceware.org/pipermail/libc-alpha/2023-June/148978.html

Ahh yes, thanks, I missed that.

Sid

> 
>> 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 <unistd.h>
>>   
>>   #include <support/xunistd.h>
>> +#include <support/xstdlib.h>
>>   
>>   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 <support/support.h>
>>   #include <support/check.h>
>> +#include <support/xstdlib.h>
>>   
>>   /* 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
>> +   <https://www.gnu.org/licenses/>.  */
>> +
>> +#ifndef SUPPORT_XSTDLIB_H
>> +#define SUPPORT_XSTDLIB_H
>> +
>> +#include <stdlib.h>
>> +#include <sys/cdefs.h>
>> +
>> +__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
>> +   <https://www.gnu.org/licenses/>.  */
>> +
>> +#include <support/support.h>
>> +#include <support/check.h>
>> +
>> +#include <support/xstdlib.h>
>> +
>> +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);
>> +}
> 

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v7 1/4] tests: replace read by xread
  2023-06-12 16:57     ` Joseph Myers
@ 2023-06-13 14:22       ` Frederic Berat
  0 siblings, 0 replies; 109+ messages in thread
From: Frederic Berat @ 2023-06-13 14:22 UTC (permalink / raw)
  To: Joseph Myers; +Cc: libc-alpha, siddhesh

On Mon, Jun 12, 2023 at 6:58 PM Joseph Myers <joseph@codesourcery.com> wrote:
>
> On Mon, 12 Jun 2023, Frédéric Bérat via Libc-alpha wrote:
>
> > @@ -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));
>
> Since the comment suggests this code is actually testing a property of the
> read function, it seems doubtful whether it's appropriate to change it to
> call xread - which would impose an API requirement on xread that it calls
> read in exactly the way under test.  Calling x* functions is fine in
> testcases when the function in question is not under test - for example,
> when the point of calling xread is just to read some data, with the
> details of how that's done being irrelevant - but questionable when the
> test is a test of read itself.
>

My understanding is that's not really a property of read that is
tested here, since it could be theoretically replaced by anything that
blocks and is a cancellation point. So in principle, the way it is
done is still irrelevant.

That said, I see your point. The wrapper may bring opacity that is not
wanted here.


> > @@ -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");
>
> The same might apply here.
>
> --
> Joseph S. Myers
> joseph@codesourcery.com


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v7 4/4] tests: replace fgets by xfgets
  2023-06-13 12:23     ` Siddhesh Poyarekar
@ 2023-06-13 18:25       ` Joseph Myers
  2023-06-13 23:56         ` Siddhesh Poyarekar
  0 siblings, 1 reply; 109+ messages in thread
From: Joseph Myers @ 2023-06-13 18:25 UTC (permalink / raw)
  To: Siddhesh Poyarekar; +Cc: Frédéric Bérat, libc-alpha

On Tue, 13 Jun 2023, Siddhesh Poyarekar wrote:

> > 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 <string.h>
> >   #include <setjmp.h>
> >   +#include <support/xstdio.h>
> > +
> >   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);
> 
> Joseph, would you apply the same rationale for these tests too, i.e. since the
> test involves interaction with stdio and signals, would you avoid adding an
> xstdio abstraction here to test stdio directly?

I think this test is using fgets to check file contents rather than 
testing particular things about how fgets behaves.

> Likewise for the stdio-common test here and in patch 3/4, what do you think?

And those look they are using fread to check contents while testing some 
other stdio functions.

-- 
Joseph S. Myers
joseph@codesourcery.com

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v7 4/4] tests: replace fgets by xfgets
  2023-06-13 18:25       ` Joseph Myers
@ 2023-06-13 23:56         ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-13 23:56 UTC (permalink / raw)
  To: Joseph Myers; +Cc: Frédéric Bérat, libc-alpha

On 2023-06-13 14:25, Joseph Myers wrote:
> On Tue, 13 Jun 2023, Siddhesh Poyarekar wrote:
> 
>>> 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 <string.h>
>>>    #include <setjmp.h>
>>>    +#include <support/xstdio.h>
>>> +
>>>    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);
>>
>> Joseph, would you apply the same rationale for these tests too, i.e. since the
>> test involves interaction with stdio and signals, would you avoid adding an
>> xstdio abstraction here to test stdio directly?
> 
> I think this test is using fgets to check file contents rather than
> testing particular things about how fgets behaves.
> 
>> Likewise for the stdio-common test here and in patch 3/4, what do you think?
> 
> And those look they are using fread to check contents while testing some
> other stdio functions.

OK, in that case, LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v7 3/4] tests: replace fread by xfread
  2023-06-12 15:18   ` [PATCH v7 3/4] tests: replace fread by xfread Frédéric Bérat
@ 2023-06-13 23:57     ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-13 23:57 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-12 11:18, Frédéric Bérat wrote:
> With fortification enabled, fread calls return result needs to be checked,
> has it gets the __wur macro enabled.
> ---
> Changes since v6:
>   - Fixed support/Makefile ordering
>   - Fixed header copyright date

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
>   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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/xstdio.h>
>   
>   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 <stdio.h>
>   #include <string.h>
>   
> +#include <support/xstdio.h>
> +
>   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 <stdio.h>
>   #include <string.h>
>   
> +#include <support/xstdio.h>
> +
>   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 <unistd.h>
>   #include <string.h>
>   
> +#include <support/xstdio.h>
> +
>   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 <stdio.h>
>   
> +#include <support/xstdio.h>
> +
>   
>   #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 <string.h>
>   #include <sys/types.h>
>   
> +#include <support/xstdio.h>
> +
>   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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/xstdio.h>
> +
> +#include <support/check.h>
> +#include <stdlib.h>
> +
> +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

^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH v8 1/2] tests: replace read by xread
  2023-06-12 15:18   ` [PATCH v7 1/4] tests: replace read by xread Frédéric Bérat
  2023-06-12 16:57     ` Joseph Myers
@ 2023-06-14  8:52     ` Frédéric Bérat
  2023-06-14 11:51       ` Siddhesh Poyarekar
  1 sibling, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-14  8:52 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

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 v7:
 - sysdeps/pthread/tst-cancel11.c directly uses read instead of xread to ensure
   clarity on what is actually wanted.

 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 |  6 +++++-
 sysdeps/pthread/tst-cancel20.c |  2 --
 sysdeps/pthread/tst-cancel21.c |  2 --
 sysdeps/pthread/tst-fini1mod.c |  4 +++-
 11 files changed, 56 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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 /* 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 8ebe68aa95..5939e53469 100644
--- a/support/Makefile
+++ b/support/Makefile
@@ -197,6 +197,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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/xunistd.h>
+
+#include <support/check.h>
+
+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..0fdc444154 100644
--- a/sysdeps/pthread/tst-cancel11.c
+++ b/sysdeps/pthread/tst-cancel11.c
@@ -56,7 +56,11 @@ tf (void *arg)
 
   /* This call should block and be cancelable.  */
   char buf[20];
-  read (fd[0], buf, sizeof (buf));
+  if (read (fd[0], buf, sizeof (buf)))
+  {
+    puts ("read unexpectedly returned");
+    exit (1);
+  }
 
   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 <stdlib.h>
 #include <unistd.h>
 
+#include <support/xunistd.h>
+
 
 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);
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* [PATCH 2/2] tests: replace system by xsystem
  2023-06-12 15:18   ` [PATCH v7 2/4] tests: replace system by xsystem Frédéric Bérat
  2023-06-13 14:10     ` Siddhesh Poyarekar
  2023-06-13 14:13     ` Adhemerval Zanella Netto
@ 2023-06-14  8:52     ` Frédéric Bérat
  2023-06-14 11:53       ` Siddhesh Poyarekar
  2 siblings, 1 reply; 109+ messages in thread
From: Frédéric Bérat @ 2023-06-14  8:52 UTC (permalink / raw)
  To: libc-alpha; +Cc: siddhesh, fberat

With fortification enabled, system calls return result needs to be checked,
has it gets the __wur macro enabled.
---
Changes since v7:
 - sysdeps/pthread/tst-cancel7.c directly uses system instead of xsystem to
   ensure clarity on what is actually wanted.

 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 <tls.h>
 #include <unistd.h>
 
+#include <support/xstdlib.h>
+
 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 <stdlib.h>
 #include <string.h>
 
+#include <support/xstdlib.h>
 
 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..ef9e11ce58 100644
--- a/nptl/tst-cancel7.c
+++ b/nptl/tst-cancel7.c
@@ -43,7 +43,8 @@ tf (void *arg)
 {
   char *cmd = xasprintf ("%s --direct --sem %s --pidfile %s",
 			 command, semfilename, pidfilename);
-  system (cmd);
+  if (system (cmd))
+    FAIL_EXIT1("system call unexpectedly returned");
   /* 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 <unistd.h>
 
 #include <support/xunistd.h>
+#include <support/xstdlib.h>
 
 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 <support/support.h>
 #include <support/check.h>
+#include <support/xstdlib.h>
 
 /* 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 5939e53469..c81e3c928c 100644
--- a/support/Makefile
+++ b/support/Makefile
@@ -212,6 +212,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
+   <https://www.gnu.org/licenses/>.  */
+
+#ifndef SUPPORT_XSTDLIB_H
+#define SUPPORT_XSTDLIB_H
+
+#include <stdlib.h>
+#include <sys/cdefs.h>
+
+__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
+   <https://www.gnu.org/licenses/>.  */
+
+#include <support/support.h>
+#include <support/check.h>
+
+#include <support/xstdlib.h>
+
+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);
+}
-- 
2.40.1


^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH v8 1/2] tests: replace read by xread
  2023-06-14  8:52     ` [PATCH v8 1/2] " Frédéric Bérat
@ 2023-06-14 11:51       ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-14 11:51 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-14 04:52, Frédéric Bérat wrote:
> 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 v7:
>   - sysdeps/pthread/tst-cancel11.c directly uses read instead of xread to ensure
>     clarity on what is actually wanted.

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
>   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 |  6 +++++-
>   sysdeps/pthread/tst-cancel20.c |  2 --
>   sysdeps/pthread/tst-cancel21.c |  2 --
>   sysdeps/pthread/tst-fini1mod.c |  4 +++-
>   11 files changed, 56 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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
> +
>   /* 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 8ebe68aa95..5939e53469 100644
> --- a/support/Makefile
> +++ b/support/Makefile
> @@ -197,6 +197,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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/xunistd.h>
> +
> +#include <support/check.h>
> +
> +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..0fdc444154 100644
> --- a/sysdeps/pthread/tst-cancel11.c
> +++ b/sysdeps/pthread/tst-cancel11.c
> @@ -56,7 +56,11 @@ tf (void *arg)
>   
>     /* This call should block and be cancelable.  */
>     char buf[20];
> -  read (fd[0], buf, sizeof (buf));
> +  if (read (fd[0], buf, sizeof (buf)))
> +  {
> +    puts ("read unexpectedly returned");
> +    exit (1);
> +  }
>   
>     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 <stdlib.h>
>   #include <unistd.h>
>   
> +#include <support/xunistd.h>
> +
>   
>   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);

^ permalink raw reply	[flat|nested] 109+ messages in thread

* Re: [PATCH 2/2] tests: replace system by xsystem
  2023-06-14  8:52     ` [PATCH 2/2] " Frédéric Bérat
@ 2023-06-14 11:53       ` Siddhesh Poyarekar
  0 siblings, 0 replies; 109+ messages in thread
From: Siddhesh Poyarekar @ 2023-06-14 11:53 UTC (permalink / raw)
  To: Frédéric Bérat, libc-alpha



On 2023-06-14 04:52, Frédéric Bérat wrote:
> With fortification enabled, system calls return result needs to be checked,
> has it gets the __wur macro enabled.
> ---
> Changes since v7:
>   - sysdeps/pthread/tst-cancel7.c directly uses system instead of xsystem to
>     ensure clarity on what is actually wanted.
> 
>   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

LGTM.

Reviewed-by: Siddhesh Poyarekar <siddhesh@sourceware.org>

> 
> 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 <tls.h>
>   #include <unistd.h>
>   
> +#include <support/xstdlib.h>
> +
>   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 <stdlib.h>
>   #include <string.h>
>   
> +#include <support/xstdlib.h>
>   
>   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..ef9e11ce58 100644
> --- a/nptl/tst-cancel7.c
> +++ b/nptl/tst-cancel7.c
> @@ -43,7 +43,8 @@ tf (void *arg)
>   {
>     char *cmd = xasprintf ("%s --direct --sem %s --pidfile %s",
>   			 command, semfilename, pidfilename);
> -  system (cmd);
> +  if (system (cmd))
> +    FAIL_EXIT1("system call unexpectedly returned");
>     /* 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 <unistd.h>
>   
>   #include <support/xunistd.h>
> +#include <support/xstdlib.h>
>   
>   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 <support/support.h>
>   #include <support/check.h>
> +#include <support/xstdlib.h>
>   
>   /* 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 5939e53469..c81e3c928c 100644
> --- a/support/Makefile
> +++ b/support/Makefile
> @@ -212,6 +212,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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#ifndef SUPPORT_XSTDLIB_H
> +#define SUPPORT_XSTDLIB_H
> +
> +#include <stdlib.h>
> +#include <sys/cdefs.h>
> +
> +__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
> +   <https://www.gnu.org/licenses/>.  */
> +
> +#include <support/support.h>
> +#include <support/check.h>
> +
> +#include <support/xstdlib.h>
> +
> +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);
> +}

^ permalink raw reply	[flat|nested] 109+ messages in thread

end of thread, other threads:[~2023-06-14 11:53 UTC | newest]

Thread overview: 109+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-18 12:11 [PATCH 0/8] Fix warn unused result Frédéric Bérat
2023-04-18 12:11 ` [PATCH 1/8] tests: fix " Frédéric Bérat
2023-04-18 12:23   ` Siddhesh Poyarekar
2023-04-28 12:21   ` [PATCH v4 07/15] tests: fix warn unused result on asprintf calls Frédéric Bérat
2023-05-25  1:07     ` Siddhesh Poyarekar
2023-05-31 14:36       ` Frederic Berat
2023-04-28 12:21   ` [PATCH v4 08/15] tests: replace write by xwrite Frédéric Bérat
2023-05-25  1:16     ` Siddhesh Poyarekar
2023-06-01 16:39       ` Siddhesh Poyarekar
2023-04-28 12:21   ` [PATCH v4 09/15] tests: replace read by xread Frédéric Bérat
2023-04-28 12:21   ` [PATCH v4 10/15] tests: replace system by xsystem Frédéric Bérat
2023-05-25  1:22     ` Siddhesh Poyarekar
2023-04-28 12:21   ` [PATCH v4 11/15] tests: replace fread by xfread Frédéric Bérat
2023-04-28 12:21   ` [PATCH v4 12/15] tests: replace ftruncate by xftruncate Frédéric Bérat
2023-05-25  1:25     ` Siddhesh Poyarekar
2023-06-01 16:42       ` Siddhesh Poyarekar
2023-04-28 12:21   ` [PATCH v4 13/15] tests: replace fgets by xfgets Frédéric Bérat
2023-04-28 12:21   ` [PATCH v4 14/15] tests: Replace various function calls with their x variant Frédéric Bérat
2023-05-25  1:29     ` Siddhesh Poyarekar
2023-05-25 16:10       ` Frederic Berat
2023-04-28 12:21   ` [PATCH v4 15/15] tests: fix warn unused results Frédéric Bérat
2023-05-25  1:35     ` Siddhesh Poyarekar
2023-06-01 14:27   ` [PATCH v5 04/12] tests: fix warn unused result on asprintf calls Frédéric Bérat
2023-06-01 16:52     ` Siddhesh Poyarekar
2023-06-01 14:27   ` [PATCH v5 07/12] tests: replace system by xsystem Frédéric Bérat
2023-06-01 16:56     ` Siddhesh Poyarekar
2023-06-01 14:27   ` [PATCH v5 12/12] tests: fix warn unused results Frédéric Bérat
2023-06-01 16:57     ` Siddhesh Poyarekar
2023-04-18 12:11 ` [PATCH 2/8] catgets/gencat.c: fix warn unused result Frédéric Bérat
2023-04-18 12:36   ` Siddhesh Poyarekar
2023-04-28 12:21   ` [PATCH v4 01/15] " Frédéric Bérat
2023-05-24 22:47     ` Siddhesh Poyarekar
2023-06-01 14:27   ` [PATCH v5 01/12] " Frédéric Bérat
2023-06-01 16:47     ` Siddhesh Poyarekar
2023-04-18 12:11 ` [PATCH 3/8] inet/rcmd.c: " Frédéric Bérat
2023-04-18 12:37   ` Siddhesh Poyarekar
2023-04-18 13:40   ` Andreas Schwab
2023-04-18 13:50     ` Siddhesh Poyarekar
2023-04-18 12:11 ` [PATCH 4/8] locale/programs/locarchive.c: " Frédéric Bérat
2023-04-18 12:43   ` Siddhesh Poyarekar
2023-04-28 12:21   ` [PATCH v4 02/15] " Frédéric Bérat
2023-05-24 22:49     ` Siddhesh Poyarekar
2023-06-01 14:27   ` [PATCH v5 02/12] malloc/{memusage.c,memusagestat.c}: " Frédéric Bérat
2023-06-01 16:49     ` Siddhesh Poyarekar
2023-04-18 12:11 ` [PATCH 5/8] " Frédéric Bérat
2023-04-18 12:47   ` [PATCH 5/8] malloc/{memusage.c, memusagestat.c}: " Siddhesh Poyarekar
2023-04-28 12:21   ` [PATCH v4 03/15] malloc/{memusage.c,memusagestat.c}: " Frédéric Bérat
2023-05-25  0:50     ` Siddhesh Poyarekar
2023-06-01 15:55       ` [PATCH] Move {read,write}_all functions to a dedicated header Frédéric Bérat
2023-06-01 17:07         ` Siddhesh Poyarekar
2023-06-02  6:10           ` Frederic Berat
2023-06-02 10:01             ` Siddhesh Poyarekar
2023-04-18 12:11 ` [PATCH 6/8] nptl_db/thread_dbP.h: fix warn unused result Frédéric Bérat
2023-04-18 12:49   ` Siddhesh Poyarekar
2023-04-18 12:51     ` Siddhesh Poyarekar
2023-04-28 12:21   ` [PATCH v4 04/15] " Frédéric Bérat
2023-05-25  0:51     ` Siddhesh Poyarekar
2023-05-25  1:52       ` Siddhesh Poyarekar
2023-06-01 14:27   ` [PATCH v5 03/12] " Frédéric Bérat
2023-06-01 16:49     ` Siddhesh Poyarekar
2023-04-18 12:11 ` [PATCH 7/8] sunrpc/netname.c: " Frédéric Bérat
2023-04-18 12:51   ` Siddhesh Poyarekar
2023-04-28 12:21   ` [PATCH v4 05/15] " Frédéric Bérat
2023-05-25  0:53     ` Siddhesh Poyarekar
2023-04-18 12:11 ` [PATCH 8/8] sysdeps/pthread/eintr.c: " Frédéric Bérat
2023-04-18 12:54   ` Siddhesh Poyarekar
2023-04-28 12:21   ` [PATCH v4 06/15] " Frédéric Bérat
2023-05-25  0:59     ` Siddhesh Poyarekar
2023-04-28 12:21 ` [PATCH v4 00/15] Fix " Frédéric Bérat
2023-05-25  1:53   ` Siddhesh Poyarekar
2023-06-02 15:28 ` [PATCH v6 0/7] " Frédéric Bérat
2023-06-02 15:28   ` [PATCH v6 1/7] tests: fix warn unused result on asprintf calls Frédéric Bérat
2023-06-06  6:18     ` Siddhesh Poyarekar
2023-06-02 15:28   ` [PATCH v6 2/7] tests: replace read by xread Frédéric Bérat
2023-06-06  6:21     ` Siddhesh Poyarekar
2023-06-06  8:00       ` Frederic Berat
2023-06-12 14:22         ` Siddhesh Poyarekar
2023-06-07 19:04     ` Frederic Berat
2023-06-02 15:28   ` [PATCH v6 3/7] tests: replace system by xsystem Frédéric Bérat
2023-06-06 12:17     ` Siddhesh Poyarekar
2023-06-02 15:28   ` [PATCH v6 4/7] tests: replace fread by xfread Frédéric Bérat
2023-06-06 12:18     ` Siddhesh Poyarekar
2023-06-07 19:03     ` Frederic Berat
2023-06-02 15:28   ` [PATCH v6 5/7] tests: replace fgets by xfgets Frédéric Bérat
2023-06-06 12:20     ` Siddhesh Poyarekar
2023-06-08  5:50     ` Maxim Kuvyrkov
2023-06-08  6:57       ` Frederic Berat
2023-06-02 15:28   ` [PATCH v6 6/7] tests: Replace various function calls with their x variant Frédéric Bérat
2023-06-06 12:20     ` Siddhesh Poyarekar
2023-06-02 15:28   ` [PATCH v6 7/7] Move {read,write}_all functions to a dedicated header Frédéric Bérat
2023-06-06 12:21     ` Siddhesh Poyarekar
2023-06-12 15:18 ` [PATCH v7 0/4] Fix warn unused result Frédéric Bérat
2023-06-12 15:18   ` [PATCH v7 1/4] tests: replace read by xread Frédéric Bérat
2023-06-12 16:57     ` Joseph Myers
2023-06-13 14:22       ` Frederic Berat
2023-06-14  8:52     ` [PATCH v8 1/2] " Frédéric Bérat
2023-06-14 11:51       ` Siddhesh Poyarekar
2023-06-12 15:18   ` [PATCH v7 2/4] tests: replace system by xsystem Frédéric Bérat
2023-06-13 14:10     ` Siddhesh Poyarekar
2023-06-13 14:13     ` Adhemerval Zanella Netto
2023-06-13 14:16       ` Siddhesh Poyarekar
2023-06-14  8:52     ` [PATCH 2/2] " Frédéric Bérat
2023-06-14 11:53       ` Siddhesh Poyarekar
2023-06-12 15:18   ` [PATCH v7 3/4] tests: replace fread by xfread Frédéric Bérat
2023-06-13 23:57     ` Siddhesh Poyarekar
2023-06-12 15:18   ` [PATCH v7 4/4] tests: replace fgets by xfgets Frédéric Bérat
2023-06-13 12:23     ` Siddhesh Poyarekar
2023-06-13 18:25       ` Joseph Myers
2023-06-13 23:56         ` Siddhesh Poyarekar

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).