< prev index next >

src/hotspot/share/jfr/periodic/sampling/jfrThreadSampler.cpp

Print this page

        

@@ -1,7 +1,7 @@
 /*
- * Copyright (c) 2012, 2018, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2012, 2019, 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
  * under the terms of the GNU General Public License version 2 only, as
  * published by the Free Software Foundation.

@@ -465,12 +465,21 @@
     jlong java_interval = _interval_java == 0 ? max_jlong : MAX2<jlong>(_interval_java, 10);
     jlong native_interval = _interval_native == 0 ? max_jlong : MAX2<jlong>(_interval_native, 10);
 
     jlong now_ms = get_monotonic_ms();
 
-    jlong next_j = java_interval + last_java_ms - now_ms;
-    jlong next_n = native_interval + last_native_ms - now_ms;
+    /*
+     * Let I be java_interval or native_interval.
+     * Let L be last_java_ms or last_native_ms.
+     * Let N be now_ms.
+     *
+     * Interval, I, might be max_jlong so the addition
+     * could potentially overflow without parenthesis (UB). Also note that
+     * L - N < 0. Avoid UB, by adding parenthesis.
+     */
+    jlong next_j = java_interval + (last_java_ms - now_ms);
+    jlong next_n = native_interval + (last_native_ms - now_ms);
 
     jlong sleep_to_next = MIN2<jlong>(next_j, next_n);
 
     if (sleep_to_next > 0) {
       os::naked_short_sleep(sleep_to_next);
< prev index next >