`ValueEncoderFactory.getScalarEncoder()` fails for values longer than 64 chars
Nobody has claimed this yet.
Assessment
- Difficulty
- 2/5
- Estimated time
- 1-3 hours
- Newbie friendliness
- 78/100
Research direction
Start by locating ValueEncoderFactory.getScalarEncoder(), AsciiValueEncoder.MIN_CHARS_WITHOUT_FLUSH, and StringEncoder.encodeMore(char[], ...). Review the encoder paths for values over 64 characters, then verify that long BigInteger and BigDecimal values work for both char[] and byte[] output, including cases where the remaining buffer is too small.
Written by the indexing model from the issue text.
Description
Note: I can also provide a unit test, but since the repository doesn't seem to have any test infrastructure, I'm not sure what to do with it.
Summary
ValueEncoderFactory.getScalarEncoder(String) chooses the wrong encoder. Values longer than AsciiValueEncoder.MIN_CHARS_WITHOUT_FLUSH (64) get TokenEncoder, which writes the whole value in one call and ignores the end of the buffer. When such a value doesn't fit in the space left in the output buffer, encoding throws StringIndexOutOfBoundsException (char[] output) or ArrayIndexOutOfBoundsException (byte[] output).
A second bug was hidden behind the first: StringEncoder.encodeMore(char[], ...) passes a length to String.getChars() where an end index is expected, so any output after the first chunk is wrong or throws.
Both bugs date back to the initial import.
Impact
Woodstox uses this encoder for writeInteger(BigInteger), writeDecimal(BigDecimal) and their attribute variants. Writing a value whose toString() is longer than 64 characters fails whenever the writer's buffer has at least 64 characters free but not enough for the whole value. That depends on what was written before, so the failure looks intermittent. The exception is unchecked, not an XMLStreamException.
Example: a 100-digit BigInteger fails when 64–99 characters of the output buffer are free.
Fix
- Use
TokenEncoderonly for values of 64 characters or fewer; longer values go to the chunkedStringEncoder. - Pass the correct end index to
getChars()inStringEncoder.
Workaround
Write the value as text: writeCharacters(value.toString()).
- Dominant language
- Java
- Stars
- 42
- Forks
- 21
- Avg merge
- 1d 2h
- Merged PRs (30d)
- 7
Getting set up
We have not checked this project's setup files yet. Start from its README, and see our first-contribution guide for the general steps.
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
More from FasterXML/stax2-api
All issues in FasterXML/stax2-api
Similar issues
-
link-check link-check:manual
Difficulty 2/5 1-3 hours Newbie friendliness 85/100
-
Difficulty 1/5 Under an hour Newbie friendliness 91/100
open-telemetry/opentelemetry-java#8870 ·
Maintainers usually reply within 1 day
-
P2 testing
Difficulty 1/5 Under an hour Newbie friendliness 90/100
Maintainers usually reply within 1 day
-
enhancement javascript
Difficulty 2/5 1-3 hours Newbie friendliness 78/100
Maintainers usually reply within 1 day
-
area/core kind/bug status/triage team/core-shared
Difficulty 2/5 1-3 hours Newbie friendliness 82/100
Maintainers usually reply within 1 day