8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes)
authormbaesken
Thu, 25 Jul 2019 12:15:27 +0200
changeset 57535 51286afcbf49
parent 57534 de62b363bbe6
child 57536 67cce1b84a9a
8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes) Reviewed-by: dholmes, sgehwolf
src/java.base/linux/classes/jdk/internal/platform/cgroupv1/SubSystem.java
test/lib/jdk/test/lib/containers/cgroup/MetricsTester.java
--- a/src/java.base/linux/classes/jdk/internal/platform/cgroupv1/SubSystem.java	Wed Jul 24 09:28:48 2019 +0200
+++ b/src/java.base/linux/classes/jdk/internal/platform/cgroupv1/SubSystem.java	Thu Jul 25 12:15:27 2019 +0200
@@ -132,7 +132,7 @@
             retval = Long.parseLong(strval);
         } catch (NumberFormatException e) {
             // For some properties (e.g. memory.limit_in_bytes) we may overflow the range of signed long.
-            // In this case, return Long.max
+            // In this case, return Long.MAX_VALUE
             BigInteger b = new BigInteger(strval);
             if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
                 return Long.MAX_VALUE;
--- a/test/lib/jdk/test/lib/containers/cgroup/MetricsTester.java	Wed Jul 24 09:28:48 2019 +0200
+++ b/test/lib/jdk/test/lib/containers/cgroup/MetricsTester.java	Thu Jul 25 12:15:27 2019 +0200
@@ -26,6 +26,7 @@
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.IOException;
+import java.math.BigInteger;
 import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
@@ -157,7 +158,24 @@
 
     private static long getLongValueFromFile(SubSystem subSystem, String fileName) {
         String data = getFileContents(subSystem, fileName);
-        return data.isEmpty() ? 0L : Long.parseLong(data);
+        return data.isEmpty() ? 0L : convertStringToLong(data);
+    }
+
+    private static long convertStringToLong(String strval) {
+        long retval = 0;
+        if (strval == null) return 0L;
+
+        try {
+            retval = Long.parseLong(strval);
+        } catch (NumberFormatException e) {
+            // For some properties (e.g. memory.limit_in_bytes) we may overflow the range of signed long.
+            // In this case, return Long.MAX_VALUE
+            BigInteger b = new BigInteger(strval);
+            if (b.compareTo(BigInteger.valueOf(Long.MAX_VALUE)) > 0) {
+                return Long.MAX_VALUE;
+            }
+        }
+        return retval;
     }
 
     private static long getLongValueFromFile(SubSystem subSystem, String metric, String subMetric) {
@@ -165,7 +183,8 @@
         String[] tokens = stats.split("[\\r\\n]+");
         for (int i = 0; i < tokens.length; i++) {
             if (tokens[i].startsWith(subMetric)) {
-                return Long.parseLong(tokens[i].split("\\s+")[1]);
+                String strval = tokens[i].split("\\s+")[1];
+                return convertStringToLong(strval);
             }
         }
         return 0L;