# HG changeset patch # User mbaesken # Date 1564049727 -7200 # Node ID 51286afcbf4962a8282a33b41d356f69941fc841 # Parent de62b363bbe6cfd09ab41d064c3f3e3d06aa4e9a 8228585: jdk/internal/platform/cgroup/TestCgroupMetrics.java - NumberFormatException because of large long values (memory limit_in_bytes) Reviewed-by: dholmes, sgehwolf diff -r de62b363bbe6 -r 51286afcbf49 src/java.base/linux/classes/jdk/internal/platform/cgroupv1/SubSystem.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; diff -r de62b363bbe6 -r 51286afcbf49 test/lib/jdk/test/lib/containers/cgroup/MetricsTester.java --- 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;