Hao Zhong created CASSANDRA-13680:
-------------------------------------

             Summary: readBytes needs to clone its data
                 Key: CASSANDRA-13680
                 URL: https://issues.apache.org/jira/browse/CASSANDRA-13680
             Project: Cassandra
          Issue Type: Bug
            Reporter: Hao Zhong


The code of the ByteBufferUtil_readBytes method is as follow:
{code}
 public static ByteBuffer readBytes(ByteBuffer bb, int length)
    {
        ByteBuffer copy = bb.duplicate();
        copy.limit(copy.position() + length);
        bb.position(bb.position() + length);
        return copy;
    }
{code}


I found that CASSANDRA-3179 fixed a related bug. The buggy code is as follow:

{code}
 public synchronized ByteBuffer readBytes(int length) throws IOException
    {
        int remaining = buffer.remaining() - position;
        if (length > remaining)
            throw new IOException(String.format("mmap segment underflow; 
remaining is %d but %d requested",
                                                remaining, length));

        ByteBuffer bytes = buffer.duplicate();
        bytes.position(buffer.position() + position).limit(buffer.position() + 
position + length);
        position += length;

        return bytes;
    }
{code}

The fixed code is:
{code}
 public synchronized ByteBuffer readBytes(int length) throws IOException
    {
        int remaining = buffer.remaining() - position;
        if (length > remaining)
            throw new IOException(String.format("mmap segment underflow; 
remaining is %d but %d requested",
                                                remaining, length));

        if (length == 0)
            return ByteBufferUtil.EMPTY_BYTE_BUFFER;

        ByteBuffer bytes = buffer.duplicate();
        bytes.position(buffer.position() + position).limit(buffer.position() + 
position + length);
        position += length;

        // we have to copy the data in case we unreference the underlying 
sstable.  See CASSANDRA-3179
        ByteBuffer clone = ByteBuffer.allocate(bytes.remaining());
        clone.put(bytes);
        clone.flip();
        return clone;
    }
{code}
The ByteBufferUtil_readBytes method may be modified in the same way to handle 
the similar problem. 



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@cassandra.apache.org
For additional commands, e-mail: commits-h...@cassandra.apache.org

Reply via email to