8168923: Use unsigned random long in a temp directory name
authorigerasim
Wed, 02 Nov 2016 18:45:14 +0300
changeset 41881 b26ea3cb64c5
parent 41835 90450650fe74
child 41882 0549e50db4bf
8168923: Use unsigned random long in a temp directory name Reviewed-by: bpb, alanb
jdk/src/java.base/share/classes/java/io/File.java
jdk/src/java.base/share/classes/java/nio/file/TempFileHelper.java
--- a/jdk/src/java.base/share/classes/java/io/File.java	Wed Nov 02 14:46:40 2016 +0000
+++ b/jdk/src/java.base/share/classes/java/io/File.java	Wed Nov 02 18:45:14 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 1994, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 1994, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -1907,16 +1907,10 @@
             throws IOException
         {
             long n = random.nextLong();
-            if (n == Long.MIN_VALUE) {
-                n = 0;      // corner case
-            } else {
-                n = Math.abs(n);
-            }
 
             // Use only the file name from the supplied prefix
             prefix = (new File(prefix)).getName();
-
-            String name = prefix + Long.toString(n) + suffix;
+            String name = prefix + Long.toUnsignedString(n) + suffix;
             File f = new File(dir, name);
             if (!name.equals(f.getName()) || f.isInvalid()) {
                 if (System.getSecurityManager() != null)
--- a/jdk/src/java.base/share/classes/java/nio/file/TempFileHelper.java	Wed Nov 02 14:46:40 2016 +0000
+++ b/jdk/src/java.base/share/classes/java/nio/file/TempFileHelper.java	Wed Nov 02 18:45:14 2016 +0300
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2009, 2013, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2009, 2016, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -55,8 +55,8 @@
     private static final SecureRandom random = new SecureRandom();
     private static Path generatePath(String prefix, String suffix, Path dir) {
         long n = random.nextLong();
-        n = (n == Long.MIN_VALUE) ? 0 : Math.abs(n);
-        Path name = dir.getFileSystem().getPath(prefix + Long.toString(n) + suffix);
+        String s = prefix + Long.toUnsignedString(n) + suffix;
+        Path name = dir.getFileSystem().getPath(s);
         // the generated name should be a simple file name
         if (name.getParent() != null)
             throw new IllegalArgumentException("Invalid prefix or suffix");