Skip to content

ReadOnlyByteBuf (and sub-classes) does not create derived buffers tha… - #14076

Merged
normanmaurer merged 2 commits into
4.1from
readonly_share_refcnt
May 24, 2024
Merged

normanmaurer merged 2 commits into
4.1from
readonly_share_refcnt

Conversation

@normanmaurer

Copy link
Copy Markdown
Member

…t share reference count

Motivation:

The derived buffers that are created via ReadOnlyByteBuf (and sub-classes) do not share the reference count with the parent. This results in buffer leaks

Modifications:

  • Correctly share reference count
  • Add unit tests

Result:

Fix #14075

…t share reference count

Motivation:

The derived buffers that are created via ReadOnlyByteBuf (and sub-classes) do not share the reference count with the parent. This results in buffer leaks

Modifications:

- Correctly share reference count
- Add unit tests

Result:

Fix #14075

@lhotari lhotari left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks great! Thanks for addressing this so quickly.

public boolean isWritable(int numBytes) {
return false;
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In addition to the above methods, nioBuffer and nioBuffers methods should return read-only ByteBuffer instances. While looking into this I noticed that the override for nioBuffer exists in ReadOnlyByteBufferBuf, but the nioBuffers override is missing.

Something like this for making nioBuffer and nioBuffers return read-only ByteBuffers?

    @Override
    public ByteBuffer nioBuffer(int index, int length) {
        return unwrap().nioBuffer(index, length).asReadOnlyBuffer();
    }

    @Override
    public ByteBuffer[] nioBuffers(int index, int length) {
        ByteBuffer[] byteBuffers = unwrap().nioBuffers(index, length);
        for (int i = 0; i < byteBuffers.length; i++) {
            byteBuffers[i] = byteBuffers[i].asReadOnlyBuffer();
        }
        return byteBuffers;
    }

@lhotari lhotari May 24, 2024 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The reason why read-only ByteBuffers are needed in the SslHandler use case is to avoid mutating the buffer. JDK's SSLEngine will mutate the input buffer with an in-place encryption reusing the input buffer unless the buffer is read-only. The support for read-only ByteBuffers in JDK's SSLEngine was added as a result of a bug reported by @chrisvest, more details in comment #14069 (comment) .

lhotari

This comment was marked as resolved.

@normanmaurer
normanmaurer merged commit 5666d55 into 4.1 May 24, 2024
@normanmaurer
normanmaurer deleted the readonly_share_refcnt branch May 24, 2024 16:26
normanmaurer added a commit that referenced this pull request May 24, 2024
#14076)

…t share reference count

Motivation:

The derived buffers that are created via ReadOnlyByteBuf (and
sub-classes) do not share the reference count with the parent. This
results in buffer leaks

Modifications:

- Correctly share reference count
- Add unit tests

Result:

Fix #14075
@normanmaurer normanmaurer added this to the 4.1.111.Final milestone May 24, 2024
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

Successfully merging this pull request may close these issues.

ByteBuf.asReadOnly() derived buffer doesn't support parent.release on deallocation which makes it unsuitable for ctx.write

3 participants