Skip to content

Commit ed518cd

Browse files
committed
Improve SNBT floating point error handling
1 parent a5cc087 commit ed518cd

1 file changed

Lines changed: 11 additions & 6 deletions

File tree

  • src/main/java/org/glavo/nbt/internal/snbt

‎src/main/java/org/glavo/nbt/internal/snbt/Token.java‎

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -125,13 +125,18 @@ static NumberToken parse(CharSequence value, int beginIndex, int endIndex) {
125125
throw invalidNumberLiteral(value, beginIndex, endIndex);
126126
}
127127

128-
double doubleValue = Double.parseDouble(clean.subSequence(cleanBeginIndex, cleanEndIndex).toString().replace("_", ""));
129-
if (!Double.isFinite(doubleValue)
130-
|| floatingType == FloatingType.FLOAT && (doubleValue < -Float.MAX_VALUE || doubleValue > Float.MAX_VALUE)) {
131-
throw invalidNumberLiteral(value, beginIndex, endIndex);
128+
try {
129+
double doubleValue = Double.parseDouble(clean.subSequence(cleanBeginIndex, cleanEndIndex).toString().replace("_", ""));
130+
if (!Double.isFinite(doubleValue)
131+
|| floatingType == FloatingType.FLOAT && (doubleValue < -Float.MAX_VALUE || doubleValue > Float.MAX_VALUE)) {
132+
throw new IllegalArgumentException("Invalid floating point number: " + doubleValue);
133+
}
134+
return new Token.FloatingToken(doubleValue, floatingType);
135+
} catch (Exception e) {
136+
IllegalArgumentException e2 = invalidNumberLiteral(value, beginIndex, endIndex);
137+
e2.initCause(e);
138+
throw e2;
132139
}
133-
134-
return new Token.FloatingToken(doubleValue, floatingType);
135140
} else {
136141
IntegralToken.Suffix suffix = IntegralToken.Suffix.check(clean, cleanBeginIndex, cleanEndIndex, radix);
137142
cleanEndIndex -= suffix.value().length();

0 commit comments

Comments
 (0)