GeezerVisonThe World Wide Web, restored to 800 pixels of glory. | |||||||||||||
| Home|About|Gaming cabinet|Archives|How it works|Starter sites | |||||||||||||
GeezerVison is showing github.com — Fix retainedSlice() of non-retained pooled derived buffers by lhotari · Pull Request #17759 · netty/netty Conversation
Contributor
Motivation:
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, This was found in Apache Pulsar, where a Modification:
Result:
Sorry, something went wrong. Uh oh!
There was an error while loading. Please reload this page. All reactions…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. …he retainedSlice() test
|
There was an error while loading. Please reload this page.
✅ All tests passed ✅🏷️ Commit: 388b4e0 Learn more about TestLens at testlens.app/docs. |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
@lhotari good catch! |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
There was an error while loading. Please reload this page.
|
Auto-port PR for 5.0: #17760 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
|
Auto-port PR for 4.1: #17761 |
Sorry, something went wrong.
There was an error while loading. Please reload this page.
…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]>
…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]>
…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]>
There was an error while loading. Please reload this page.
Successfully merging this pull request may close these issues.
There was an error while loading. Please reload this page.