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

Limit size of exception message in BigDecimalParser #744

Merged
Merged
Show file tree
Hide file tree
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
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"));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is also helper method verifyException in test base class for simple inclusion checks.
(this is fine too)

assertTrue("exception message value contains truncated", nfe.getMessage().contains("truncated"));
}
}
}