Skip to content

Commit f90ba05

Browse files
authored
exception message generation uses too much memory (#1333)
1 parent c86938e commit f90ba05

File tree

1 file changed

+32
-7
lines changed

1 file changed

+32
-7
lines changed

src/main/java/com/fasterxml/jackson/core/io/BigDecimalParser.java

Lines changed: 32 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public static BigDecimal parse(final char[] chars, final int off, final int len)
6363
// 20-Aug-2022, tatu: Although "new BigDecimal(...)" only throws NumberFormatException
6464
// operations by "parseBigDecimal()" can throw "ArithmeticException", so handle both:
6565
} catch (ArithmeticException | NumberFormatException e) {
66-
throw _parseFailure(e, new String(chars, off, len));
66+
throw _parseFailure(e, chars, off, len);
6767
}
6868
}
6969

@@ -115,7 +115,7 @@ public static BigDecimal parseWithFastParser(final char[] ch, final int off, fin
115115
try {
116116
return JavaBigDecimalParser.parseBigDecimal(ch, off, len);
117117
} catch (ArithmeticException | NumberFormatException e) {
118-
throw _parseFailure(e, new String(ch, off, len));
118+
throw _parseFailure(e, ch, off, len);
119119
}
120120
}
121121

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

133-
private static String _getValueDesc(String fullValue) {
132+
private static NumberFormatException _parseFailure(final Exception e,
133+
final char[] array,
134+
final int offset,
135+
final int len) {
136+
String desc = e.getMessage();
137+
// 05-Feb-2021, tatu: Alas, JDK mostly has null message so:
138+
if (desc == null) {
139+
desc = "Not a valid number representation";
140+
}
141+
String valueToReport = _getValueDesc(array, offset, len);
142+
return new NumberFormatException(_generateExceptionMessage(valueToReport, desc));
143+
}
144+
145+
private static String _getValueDesc(final String fullValue) {
134146
final int len = fullValue.length();
135147
if (len <= MAX_CHARS_TO_REPORT) {
136148
return String.format("\"%s\"", fullValue);
137149
}
138150
return String.format("\"%s\" (truncated to %d chars (from %d))",
139-
fullValue.substring(0, MAX_CHARS_TO_REPORT),
140-
MAX_CHARS_TO_REPORT, len);
151+
fullValue.substring(0, MAX_CHARS_TO_REPORT),
152+
MAX_CHARS_TO_REPORT, len);
141153
}
142154

155+
private static String _getValueDesc(final char[] array, final int offset, final int len) {
156+
if (len <= MAX_CHARS_TO_REPORT) {
157+
return String.format("\"%s\"", new String(array, offset, len));
158+
}
159+
return String.format("\"%s\" (truncated to %d chars (from %d))",
160+
new String(array, offset, MAX_CHARS_TO_REPORT),
161+
MAX_CHARS_TO_REPORT, len);
162+
}
163+
164+
private static String _generateExceptionMessage(final String valueToReport, final String desc) {
165+
return String.format("Value %s can not be deserialized as `java.math.BigDecimal`, reason: %s" ,
166+
valueToReport, desc);
167+
}
143168
}

0 commit comments

Comments
 (0)