[v6,1/7] tests: fix warn unused result on asprintf calls

Message ID 20230602152812.108497-2-fberat@redhat.com
State Committed
Commit 8c4f69d711481a18c70cb9a6c0a5367604894320
Delegated to: Siddhesh Poyarekar
Headers
Series Fix warn unused result |

Checks

Context Check Description
redhat-pt-bot/TryBot-apply_patch success Patch applied to master at the time it was sent
linaro-tcwg-bot/tcwg_glibc_build--master-arm success Testing passed
linaro-tcwg-bot/tcwg_glibc_check--master-arm pending Patch applied
linaro-tcwg-bot/tcwg_glibc_build--master-aarch64 success Testing passed

Commit Message

Frédéric Bérat June 2, 2023, 3:28 p.m. UTC
  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(-)
  

Comments

Siddhesh Poyarekar June 6, 2023, 6:18 a.m. UTC | #1
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";
>   
>   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);
  

Patch

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