Index: libgfortran/io/unix.c =================================================================== --- libgfortran/io/unix.c (revision 170899) +++ libgfortran/io/unix.c (working copy) @@ -1055,7 +1055,8 @@ tempfile (st_parameter_open *opp) ) slash = ""; - template = get_mem (strlen (tempdir) + 20); + // The maximal string length is in the mktemp() case below. + template = get_mem (strlen (tempdir) + 23); #ifdef HAVE_MKSTEMP sprintf (template, "%s%sgfortrantmpXXXXXX", tempdir, slash); @@ -1066,7 +1067,17 @@ tempfile (st_parameter_open *opp) fd = -1; do { - sprintf (template, "%s%sgfortrantmpXXXXXX", tempdir, slash); + /* Some implementations of mktemp(), following old BSD behavior, + only allow 26 different names. So, we add a short almost-unique + prefix ourselves, to work around this issue. */ + static int count = 0; + char l3 = 'a' + count % 26; + char l2 = 'a' + (count / 26) % 26; + char l1 = 'a' + (count / (26 * 26)) % 26; + count++; + sprintf (template, "%s%sgfortrantmp%c%c%cXXXXXX", tempdir, slash, + l1, l2, l3); + if (!mktemp (template)) break; #if defined(HAVE_CRLF) && defined(O_BINARY)