Skip to content

Commit

Permalink
Limit size of exception message (#744)
Browse files Browse the repository at this point in the history
  • Loading branch information
pjfanning authored Apr 1, 2022
1 parent fdf25c4 commit 48481e3
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
*/
public final class BigDecimalParser
{
private final static int MAX_CHARS_TO_REPORT = 1000;
private final char[] chars;

BigDecimalParser(char[] chars) {
Expand Down Expand Up @@ -51,7 +52,14 @@ public static BigDecimal parse(char[] chars) {
if (desc == null) {
desc = "Not a valid number representation";
}
throw new NumberFormatException("Value \"" + new String(chars)
String stringToReport;
if (chars.length <= MAX_CHARS_TO_REPORT) {
stringToReport = new String(chars);
} else {
stringToReport = new String(Arrays.copyOfRange(chars, 0, MAX_CHARS_TO_REPORT))
+ "(truncated, full length is " + chars.length + " chars)";
}
throw new NumberFormatException("Value \"" + stringToReport
+ "\" can not be represented as `java.math.BigDecimal`, reason: " + desc);
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package com.fasterxml.jackson.core.io;

public class BigDecimalParserTest extends com.fasterxml.jackson.core.BaseTest {
public void testLongStringParse() {
final int len = 1500;
final StringBuilder sb = new StringBuilder(len);
for (int i = 0; i < len; i++) {
sb.append("A");
}
try {
BigDecimalParser.parse(sb.toString());
fail("expected NumberFormatException");
} catch (NumberFormatException nfe) {
assertTrue("exception message starts as expected?", nfe.getMessage().startsWith("Value \"AAAAA"));
assertTrue("exception message value contains truncated", nfe.getMessage().contains("truncated"));
}
}
}

0 comments on commit 48481e3

Please sign in to comment.