Skip to content

Commit

Permalink
Handle long values above max
Browse files Browse the repository at this point in the history
  • Loading branch information
dbwiddis committed Dec 31, 2019
1 parent 6bdff1c commit 4285cfa
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
11 changes: 6 additions & 5 deletions oshi-core/src/main/java/oshi/util/ParseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -662,7 +662,8 @@ public static String removeMatchingString(final String original, final String to
* sanity checks of data.
*
* As a special case, non-numeric fields (such as UUIDs in OpenVZ) at the end of
* the list are ignored.
* the list are ignored. Values greater than the max long value return the max
* long value.
*
* The indices parameters are referenced assuming the length as specified, and
* leading characters are ignored. For example, if the string is "foo 12 34 5"
Expand Down Expand Up @@ -721,11 +722,11 @@ public static long[] parseStringToLongArray(String s, int[] indices, int length,
// Doesn't impact parsing, ignore
delimCurrent = false;
} else if (c >= '0' && c <= '9' && !dashSeen) {
if (power > 18) {
LOG.error("Number is too big for a long parsing string '{}' to long array", s);
return new long[indices.length];
if (power > 18 || power == 17 && c == '9' && parsed[parsedIndex] > 223372036854775807L) {
parsed[parsedIndex] = Long.MAX_VALUE;
} else {
parsed[parsedIndex] += (c - '0') * ParseUtil.POWERS_OF_TEN[power++];
}
parsed[parsedIndex] += (c - '0') * ParseUtil.POWERS_OF_TEN[power++];
delimCurrent = false;
} else if (c == '-') {
parsed[parsedIndex] *= -1L;
Expand Down
4 changes: 2 additions & 2 deletions oshi-core/src/test/java/oshi/util/ParseUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -347,9 +347,9 @@ public void testParseStringToLongArray() {
result = ParseUtil.parseStringToLongArray(foo, indices, 4, ' ');
assertEquals(0, result[1]);

foo = String.format("Exceeds max long %d %d %d %d0", 123, 456, 789, Long.MAX_VALUE);
foo = String.format("Exceeds max long %d %d %d 1%d", 123, 456, 789, Long.MAX_VALUE);
result = ParseUtil.parseStringToLongArray(foo, indices, 4, ' ');
assertEquals(0, result[1]);
assertEquals(Long.MAX_VALUE, result[1]);

foo = String.format("String too short %d %d %d %d", 123, 456, 789, now);
result = ParseUtil.parseStringToLongArray(foo, indices, 9, ' ');
Expand Down

0 comments on commit 4285cfa

Please sign in to comment.