GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Fix retainedSlice() of non-retained pooled derived buffers by lhotari · Pull Request #17759 · netty/netty

Conversation

@lhotari

@lhotari lhotari commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

Motivation:

ByteBuf.retainedSlice() is specified as identical to slice(readerIndex(), readableBytes()), except that it retains the result. duplicate() and slice() of a pooled derived buffer, such as a pooled buffer's retainedDuplicate() or retainedSlice(), return non-retained derived buffers. Their retainedSlice() uses the capacity as the length:

Class retainedSlice() returned Effect
PooledNonRetainedDuplicateByteBuf retainedSlice(readerIndex(), capacity()) With a reader index of 0, it includes the bytes after the writer index; otherwise IndexOutOfBoundsException
PooledNonRetainedSlicedByteBuf retainedSlice(0, capacity()) It ignores the reader and writer indexes
ByteBuf root = PooledByteBufAllocator.DEFAULT.directBuffer(16).writeBytes("pooled-data".getBytes(CharsetUtil.US_ASCII));
ByteBuf retainedDuplicate = root.retainedDuplicate();
ByteBuf duplicate = retainedDuplicate.duplicate();  // a PooledNonRetainedDuplicateByteBuf
ByteBuf slice = duplicate.retainedSlice();          // 16 readable bytes instead of 11
slice.release();
duplicate.readByte();
duplicate.retainedSlice();                          // IndexOutOfBoundsException
retainedDuplicate.release();
root.release();

The overrides came with #6024 (4bba752, a fix for #5999). Before it, slice() and duplicate() of a pooled derived buffer delegated to the root buffer's slice(), whose retainedSlice() respected the indexes. The length == capacity() comment was copied from PooledSlicedByteBuf.duplicate(), where it's correct, but it doesn't hold for retainedSlice().

This was found in Apache Pulsar, where a retainedSlice() of such a duplicate returned bytes beyond the writer index.

Modification:

  • Both overrides now return retainedSlice(readerIndex(), readableBytes()).
  • AbstractByteBufTest.testRetainedSliceOfNonRetainedDerivedBufferCoversTheReadableBytes checks retainedSlice() of duplicates and slices (also nested) of a retained duplicate and a retained slice. It runs with reader index 0 and 1, a writer index below the capacity, and before and after a read. Without the fix, it fails in the 5 pooled buffer test classes.

Result:

retainedSlice() of a non-retained pooled derived buffer covers exactly its readable bytes.

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
lhotari added 2 commits October 4, 2026 19:33
@lhotari
Fix retainedSlice() of non-retained duplicates and slices of pooled d…
3912db8
…erived buffers

Motivation:

ByteBuf.retainedSlice() is retainedSlice(readerIndex(), readableBytes()).
The non-retained duplicate and slice that duplicate() and slice() return
for a pooled derived buffer, such as a pooled buffer's retainedDuplicate()
or retainedSlice(), didn't follow that:

- PooledNonRetainedDuplicateByteBuf.retainedSlice() returned
  retainedSlice(readerIndex(), capacity()): it included the bytes after the
  writer index, and threw IndexOutOfBoundsException when the reader index
  wasn't 0.
- PooledNonRetainedSlicedByteBuf.retainedSlice() returned
  retainedSlice(0, capacity()), ignoring the reader and writer indexes.

A caller then sees bytes that weren't written or were already read, or
gets an exception.

Modification:

Return retainedSlice(readerIndex(), readableBytes()) in both, as
AbstractByteBuf and AbstractPooledDerivedByteBuf do. Add a test to
AbstractByteBufTest that checks retainedSlice() of duplicates and slices of
retained duplicates and retained slices, with a writer index below the
capacity and before and after reading.

Result:

retainedSlice() of a non-retained derived buffer covers its readable bytes.
@lhotari
Fill the buffer, cover reader index 0 and nested derived buffers in t…
388b4e0
…he retainedSlice() test
@testlens-app

testlens-app Bot commented Oct 4, 2026 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 388b4e0
▶️ Tests: 147274 executed
⚪️ Checks: 33/33 completed


Learn more about TestLens at testlens.app/docs.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer

normanmaurer commented Oct 5, 2026

Copy link
Copy Markdown
Member

@lhotari good catch!

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@normanmaurer normanmaurer added this to the 4.2.19.Final milestone Oct 5, 2026
@normanmaurer normanmaurer added needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. labels Oct 5, 2026
@normanmaurer
normanmaurer merged commit d84dc6b into netty:4.2 Oct 5, 2026
31 checks passed

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot

netty-project-bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #17760

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot netty-project-bot mentioned this pull request Oct 5, 2026
Auto-port 5.0: Fix retainedSlice() of non-retained pooled derived buffers #17760
Merged
@github-actions github-actions Bot removed the needs-cherry-pick-5.0 This PR should be cherry-picked to 5.0 once merged. label Oct 5, 2026
@netty-project-bot

netty-project-bot commented Oct 5, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #17761

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@netty-project-bot netty-project-bot mentioned this pull request Oct 5, 2026
Auto-port 4.1: Fix retainedSlice() of non-retained pooled derived buffers #17761
Merged
@github-actions github-actions Bot removed the needs-cherry-pick-4.1 This PR should be cherry-picked to 4.1 once merged. label Oct 5, 2026
normanmaurer pushed a commit that referenced this pull request Oct 5, 2026
@netty-project-bot @lhotari
Auto-port 5.0: Fix retainedSlice() of non-retained pooled derived buf…
134adc1
…fers (#17760)

Auto-port of #17759 to 5.0
Cherry-picked commit: d84dc6b

---
Motivation:

`ByteBuf.retainedSlice()` is specified as identical to
`slice(readerIndex(), readableBytes())`, except that it retains the
result. `duplicate()` and `slice()` of a pooled derived buffer, such as
a pooled buffer's `retainedDuplicate()` or `retainedSlice()`, return
non-retained derived buffers. Their `retainedSlice()` uses the capacity
as the length:

| Class | `retainedSlice()` returned | Effect |
|---|---|---|
| `PooledNonRetainedDuplicateByteBuf` | `retainedSlice(readerIndex(),
capacity())` | With a reader index of 0, it includes the bytes after the
writer index; otherwise `IndexOutOfBoundsException` |
| `PooledNonRetainedSlicedByteBuf` | `retainedSlice(0, capacity())` | It
ignores the reader and writer indexes |

```java
ByteBuf root = PooledByteBufAllocator.DEFAULT.directBuffer(16).writeBytes("pooled-data".getBytes(CharsetUtil.US_ASCII));
ByteBuf retainedDuplicate = root.retainedDuplicate();
ByteBuf duplicate = retainedDuplicate.duplicate();  // a PooledNonRetainedDuplicateByteBuf
ByteBuf slice = duplicate.retainedSlice();          // 16 readable bytes instead of 11
slice.release();
duplicate.readByte();
duplicate.retainedSlice();                          // IndexOutOfBoundsException
retainedDuplicate.release();
root.release();
```

The overrides came with #6024 (4bba752, a fix for
#5999). Before it, `slice()` and `duplicate()` of a pooled
derived buffer delegated to the root buffer's `slice()`, whose
`retainedSlice()` respected the indexes. The `length == capacity()`
comment was copied from `PooledSlicedByteBuf.duplicate()`, where it's
correct, but it doesn't hold for `retainedSlice()`.

This was found in Apache Pulsar, where a `retainedSlice()` of such a
duplicate returned bytes beyond the writer index.

Modification:

- Both overrides now return `retainedSlice(readerIndex(),
readableBytes())`.
-
`AbstractByteBufTest.testRetainedSliceOfNonRetainedDerivedBufferCoversTheReadableBytes`
checks `retainedSlice()` of duplicates and slices (also nested) of a
retained duplicate and a retained slice. It runs with reader index 0 and
1, a writer index below the capacity, and before and after a read.
Without the fix, it fails in the 5 pooled buffer test classes.

Result:

`retainedSlice()` of a non-retained pooled derived buffer covers exactly
its readable bytes.

Co-authored-by: Lari Hotari <[email protected]>
normanmaurer pushed a commit that referenced this pull request Oct 5, 2026
@netty-project-bot @lhotari
Auto-port 4.1: Fix retainedSlice() of non-retained pooled derived buf…
cf6c467
…fers (#17761)

Auto-port of #17759 to 4.1
Cherry-picked commit: d84dc6b

---
Motivation:

`ByteBuf.retainedSlice()` is specified as identical to
`slice(readerIndex(), readableBytes())`, except that it retains the
result. `duplicate()` and `slice()` of a pooled derived buffer, such as
a pooled buffer's `retainedDuplicate()` or `retainedSlice()`, return
non-retained derived buffers. Their `retainedSlice()` uses the capacity
as the length:

| Class | `retainedSlice()` returned | Effect |
|---|---|---|
| `PooledNonRetainedDuplicateByteBuf` | `retainedSlice(readerIndex(),
capacity())` | With a reader index of 0, it includes the bytes after the
writer index; otherwise `IndexOutOfBoundsException` |
| `PooledNonRetainedSlicedByteBuf` | `retainedSlice(0, capacity())` | It
ignores the reader and writer indexes |

```java
ByteBuf root = PooledByteBufAllocator.DEFAULT.directBuffer(16).writeBytes("pooled-data".getBytes(CharsetUtil.US_ASCII));
ByteBuf retainedDuplicate = root.retainedDuplicate();
ByteBuf duplicate = retainedDuplicate.duplicate();  // a PooledNonRetainedDuplicateByteBuf
ByteBuf slice = duplicate.retainedSlice();          // 16 readable bytes instead of 11
slice.release();
duplicate.readByte();
duplicate.retainedSlice();                          // IndexOutOfBoundsException
retainedDuplicate.release();
root.release();
```

The overrides came with #6024 (4bba752, a fix for
#5999). Before it, `slice()` and `duplicate()` of a pooled
derived buffer delegated to the root buffer's `slice()`, whose
`retainedSlice()` respected the indexes. The `length == capacity()`
comment was copied from `PooledSlicedByteBuf.duplicate()`, where it's
correct, but it doesn't hold for `retainedSlice()`.

This was found in Apache Pulsar, where a `retainedSlice()` of such a
duplicate returned bytes beyond the writer index.

Modification:

- Both overrides now return `retainedSlice(readerIndex(),
readableBytes())`.
-
`AbstractByteBufTest.testRetainedSliceOfNonRetainedDerivedBufferCoversTheReadableBytes`
checks `retainedSlice()` of duplicates and slices (also nested) of a
retained duplicate and a retained slice. It runs with reader index 0 and
1, a writer index below the capacity, and before and after a read.
Without the fix, it fails in the 5 pooled buffer test classes.

Result:

`retainedSlice()` of a non-retained pooled derived buffer covers exactly
its readable bytes.

Co-authored-by: Lari Hotari <[email protected]>
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
@netty-project-bot @lhotari @lltopk
Auto-port 4.1: Fix retainedSlice() of non-retained pooled derived buf…
9889e33
…fers (netty#17761)

Auto-port of netty#17759 to 4.1
Cherry-picked commit: d84dc6b

---
Motivation:

`ByteBuf.retainedSlice()` is specified as identical to
`slice(readerIndex(), readableBytes())`, except that it retains the
result. `duplicate()` and `slice()` of a pooled derived buffer, such as
a pooled buffer's `retainedDuplicate()` or `retainedSlice()`, return
non-retained derived buffers. Their `retainedSlice()` uses the capacity
as the length:

| Class | `retainedSlice()` returned | Effect |
|---|---|---|
| `PooledNonRetainedDuplicateByteBuf` | `retainedSlice(readerIndex(),
capacity())` | With a reader index of 0, it includes the bytes after the
writer index; otherwise `IndexOutOfBoundsException` |
| `PooledNonRetainedSlicedByteBuf` | `retainedSlice(0, capacity())` | It
ignores the reader and writer indexes |

```java
ByteBuf root = PooledByteBufAllocator.DEFAULT.directBuffer(16).writeBytes("pooled-data".getBytes(CharsetUtil.US_ASCII));
ByteBuf retainedDuplicate = root.retainedDuplicate();
ByteBuf duplicate = retainedDuplicate.duplicate();  // a PooledNonRetainedDuplicateByteBuf
ByteBuf slice = duplicate.retainedSlice();          // 16 readable bytes instead of 11
slice.release();
duplicate.readByte();
duplicate.retainedSlice();                          // IndexOutOfBoundsException
retainedDuplicate.release();
root.release();
```

The overrides came with netty#6024 (4bba752, a fix for
netty#5999). Before it, `slice()` and `duplicate()` of a pooled
derived buffer delegated to the root buffer's `slice()`, whose
`retainedSlice()` respected the indexes. The `length == capacity()`
comment was copied from `PooledSlicedByteBuf.duplicate()`, where it's
correct, but it doesn't hold for `retainedSlice()`.

This was found in Apache Pulsar, where a `retainedSlice()` of such a
duplicate returned bytes beyond the writer index.

Modification:

- Both overrides now return `retainedSlice(readerIndex(),
readableBytes())`.
-
`AbstractByteBufTest.testRetainedSliceOfNonRetainedDerivedBufferCoversTheReadableBytes`
checks `retainedSlice()` of duplicates and slices (also nested) of a
retained duplicate and a retained slice. It runs with reader index 0 and
1, a writer index below the capacity, and before and after a read.
Without the fix, it fails in the 5 pooled buffer test classes.

Result:

`retainedSlice()` of a non-retained pooled derived buffer covers exactly
its readable bytes.

Co-authored-by: Lari Hotari <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

4.2.19.Final

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

3 participants

@lhotari @normanmaurer @netty-project-bot
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.