Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exception message generation uses too much memory #1333

Merged
merged 1 commit into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}