jackson-core icon indicating copy to clipboard operation
jackson-core copied to clipboard

Copy String chars to char array by TextBuffer fails with ArrayIndexOutOfBoundsException

Open farfalena-zz opened this issue 14 years ago • 6 comments

Related jackson-core version is 2.0.4.

In com.fasterxml.jackson.core.util.TextBuffer line 498 expand( len ) is called which results in setting the _currentSegment to MAX_SEGMENT_LEN, even when the segment size is supposed to grow.

Thanks in advance for your support with this!

Related test is given below:


package europass.ewa.services.conversion.xml;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.io.IOException;
import java.util.Random;

import org.junit.Test;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

public class BytesToXmlTest{
    
    
    @Test
    public void write256KBData() throws JsonGenerationException, JsonMappingException, IOException {
        testWriteData(256);
    }
    
    @Test( expected=JsonMappingException.class )
    public void write384KBData() throws JsonGenerationException, JsonMappingException, IOException {
        testWriteData(384);
    }

    public void testWriteData(int kb) throws JsonGenerationException, JsonMappingException, IOException {
        Random r = new Random(1);
        Data data = new Data();
        data.bytes  = new byte[1024*kb];
        r.nextBytes(data.bytes);
        String xml = new XmlMapper().writeValueAsString(data);
        assertThat(xml, is(notNullValue()));
    }


    public static class Data {
        
        @JsonProperty
        byte[] bytes;
    }
}

farfalena-zz avatar Jul 18 '12 11:07 farfalena-zz

Ok: I will try to recreate this with basic JsonGenerator, to ensure it does not rely on use of XmlMapper.

cowtowncoder avatar Jul 18 '12 19:07 cowtowncoder

Hmmh. I can not reproduce the problem, although I can see what needs fixing. I will write a simpler test for TextBuffer to work around this.

cowtowncoder avatar Jul 18 '12 19:07 cowtowncoder

Easy enough to reproduce directly, fix. Will get fix done later today, checked in. Thanks again!

cowtowncoder avatar Jul 18 '12 20:07 cowtowncoder

Fixed.

cowtowncoder avatar Jul 18 '12 23:07 cowtowncoder

Many thanks for the fix! Could you please let me know, when are you planning to have the next tag release? Will this be 2.0.5?

farfalena-zz avatar Jul 19 '12 06:07 farfalena-zz

Yes, this would be 2.0.5. No concrete plans for 2.0.5, but within month most likely.

cowtowncoder avatar Jul 19 '12 18:07 cowtowncoder