7077172: KerberosTime does not take into account system clock adjustement
authorweijun
Fri, 18 Nov 2011 16:13:39 +0800
changeset 11025 d7f6c062c454
parent 11024 a35b10c0eec3
child 11026 41ce4af91010
7077172: KerberosTime does not take into account system clock adjustement Reviewed-by: valeriep
jdk/src/share/classes/sun/security/krb5/internal/KerberosTime.java
--- a/jdk/src/share/classes/sun/security/krb5/internal/KerberosTime.java	Thu Nov 17 15:47:34 2011 -0800
+++ b/jdk/src/share/classes/sun/security/krb5/internal/KerberosTime.java	Fri Nov 18 16:13:39 2011 +0800
@@ -68,8 +68,8 @@
     private int  microSeconds; // the last three digits of the microsecond value
 
     // The time when this class is loaded. Used in setNow()
-    private static final long initMilli = System.currentTimeMillis();
-    private static final long initMicro = System.nanoTime() / 1000;
+    private static long initMilli = System.currentTimeMillis();
+    private static long initMicro = System.nanoTime() / 1000;
 
     private static long syncTime;
     private static boolean DEBUG = Krb5.DEBUG;
@@ -212,9 +212,22 @@
     }
 
     public void setNow() {
-        long microElapsed = System.nanoTime() / 1000 - initMicro;
-        setTime(initMilli + microElapsed/1000);
-        microSeconds = (int)(microElapsed % 1000);
+        long newMilli = System.currentTimeMillis();
+        long newMicro = System.nanoTime() / 1000;
+        long microElapsed = newMicro - initMicro;
+        long calcMilli = initMilli + microElapsed/1000;
+        if (calcMilli - newMilli > 100 || newMilli - calcMilli > 100) {
+            if (DEBUG) {
+                System.out.println("System time adjusted");
+            }
+            initMilli = newMilli;
+            initMicro = newMicro;
+            setTime(newMilli);
+            microSeconds = 0;
+        } else {
+            setTime(calcMilli);
+            microSeconds = (int)(microElapsed % 1000);
+        }
     }
 
     public int getMicroSeconds() {