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