mirror of
https://github.com/openjdk/jdk.git
synced 2025-09-22 12:04:39 +02:00
6721753: File.createTempFile produces guessable file names
Reviewed-by: sherman
This commit is contained in:
parent
3a902871ef
commit
01bf987244
1 changed files with 26 additions and 28 deletions
|
@ -33,9 +33,9 @@ import java.net.URISyntaxException;
|
|||
import java.util.ArrayList;
|
||||
import java.util.Map;
|
||||
import java.util.Hashtable;
|
||||
import java.util.Random;
|
||||
import java.security.AccessController;
|
||||
import java.security.AccessControlException;
|
||||
import java.security.SecureRandom;
|
||||
import sun.security.action.GetPropertyAction;
|
||||
|
||||
|
||||
|
@ -1678,28 +1678,28 @@ public class File
|
|||
|
||||
/* -- Temporary files -- */
|
||||
|
||||
private static final Object tmpFileLock = new Object();
|
||||
// lazy initialization of SecureRandom and temporary file directory
|
||||
private static class LazyInitialization {
|
||||
static final SecureRandom random = new SecureRandom();
|
||||
|
||||
private static int counter = -1; /* Protected by tmpFileLock */
|
||||
static final String temporaryDirectory = temporaryDirectory();
|
||||
static String temporaryDirectory() {
|
||||
return fs.normalize(
|
||||
AccessController.doPrivileged(
|
||||
new GetPropertyAction("java.io.tmpdir")));
|
||||
}
|
||||
}
|
||||
|
||||
private static File generateFile(String prefix, String suffix, File dir)
|
||||
throws IOException
|
||||
{
|
||||
if (counter == -1) {
|
||||
counter = new Random().nextInt() & 0xffff;
|
||||
long n = LazyInitialization.random.nextLong();
|
||||
if (n == Long.MIN_VALUE) {
|
||||
n = 0; // corner case
|
||||
} else {
|
||||
n = Math.abs(n);
|
||||
}
|
||||
counter++;
|
||||
return new File(dir, prefix + Integer.toString(counter) + suffix);
|
||||
}
|
||||
|
||||
private static String tmpdir; /* Protected by tmpFileLock */
|
||||
|
||||
private static String getTempDir() {
|
||||
if (tmpdir == null)
|
||||
tmpdir = fs.normalize(
|
||||
AccessController.doPrivileged(
|
||||
new GetPropertyAction("java.io.tmpdir")));
|
||||
return tmpdir;
|
||||
return new File(dir, prefix + Long.toString(n) + suffix);
|
||||
}
|
||||
|
||||
private static boolean checkAndCreate(String filename, SecurityManager sm)
|
||||
|
@ -1795,9 +1795,8 @@ public class File
|
|||
if (prefix.length() < 3)
|
||||
throw new IllegalArgumentException("Prefix string too short");
|
||||
String s = (suffix == null) ? ".tmp" : suffix;
|
||||
synchronized (tmpFileLock) {
|
||||
if (directory == null) {
|
||||
String tmpDir = getTempDir();
|
||||
String tmpDir = LazyInitialization.temporaryDirectory();
|
||||
directory = new File(tmpDir, fs.prefixLength(tmpDir));
|
||||
}
|
||||
SecurityManager sm = System.getSecurityManager();
|
||||
|
@ -1807,7 +1806,6 @@ public class File
|
|||
} while (!checkAndCreate(f.getPath(), sm));
|
||||
return f;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates an empty file in the default temporary-file directory, using
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue