changeset 56433:bcc706c719d7

8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes) Reviewed-by: dholmes, sgehwolf
author mbaesken
date Thu, 25 Jul 2019 12:15:27 +0200
parents 26f71e24dd24
children 2b38409a336e
files src/java.base/linux/classes/jdk/internal/platform/cgroupv1/SubSystem.java test/lib/jdk/test/lib/containers/cgroup/MetricsTester.java
diffstat 2 files changed, 22 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/java.base/linux/classes/jdk/internal/platform/cgroupv1/SubSystem.java	Fri Jul 26 15:05:24 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	Fri Jul 26 15:05:24 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;