50f89d
commit 6c3a8a9d868a8deddf0d6dcc785b6d120de90523
50f89d
Author: Paul Pluzhnikov <ppluzhnikov@kazbek.mtv.corp.google.com>
50f89d
Date:   Fri Aug 24 18:08:51 2018 -0700
50f89d
50f89d
    Fix BZ#23400 (creating temporary files in source tree), and undefined behavior in test.
50f89d
50f89d
diff --git a/stdlib/test-bz22786.c b/stdlib/test-bz22786.c
50f89d
index e7837f98c19fc4bf..d1aa69106ccf6ac5 100644
50f89d
--- a/stdlib/test-bz22786.c
50f89d
+++ b/stdlib/test-bz22786.c
50f89d
@@ -26,28 +26,20 @@
50f89d
 #include <unistd.h>
50f89d
 #include <sys/stat.h>
50f89d
 #include <sys/types.h>
50f89d
+#include <support/check.h>
50f89d
+#include <support/support.h>
50f89d
+#include <support/temp_file.h>
50f89d
 #include <support/test-driver.h>
50f89d
 #include <libc-diag.h>
50f89d
 
50f89d
 static int
50f89d
 do_test (void)
50f89d
 {
50f89d
-  const char dir[] = "bz22786";
50f89d
-  const char lnk[] = "bz22786/symlink";
50f89d
+  const char *dir = support_create_temp_directory ("bz22786.");
50f89d
+  const char *lnk = xasprintf ("%s/symlink", dir);
50f89d
+  const size_t path_len = (size_t) INT_MAX + strlen (lnk) + 1;
50f89d
 
50f89d
-  rmdir (dir);
50f89d
-  if (mkdir (dir, 0755) != 0 && errno != EEXIST)
50f89d
-    {
50f89d
-      printf ("mkdir %s: %m\n", dir);
50f89d
-      return EXIT_FAILURE;
50f89d
-    }
50f89d
-  if (symlink (".", lnk) != 0 && errno != EEXIST)
50f89d
-    {
50f89d
-      printf ("symlink (%s, %s): %m\n", dir, lnk);
50f89d
-      return EXIT_FAILURE;
50f89d
-    }
50f89d
-
50f89d
-  const size_t path_len = (size_t) INT_MAX + 1;
50f89d
+  TEST_VERIFY_EXIT (symlink (".", lnk) == 0);
50f89d
 
50f89d
   DIAG_PUSH_NEEDS_COMMENT;
50f89d
 #if __GNUC_PREREQ (7, 0)
50f89d
@@ -55,20 +47,14 @@ do_test (void)
50f89d
      allocation to succeed for the test to work.  */
50f89d
   DIAG_IGNORE_NEEDS_COMMENT (7, "-Walloc-size-larger-than=");
50f89d
 #endif
50f89d
-  char *path = malloc (path_len);
50f89d
+  char *path = xmalloc (path_len);
50f89d
   DIAG_POP_NEEDS_COMMENT;
50f89d
 
50f89d
-  if (path == NULL)
50f89d
-    {
50f89d
-      printf ("malloc (%zu): %m\n", path_len);
50f89d
-      return EXIT_UNSUPPORTED;
50f89d
-    }
50f89d
-
50f89d
-  /* Construct very long path = "bz22786/symlink/aaaa....."  */
50f89d
-  char *p = mempcpy (path, lnk, sizeof (lnk) - 1);
50f89d
+  /* Construct very long path = "/tmp/bz22786.XXXX/symlink/aaaa....."  */
50f89d
+  char *p = mempcpy (path, lnk, strlen (lnk));
50f89d
   *(p++) = '/';
50f89d
-  memset (p, 'a', path_len - (path - p) - 2);
50f89d
-  p[path_len - (path - p) - 1] = '\0';
50f89d
+  memset (p, 'a', path_len - (p - path) - 2);
50f89d
+  p[path_len - (p - path) - 1] = '\0';
50f89d
 
50f89d
   /* This call crashes before the fix for bz22786 on 32-bit platforms.  */
50f89d
   p = realpath (path, NULL);
50f89d
@@ -81,7 +67,6 @@ do_test (void)
50f89d
 
50f89d
   /* Cleanup.  */
50f89d
   unlink (lnk);
50f89d
-  rmdir (dir);
50f89d
 
50f89d
   return 0;
50f89d
 }