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