Maja,
I had found this same bug a couple of weeks ago and was working on the patch. Attached is the unit tests I was working on. It was still a work in progress, but I hope you find it useful.

On 1/15/2014 3:16 PM, Maja Kabiljo (JIRA) wrote:
     [ 
https://issues.apache.org/jira/browse/GIRAPH-724?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13872541#comment-13872541
 ]

Maja Kabiljo commented on GIRAPH-724:
-------------------------------------

In writeBytes/writeChars/writeUTF, we can just call 
writeChar/writeByte/writeShort methods which already do it all (check the size, 
call unsafe, move position).

UnsafeByteArrayOutputStream.writeUTF should call ensureSize
-----------------------------------------------------------

                 Key: GIRAPH-724
                 URL: https://issues.apache.org/jira/browse/GIRAPH-724
             Project: Giraph
          Issue Type: Bug
            Reporter: Maja Kabiljo
         Attachments: GIRAPH-724.patch, GIRAPH-724.patch, GIRAPH-724.patch


UnsafeByteArrayOutputStream.writeUTF can throw an exception if the String 
doesn't fit in current byte array because ensureSize is not called.


--
This message was sent by Atlassian JIRA
(v6.1.5#6160)


package testThreads;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.IOException;

import org.apache.giraph.utils.UnsafeByteArrayInputStream;
import org.apache.giraph.utils.UnsafeByteArrayOutputStream;
import org.junit.Test;

public class testUnsafe {
        private static final String ASCII_STRING = "Ascii";
        private static final String CHINESE_STRING = "船帆座超新星殘骸";
        private static final String RUSSIAN_STRING = "на англий�кий 
�зык";

        @Test
        public void test1() throws IOException {
                final UnsafeByteArrayOutputStream os = new 
UnsafeByteArrayOutputStream();

                assertEquals("test ", 0, os.getPos());

                fillStream(os);

                os.close();

                final UnsafeByteArrayInputStream is = new 
UnsafeByteArrayInputStream(os.getByteArray());

                testStream(is);
        }

        @Test
        public void test2() throws IOException {
                for (int size = 0; size <= 50; size++) {
                        final UnsafeByteArrayOutputStream os = new 
UnsafeByteArrayOutputStream(new byte[size]);

                        assertEquals("test ", 0, os.getPos());

                        fillStream(os);

                        os.close();

                        final UnsafeByteArrayInputStream is = new 
UnsafeByteArrayInputStream(os.getByteArray());

                        testStream(is);
                }
        }

        private static void fillStream(final UnsafeByteArrayOutputStream os) 
throws IOException {
                os.write(Integer.MAX_VALUE);
                os.write(Integer.MIN_VALUE);
                os.write("P1".getBytes());
                os.write("abcdefg".getBytes(), 3, 3);
                os.writeBoolean(true);
                os.writeBoolean(false);
                os.writeByte((byte) 0x7f);
                os.writeBytes("Bytes");
                os.writeChar((char) 1010);
                // TODO os.writeChars("Chars1");
                os.writeDouble(Double.MAX_VALUE);
                os.writeDouble(Double.MIN_VALUE);
                os.writeFloat(Float.MAX_VALUE);
                os.writeFloat(Float.MIN_VALUE);
                os.writeInt(Integer.MAX_VALUE);
                os.writeInt(Integer.MIN_VALUE);
                // TODO
                os.writeLong(Long.MAX_VALUE);
                os.writeLong(Long.MIN_VALUE);
                os.writeShort(Short.MAX_VALUE);
                os.writeShort(Short.MIN_VALUE);
                os.writeUTF(ASCII_STRING);
                os.writeUTF(CHINESE_STRING);
                os.writeUTF(RUSSIAN_STRING);
        }

        private static void testStream(final UnsafeByteArrayInputStream is) 
throws IOException {
                assertEquals("test ", (byte) (Integer.MAX_VALUE & 0x000000FF), 
is.readByte());
                assertEquals("test ", (byte) (Integer.MIN_VALUE & 0x000000FF), 
is.readByte());
                assertEquals("Test ", (byte) 'P', is.readByte());
                assertEquals("Test ", (byte) '1', is.readByte());
                assertEquals("Test ", (byte) 'd', is.readByte());
                assertEquals("Test ", (byte) 'e', is.readByte());
                assertEquals("Test ", (byte) 'f', is.readByte());
                assertTrue("Test ", is.readBoolean());
                assertFalse("Test ", is.readBoolean());
                assertEquals("Test", 0x7f, is.readByte());
                assertEquals("Test ", (byte) 'B', is.readByte());
                assertEquals("Test ", (byte) 'y', is.readByte());
                assertEquals("Test ", (byte) 't', is.readByte());
                assertEquals("Test ", (byte) 'e', is.readByte());
                assertEquals("Test ", (byte) 's', is.readByte());
                assertEquals("test", (char) 1010, is.readChar());
                // TODO assertEquals("test", "Chars1", is.readUTF());
                assertEquals("test", Double.MAX_VALUE, is.readDouble(), 0);
                assertEquals("test", Double.MIN_VALUE, is.readDouble(), 0);
                assertEquals("test", Float.MAX_VALUE, is.readFloat(), 0);
                assertEquals("test", Float.MIN_VALUE, is.readFloat(), 0);
                assertEquals("test", Integer.MAX_VALUE, is.readInt());
                assertEquals("test", Integer.MIN_VALUE, is.readInt());
                assertEquals("test", Long.MAX_VALUE, is.readLong());
                assertEquals("test", Long.MIN_VALUE, is.readLong());
                assertEquals("test", Short.MAX_VALUE, is.readShort());
                assertEquals("test", Short.MIN_VALUE, is.readShort());
                assertEquals("test ", ASCII_STRING, is.readUTF());
                assertEquals("test ", CHINESE_STRING, is.readUTF());
                assertEquals("test ", RUSSIAN_STRING, is.readUTF());
        }
}

Reply via email to