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 the region copied by Unpooled.copiedBuffer(CharSequence, int, int, Charset) by Marti2203 · Pull Request #17971 · netty/netty

Conversation

@Marti2203

@Marti2203 Marti2203 commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Motivation:

When the CharSequence passed to Unpooled.copiedBuffer(CharSequence, int offset, int length, Charset) is a CharBuffer without an accessible array, for example one returned by CharBuffer.wrap(CharSequence) or asReadOnlyBuffer(), the method sets the limit of the slice to length instead of offset + length. The result is truncated for any offset > 0, or IllegalArgumentException is thrown if offset > length. The array-backed CharBuffer and String paths copy the requested region.

Modification:

  • Set the limit of the slice to offset + length.
  • Add UnpooledTest.testCopiedBufferCharBufferRegion, which covers array-backed and non array-backed CharBuffers, with position 0 and a non-zero position.

Result:

copiedBuffer(CharSequence, int, int, Charset) copies the requested region for every CharBuffer.

Reproduction on 4.2 (5776ebc):

CharBuffer cb = CharBuffer.wrap("hello world");                  // hasArray() == false
Unpooled.copiedBuffer(cb, 2, 5, CharsetUtil.US_ASCII);            // "llo"  (a String gives "llo w")
Unpooled.copiedBuffer(cb, 6, 5, CharsetUtil.US_ASCII);            // IllegalArgumentException: newPosition > limit: (6 > 5)

With this change they return "llo w" and "world". The new test fails without the change (expected: <llo w> but was: <llo>). UnpooledTest and UnpooledByteBufAllocatorTest pass.

This issue was found by an LLM-Enhanced Static Analysis Engine and verified manually.

🤖 Generated with Claude Code

Sorry, something went wrong.

Uh oh!

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

All reactions
@Marti2203
Fix the region copied by Unpooled.copiedBuffer(CharSequence, int, int…
1811223
…, Charset)

Motivation:

When the CharSequence is a CharBuffer without an accessible array, for
example one returned by CharBuffer.wrap(CharSequence) or
asReadOnlyBuffer(), copiedBuffer(string, offset, length, charset) sets
the limit of the slice to length instead of offset + length. The result
is truncated for any offset > 0: CharBuffer.wrap("hello world") with
offset 2 and length 5 gives "llo" instead of "llo w", and offset 6
throws IllegalArgumentException. The array-backed CharBuffer and the
String paths copy the requested region.

Modifications:

- Set the limit of the slice to offset + length.
- Add a test with array-backed and non array-backed CharBuffers.

Result:

copiedBuffer(CharSequence, int, int, Charset) copies the requested
region for every CharBuffer.
normanmaurer
normanmaurer approved these changes Oct 21, 2026
View reviewed changes
@normanmaurer normanmaurer added this to the 4.2.20.Final milestone Oct 21, 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 21, 2026
@testlens-app

testlens-app Bot commented Oct 21, 2026

Copy link
Copy Markdown

✅ All tests passed ✅

🏷️ Commit: 1811223
▶️ Tests: 167719 executed
⚪️ Checks: 30/30 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 merged commit 19e950b into netty:4.2 Oct 21, 2026
31 checks passed

Uh oh!

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

@netty-project-bot

netty-project-bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 4.1: #17995

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 21, 2026
Auto-port 4.1: Fix the region copied by Unpooled.copiedBuffer(CharSequence, int, int, Charset) #17995
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 21, 2026
@netty-project-bot

netty-project-bot commented Oct 21, 2026

Copy link
Copy Markdown
Contributor

Auto-port PR for 5.0: #17996

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 21, 2026
Auto-port 5.0: Fix the region copied by Unpooled.copiedBuffer(CharSequence, int, int, Charset) #17996
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 21, 2026
normanmaurer pushed a commit that referenced this pull request Oct 22, 2026
@netty-project-bot @Marti2203
Auto-port 4.1: Fix the region copied by Unpooled.copiedBuffer(CharSeq…
d252605
…uence, int, int, Charset) (#17995)

Auto-port of #17971 to 4.1
Cherry-picked commit: 19e950b

---
Motivation:

When the `CharSequence` passed to `Unpooled.copiedBuffer(CharSequence,
int offset, int length, Charset)` is a `CharBuffer` without an
accessible array, for example one returned by
`CharBuffer.wrap(CharSequence)` or `asReadOnlyBuffer()`, the method sets
the limit of the slice to `length` instead of `offset + length`. The
result is truncated for any `offset > 0`, or `IllegalArgumentException`
is thrown if `offset > length`. The array-backed `CharBuffer` and
`String` paths copy the requested region.

Modification:

- Set the limit of the slice to `offset + length`.
- Add `UnpooledTest.testCopiedBufferCharBufferRegion`, which covers
array-backed and non array-backed `CharBuffer`s, with position 0 and a
non-zero position.

Result:

`copiedBuffer(CharSequence, int, int, Charset)` copies the requested
region for every `CharBuffer`.

Co-authored-by: Martin Mirchev <[email protected]>
normanmaurer pushed a commit that referenced this pull request Oct 22, 2026
@netty-project-bot @Marti2203
Auto-port 5.0: Fix the region copied by Unpooled.copiedBuffer(CharSeq…
0eea8f8
…uence, int, int, Charset) (#17996)

Auto-port of #17971 to 5.0
Cherry-picked commit: 19e950b

---
Motivation:

When the `CharSequence` passed to `Unpooled.copiedBuffer(CharSequence,
int offset, int length, Charset)` is a `CharBuffer` without an
accessible array, for example one returned by
`CharBuffer.wrap(CharSequence)` or `asReadOnlyBuffer()`, the method sets
the limit of the slice to `length` instead of `offset + length`. The
result is truncated for any `offset > 0`, or `IllegalArgumentException`
is thrown if `offset > length`. The array-backed `CharBuffer` and
`String` paths copy the requested region.

Modification:

- Set the limit of the slice to `offset + length`.
- Add `UnpooledTest.testCopiedBufferCharBufferRegion`, which covers
array-backed and non array-backed `CharBuffer`s, with position 0 and a
non-zero position.

Result:

`copiedBuffer(CharSequence, int, int, Charset)` copies the requested
region for every `CharBuffer`.

Co-authored-by: Martin Mirchev <[email protected]>
lltopk pushed a commit to lltopk/netty that referenced this pull request Oct 24, 2026
@netty-project-bot @Marti2203 @lltopk
Auto-port 4.1: Fix the region copied by Unpooled.copiedBuffer(CharSeq…
815f37d
…uence, int, int, Charset) (netty#17995)

Auto-port of netty#17971 to 4.1
Cherry-picked commit: 19e950b

---
Motivation:

When the `CharSequence` passed to `Unpooled.copiedBuffer(CharSequence,
int offset, int length, Charset)` is a `CharBuffer` without an
accessible array, for example one returned by
`CharBuffer.wrap(CharSequence)` or `asReadOnlyBuffer()`, the method sets
the limit of the slice to `length` instead of `offset + length`. The
result is truncated for any `offset > 0`, or `IllegalArgumentException`
is thrown if `offset > length`. The array-backed `CharBuffer` and
`String` paths copy the requested region.

Modification:

- Set the limit of the slice to `offset + length`.
- Add `UnpooledTest.testCopiedBufferCharBufferRegion`, which covers
array-backed and non array-backed `CharBuffer`s, with position 0 and a
non-zero position.

Result:

`copiedBuffer(CharSequence, int, int, Charset)` copies the requested
region for every `CharBuffer`.

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

Reviewers

@normanmaurer normanmaurer normanmaurer approved these changes

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.20.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

@Marti2203 @netty-project-bot @normanmaurer
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.