Skip to content

Commit 48481e3

Browse files
authored
Limit size of exception message (#744)
1 parent fdf25c4 commit 48481e3

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

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

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
*/
2222
public final class BigDecimalParser
2323
{
24+
private final static int MAX_CHARS_TO_REPORT = 1000;
2425
private final char[] chars;
2526

2627
BigDecimalParser(char[] chars) {
@@ -51,7 +52,14 @@ public static BigDecimal parse(char[] chars) {
5152
if (desc == null) {
5253
desc = "Not a valid number representation";
5354
}
54-
throw new NumberFormatException("Value \"" + new String(chars)
55+
String stringToReport;
56+
if (chars.length <= MAX_CHARS_TO_REPORT) {
57+
stringToReport = new String(chars);
58+
} else {
59+
stringToReport = new String(Arrays.copyOfRange(chars, 0, MAX_CHARS_TO_REPORT))
60+
+ "(truncated, full length is " + chars.length + " chars)";
61+
}
62+
throw new NumberFormatException("Value \"" + stringToReport
5563
+ "\" can not be represented as `java.math.BigDecimal`, reason: " + desc);
5664
}
5765
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.fasterxml.jackson.core.io;
2+
3+
public class BigDecimalParserTest extends com.fasterxml.jackson.core.BaseTest {
4+
public void testLongStringParse() {
5+
final int len = 1500;
6+
final StringBuilder sb = new StringBuilder(len);
7+
for (int i = 0; i < len; i++) {
8+
sb.append("A");
9+
}
10+
try {
11+
BigDecimalParser.parse(sb.toString());
12+
fail("expected NumberFormatException");
13+
} catch (NumberFormatException nfe) {
14+
assertTrue("exception message starts as expected?", nfe.getMessage().startsWith("Value \"AAAAA"));
15+
assertTrue("exception message value contains truncated", nfe.getMessage().contains("truncated"));
16+
}
17+
}
18+
}

0 commit comments

Comments
 (0)