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

ArrayOutOfBoundException at WriterBasedJsonGenerator.writeString(Reader, int) #702

Closed
jeffreye opened this issue Jun 17, 2021 · 4 comments
Closed
Milestone

Comments

@jeffreye
Copy link

I am invoking writeString(myReader, -1) on a JsonGenerator and get this error sometimes.

java.lang.ArrayIndexOutOfBoundsException: Index 4000 out of bounds for length 4000
	at com.fasterxml.jackson.core.json.WriterBasedJsonGenerator.writeString(WriterBasedJsonGenerator.java:466) ~[jackson-core-2.12.0.jar:2.12.0]

//Add trailing quote
if ((_outputTail + len) >= _outputEnd) {
_flushBuffer();
}
_outputBuffer[_outputTail++] = _quoteChar;
if (toRead > 0 && len >= 0) {
_reportError("Didn't read enough from reader");
}

Basically, we need to add this case to the line

(_outputTail + len) >= _outputEnd
@cowtowncoder
Copy link
Member

Sounds like a flaw to fix, yes.

Do you happen to have a reproduction to use as a unit test? It would be great to verify the fix.

@jeffreye
Copy link
Author

        final ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
        final JsonFactory factory = new ObjectMapper().getFactory();
        final JsonGenerator generator = factory.createGenerator(new PrintWriter(byteArrayOutputStream));

        generator.writeStartArray();
        
        // create a relative big string to fill the _outputBuffer
        for (int i = 0; i < 799; i++) {
            generator.writeString(new StringReader("ab"), -1);

        }

        // throws java.lang.ArrayIndexOutOfBoundsException: 4000
        generator.writeString(new StringReader("abc"), -1);

        generator.writeEndArray();
        generator.flush();
        System.out.println(byteArrayOutputStream.toString());

it's a little bit tricky to get the error

@cowtowncoder
Copy link
Member

Thank you @jeffreye! Yes, buffer edge conditions like this likely is are tricky to reproduce. I appreciate your help.

I'll add this to my TODO list: it may take a while to get there but definitely want to fix the issue.

@cowtowncoder
Copy link
Member

Oh my. That method is.... all kinds of wrong. Bounds checks are incorrect in multiple places.
Thank you for reporting this, will try to see how to best address.

@cowtowncoder cowtowncoder changed the title ArrayOutOfBoundException at WriterBasedJsonGenerator.writeString ArrayOutOfBoundException at WriterBasedJsonGenerator.writeString(Reader, int) Jun 25, 2021
@cowtowncoder cowtowncoder added this to the 2.12.4 milestone Jun 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants