OutOfMemoryError when writing BigDecimal
See original GitHub issueWhen I’ve enabled the WRITE_BIGDECIMAL_AS_PLAIN
setting on Jackson 2.7.5, Jackson will attempt to write out the whole number, no matter how large the exponent.
For example, the following code:
ObjectMapper mapper = new ObjectMapper().enable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
mapper.writeValueAsString(new java.math.BigDecimal("9.223372E+1010671858"));
triggers the exception:
java.lang.OutOfMemoryError: Java heap space
at java.lang.AbstractStringBuilder.<init>(AbstractStringBuilder.java:68)
at java.lang.StringBuilder.<init>(StringBuilder.java:101)
at java.math.BigDecimal.toPlainString(BigDecimal.java:2964)
at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeNumber(WriterBasedJsonGenerator.java:690)
at com.fasterxml.jackson.databind.ser.std.NumberSerializer.serialize(NumberSerializer.java:45)
at com.fasterxml.jackson.databind.ser.std.NumberSerializer.serialize(NumberSerializer.java:19)
at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:130)
at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:3612)
at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:2980)
... 23 elided
I know technically Jackson is doing what you’re telling it to do (so if you don’t feel this is an issue feel free to close it). But it would be nice if WRITE_BIGDECIMAL_AS_PLAIN
set a reasonable length on the number, so as not to leave users open to denial of service vulnerabilities.
(Actually, I think this might technically be an issue in jackson-core; let me know if I should resubmit.)
Issue Analytics
- State:
- Created 7 years ago
- Comments:8 (5 by maintainers)
Top Results From Across the Web
OutOfMemoryError when writing BigDecimal #315 - GitHub
The first step that I will do is to simply throw an exception if attempts is made with JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN and scale ...
Read more >OutOfMemoryError on BigInteger - java - Stack Overflow
I'm writing a polish notation calculator for BigIntegers (just *, ^ and !) and I'm getting an OutOfMemoryError on the line where I'm...
Read more >java.lang.OutOfMemoryError: Java heap space | JDELIST.com
The BigDecimal / MathNumeric error was traced to the Initialize Event of Form C - Time Entry and Revisions - in Application P311221...
Read more >All you need to know about Java's BigDecimal | Piotr Horzycki
A guide to Java BigDecimal class. Examples of monetary calculations and formatting decimal numbers for different languages.
Read more >Testing code for excessively large inputs - Tomasz Nurkiewicz
When writing unit tests we mostly focus on business correctness. ... extremely long bicycle ride crashed our system with OutOfMemoryError .
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
@cowtowncoder I guess it really depends on how people use that setting. For me a maximum of ten zeroes would be fine, but there’s no limit that’s going to satisfy everyone. I see that setting mainly as a minor formatting switch to avoid writing things like 1.1E2 instead of 110; most people probably don’t want it to write out a 1000-digit number, though.
Just realized that a better place this is with
jackson-core
, since that’s where it can be more reliably caught. So recreated as:https://github.com/FasterXML/jackson-core/issues/315