Skip to content

Commit

Permalink
exception message generation uses too much memory (#1333)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning committed Sep 12, 2024
1 parent c86938e commit f90ba05
Showing 1 changed file with 32 additions and 7 deletions.
39 changes: 32 additions & 7 deletions src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public static BigDecimal parse(final char[] chars, final int off, final int len)
// 20-Aug-2022, tatu: Although "new BigDecimal(...)" only throws NumberFormatException
// operations by "parseBigDecimal()" can throw "ArithmeticException", so handle both:
} catch (ArithmeticException | NumberFormatException e) {
throw _parseFailure(e, new String(chars, off, len));
throw _parseFailure(e, chars, off, len);
}
}

Expand Down Expand Up @@ -115,7 +115,7 @@ public static BigDecimal parseWithFastParser(final char[] ch, final int off, fin
try {
return JavaBigDecimalParser.parseBigDecimal(ch, off, len);
} catch (ArithmeticException | NumberFormatException e) {
throw _parseFailure(e, new String(ch, off, len));
throw _parseFailure(e, ch, off, len);
}
}

Expand All @@ -126,18 +126,43 @@ private static NumberFormatException _parseFailure(Exception e, String fullValue
desc = "Not a valid number representation";
}
String valueToReport = _getValueDesc(fullValue);
return new NumberFormatException("Value " + valueToReport
+ " can not be deserialized as `java.math.BigDecimal`, reason: " + desc);
return new NumberFormatException(_generateExceptionMessage(valueToReport, desc));
}

private static String _getValueDesc(String fullValue) {
private static NumberFormatException _parseFailure(final Exception e,
final char[] array,
final int offset,
final int len) {
String desc = e.getMessage();
// 05-Feb-2021, tatu: Alas, JDK mostly has null message so:
if (desc == null) {
desc = "Not a valid number representation";
}
String valueToReport = _getValueDesc(array, offset, len);
return new NumberFormatException(_generateExceptionMessage(valueToReport, desc));
}

private static String _getValueDesc(final String fullValue) {
final int len = fullValue.length();
if (len <= MAX_CHARS_TO_REPORT) {
return String.format("\"%s\"", fullValue);
}
return String.format("\"%s\" (truncated to %d chars (from %d))",
fullValue.substring(0, MAX_CHARS_TO_REPORT),
MAX_CHARS_TO_REPORT, len);
fullValue.substring(0, MAX_CHARS_TO_REPORT),
MAX_CHARS_TO_REPORT, len);
}

private static String _getValueDesc(final char[] array, final int offset, final int len) {
if (len <= MAX_CHARS_TO_REPORT) {
return String.format("\"%s\"", new String(array, offset, len));
}
return String.format("\"%s\" (truncated to %d chars (from %d))",
new String(array, offset, MAX_CHARS_TO_REPORT),
MAX_CHARS_TO_REPORT, len);
}

private static String _generateExceptionMessage(final String valueToReport, final String desc) {
return String.format("Value %s can not be deserialized as `java.math.BigDecimal`, reason: %s" ,
valueToReport, desc);
}
}

0 comments on commit f90ba05

Please sign in to comment.