Fix BZ 23400 -- stdlib/test-bz22786.c creates temporary files in glibc source tree

Message ID CALoOobOOo0z5FtsAE4s2rdM_0DwtJ50XoPEDrL=qUgasKzNp8Q@mail.gmail.com
State New, archived
Headers

Commit Message

Paul Pluzhnikov July 23, 2018, 4:48 p.m. UTC
  Greetings,

Attached patch fixes BZ 23400, by using standard support for creating
temporary directories.

2018-07-23  Paul Pluzhnikov  <ppluzhnikov@google.com>

        [BZ #23400]
        * stdlib/test-bz22786.c (do_test): Use support_create_temp_directory.
  

Comments

Adhemerval Zanella Netto July 30, 2018, 8:13 p.m. UTC | #1
On 23/07/2018 13:48, Paul Pluzhnikov wrote:
> Greetings,
> 
> Attached patch fixes BZ 23400, by using standard support for creating
> temporary directories.
> 
> 2018-07-23  Paul Pluzhnikov  <ppluzhnikov@google.com>
> 
>         [BZ #23400]
>         * stdlib/test-bz22786.c (do_test): Use support_create_temp_directory.
> 
> -- Paul Pluzhnikov
> 
> 
> glibc-bz23400-20180721.txt
> 
> 
> diff --git a/stdlib/test-bz22786.c b/stdlib/test-bz22786.c
> index e7837f98c1..f7635c785f 100644
> --- a/stdlib/test-bz22786.c
> +++ b/stdlib/test-bz22786.c
> @@ -26,22 +26,21 @@
>  #include <unistd.h>
>  #include <sys/stat.h>
>  #include <sys/types.h>
> +#include <support/support.h>
> +#include <support/temp_file.h>
>  #include <support/test-driver.h>
>  #include <libc-diag.h>
>  
>  static int
>  do_test (void)
>  {
> -  const char dir[] = "bz22786";
> -  const char lnk[] = "bz22786/symlink";
> +  const char *dir = support_create_temp_directory ("bz22786.");
> +  char *lnk = xmalloc (strlen (dir) + strlen ("/symlink") + 1);
>  
> -  rmdir (dir);
> -  if (mkdir (dir, 0755) != 0 && errno != EEXIST)
> -    {
> -      printf ("mkdir %s: %m\n", dir);
> -      return EXIT_FAILURE;
> -    }
> -  if (symlink (".", lnk) != 0 && errno != EEXIST)
> +  strcpy (lnk, dir);
> +  strcat (lnk, "/symlink");

Maybe just 'char *lnk = xasprintf ("%s/symlink", dir);' instead?

> +
> +  if (symlink (".", lnk) != 0)
>      {
>        printf ("symlink (%s, %s): %m\n", dir, lnk);
>        return EXIT_FAILURE;

Use FAIL_EXIT1 or just TEST_VERIFY_EXIT.


> @@ -55,17 +54,11 @@ do_test (void)
>       allocation to succeed for the test to work.  */
>    DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
>  #endif
> -  char *path = malloc (path_len);
> +  char *path = xmalloc (path_len);
>    DIAG_POP_NEEDS_COMMENT;
>  
> -  if (path == NULL)
> -    {
> -      printf ("malloc (%zu): %m\n", path_len);
> -      return EXIT_UNSUPPORTED;
> -    }
> -
> -  /* Construct very long path = "bz22786/symlink/aaaa....."  */
> -  char *p = mempcpy (path, lnk, sizeof (lnk) - 1);
> +  /* Construct very long path = "/tmp/bz22786.XXXX/symlink/aaaa....."  */
> +  char *p = mempcpy (path, lnk, strlen (lnk));
>    *(p++) = '/';
>    memset (p, 'a', path_len - (path - p) - 2);
>    p[path_len - (path - p) - 1] = '\0';

Shouldn't it 'p - path' instead? The subtraction is clearly issuing a
overflow and I think it is not what the test meant here.

> @@ -81,7 +74,6 @@ do_test (void)
>  
>    /* Cleanup.  */
>    unlink (lnk);
> -  rmdir (dir);
>  
>    return 0;
>  }
>
  

Patch

diff --git a/stdlib/test-bz22786.c b/stdlib/test-bz22786.c
index e7837f98c1..f7635c785f 100644
--- a/stdlib/test-bz22786.c
+++ b/stdlib/test-bz22786.c
@@ -26,22 +26,21 @@ 
 #include <unistd.h>
 #include <sys/stat.h>
 #include <sys/types.h>
+#include <support/support.h>
+#include <support/temp_file.h>
 #include <support/test-driver.h>
 #include <libc-diag.h>
 
 static int
 do_test (void)
 {
-  const char dir[] = "bz22786";
-  const char lnk[] = "bz22786/symlink";
+  const char *dir = support_create_temp_directory ("bz22786.");
+  char *lnk = xmalloc (strlen (dir) + strlen ("/symlink") + 1);
 
-  rmdir (dir);
-  if (mkdir (dir, 0755) != 0 && errno != EEXIST)
-    {
-      printf ("mkdir %s: %m\n", dir);
-      return EXIT_FAILURE;
-    }
-  if (symlink (".", lnk) != 0 && errno != EEXIST)
+  strcpy (lnk, dir);
+  strcat (lnk, "/symlink");
+
+  if (symlink (".", lnk) != 0)
     {
       printf ("symlink (%s, %s): %m\n", dir, lnk);
       return EXIT_FAILURE;
@@ -55,17 +54,11 @@  do_test (void)
      allocation to succeed for the test to work.  */
   DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
 #endif
-  char *path = malloc (path_len);
+  char *path = xmalloc (path_len);
   DIAG_POP_NEEDS_COMMENT;
 
-  if (path == NULL)
-    {
-      printf ("malloc (%zu): %m\n", path_len);
-      return EXIT_UNSUPPORTED;
-    }
-
-  /* Construct very long path = "bz22786/symlink/aaaa....."  */
-  char *p = mempcpy (path, lnk, sizeof (lnk) - 1);
+  /* Construct very long path = "/tmp/bz22786.XXXX/symlink/aaaa....."  */
+  char *p = mempcpy (path, lnk, strlen (lnk));
   *(p++) = '/';
   memset (p, 'a', path_len - (path - p) - 2);
   p[path_len - (path - p) - 1] = '\0';
@@ -81,7 +74,6 @@  do_test (void)
 
   /* Cleanup.  */
   unlink (lnk);
-  rmdir (dir);
 
   return 0;
 }