Skip to content

BufferedData.length() is incorrect if wrapped offset isn't zero #782

@anthony-swirldslabs

Description

@anthony-swirldslabs
final ByteBuffer bb = ByteBuffer.wrap(new byte[]{0, 1, 2, 3, 4, 5, 6, 7}, 1, 3);
        System.err.println("bb.lim = " + bb.limit());
        final ByteArrayBufferedData babd = new ByteArrayBufferedData(bb);
        System.err.println("babd.len = " + babd.length());

prints

bb.lim = 4
babd.len = 4

As far as the ByteBuffer is concerned, the behavior is correct per the ByteBuffer javadoc.

However, while the current ByteArrayBufferedData(ByteBuffer) ctor or a similar wrap(, offset, length) method also behave per their current javadoc, the result returned by the length() method is incorrect. We wrapped a ByteBuffer of length 3 (see the last argument at the first line of code), so the length of the BufferedData object must also be 3, which currently it isn't because:

/** {@inheritDoc} */
    @Override
    public long length() {
        return buffer.limit();
    }

and this code can only return the correct result if the offset of the buffer is zero, and also, only if the client code never modified the limit via the BufferedData APIs.

We even have unit tests that verify these odd results.

We need to revisit offsets/limits/lengths handling, fix bugs, update the unit tests, and then verify if the CN code still works and doesn't rely on the current incorrect behavior.

Metadata

Metadata

Labels

No labels
No labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions