You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2015/02/25 01:25:56 UTC

svn commit: r1662147 - in /lucene/dev/branches/branch_5x: ./ lucene/ lucene/CHANGES.txt lucene/core/ lucene/core/src/java/org/apache/lucene/util/StringHelper.java

Author: rmuir
Date: Wed Feb 25 00:25:56 2015
New Revision: 1662147

URL: http://svn.apache.org/r1662147
Log:
LUCENE-6292: seed StringHelper better

Modified:
    lucene/dev/branches/branch_5x/   (props changed)
    lucene/dev/branches/branch_5x/lucene/   (props changed)
    lucene/dev/branches/branch_5x/lucene/CHANGES.txt   (contents, props changed)
    lucene/dev/branches/branch_5x/lucene/core/   (props changed)
    lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/util/StringHelper.java

Modified: lucene/dev/branches/branch_5x/lucene/CHANGES.txt
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/CHANGES.txt?rev=1662147&r1=1662146&r2=1662147&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/CHANGES.txt (original)
+++ lucene/dev/branches/branch_5x/lucene/CHANGES.txt Wed Feb 25 00:25:56 2015
@@ -138,6 +138,8 @@ Other
 * LUCENE-6239: Removed RAMUsageEstimator's sun.misc.Unsafe calls.
   (Robert Muir, Dawid Weiss, Uwe Schindler)
 
+* LUCENE-6292: Seed StringHelper better. (Robert Muir)
+
 Changes in Runtime Behavior
 
 * LUCENE-6255: PhraseQuery now ignores leading holes and requires that

Modified: lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/util/StringHelper.java
URL: http://svn.apache.org/viewvc/lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/util/StringHelper.java?rev=1662147&r1=1662146&r2=1662147&view=diff
==============================================================================
--- lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/util/StringHelper.java (original)
+++ lucene/dev/branches/branch_5x/lucene/core/src/java/org/apache/lucene/util/StringHelper.java Wed Feb 25 00:25:56 2015
@@ -17,7 +17,10 @@ package org.apache.lucene.util;
  * limitations under the License.
  */
 
+import java.io.DataInputStream;
 import java.math.BigInteger;
+import java.nio.file.Files;
+import java.nio.file.Paths;
 import java.util.Arrays;
 import java.util.Properties;
 
@@ -253,18 +256,30 @@ public abstract class StringHelper {
       x0 = Long.parseLong(prop, 16);
       x1 = x0;
     } else {
-      // Randomess from 3 different sources:
-      x0 = System.nanoTime();
-      x1 = StringHelper.class.hashCode() << 32;
-      StringBuilder sb = new StringBuilder();
-      // Properties can vary across JVM instances:
-      Properties p = System.getProperties();
-      for (String s: p.stringPropertyNames()) {
-        sb.append(s);
-        sb.append(p.getProperty(s));
+      // seed from /dev/urandom, if its available
+      try (DataInputStream is = new DataInputStream(Files.newInputStream(Paths.get("/dev/urandom")))) {
+        x0 = is.readLong();
+        x1 = is.readLong();
+      } catch (Exception unavailable) {
+        // may not be available on this platform
+        // fall back to lower quality randomness from 3 different sources:
+        x0 = System.nanoTime();
+        x1 = StringHelper.class.hashCode() << 32;
+        
+        StringBuilder sb = new StringBuilder();
+        // Properties can vary across JVM instances:
+        try {
+          Properties p = System.getProperties();
+          for (String s: p.stringPropertyNames()) {
+            sb.append(s);
+            sb.append(p.getProperty(s));
+          }
+          x1 |= sb.toString().hashCode();
+        } catch (SecurityException notallowed) {
+          // getting Properties requires wildcard read-write: may not be allowed
+          x1 |= StringBuffer.class.hashCode();
+        }
       }
-      x1 |= sb.toString().hashCode();
-      // TODO: maybe read from /dev/urandom when it's available?
     }
 
     // Use a few iterations of xorshift128 to scatter the seed