Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

`CBORParser.getText(Writer)` writes stale text for numeric tokens, NPE for binary

Closed Beginner friendly
#844 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
84/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
java

Research direction

Start by locating CBORParser.getText(Writer) and compare it with getText() and the suggested Smile fix in #835. Add regression coverage for numeric tokens and VALUE_EMBEDDED_OBJECT using the supplied reproduction, verifying writer output and returned lengths match the expected behavior without an exception.

Written by the indexing model from the issue text.

Description

cbor

Same problem as #835 (Smile), in CBORParser.getText(Writer):

  1. For numeric tokens, it writes the contents of _textBuffer (or _sharedString). CBOR never decodes numbers into _textBuffer, so whatever text was last decoded gets written, usually the previous String value. The return value is that text's length.
  2. For VALUE_EMBEDDED_OBJECT (binary), t.asCharArray() returns null, so writer.write(null) throws NullPointerException. getText() returns null for the same token.

getText() handles both cases correctly.

Reproduction
CBORFactory f = new CBORFactory();
ByteArrayOutputStream bo = new ByteArrayOutputStream();
try (JsonGenerator g = f.createGenerator(bo)) {
    g.writeStartArray();
    g.writeString("abcdefghijklmnopqrstuvwxyz0123456789");
    g.writeNumber(42);
    g.writeNumber(1.25);
    g.writeNumber(new BigInteger("123456789012345678901234567890"));
    g.writeBinary(new byte[] { 1, 2, 3 });
    g.writeEndArray();
}
try (JsonParser p = f.createParser(bo.toByteArray())) {
    JsonToken t;
    while ((t = p.nextToken()) != null) {
        if (t.isScalarValue()) {
            StringWriter w = new StringWriter();
            int n = p.getText(w);
            System.out.println(t + ": getText()=" + p.getText() + ", getText(Writer)=" + w + " (" + n + ")");
        }
    }
}

Observed (2.x; same code on 2.21), for both byte[] and InputStream input:

Token getText() getText(Writer)
VALUE_STRING "abcdefghij..." "abcdefghij..." (36)
VALUE_NUMBER_INT 42 "42" "abcdefghij..." (36)
VALUE_NUMBER_FLOAT 1.25 "1.25" "abcdefghij..." (36)
VALUE_NUMBER_INT (BigInteger) "1234567890..." "abcdefghij..." (36)
VALUE_EMBEDDED_OBJECT null NullPointerException
Suggested fix

As in #835: handle VALUE_STRING (and FIELD_NAME) directly, and fall back to super.getText(writer) for everything else. jackson-core's default goes through getText() and writes nothing for null.

Dominant language
Java
Stars
348
Forks
159
Avg merge
1d 1h
Merged PRs (30d)
44

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from FasterXML/jackson-dataformats-binary

All issues in FasterXML/jackson-dataformats-binary

Similar issues

More Java issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.