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 — ByteBuf slice duplicates are not slices · Issue #12919 · netty/netty

ByteBuf slice duplicates are not slices #12919

New issue
Copy link
New issue
Copy link
Closed
#14093
Closed
ByteBuf slice duplicates are not slices#12919
#14093
Copy link
Milestone
4.1.111.Final

Description

@vietj
vietj
opened on Oct 21, 2022
Issue body actions

Expected behavior

A ByteBuf duplicated slice remains a slice.

Actual behavior

A ByteBuf duplicated slice is a duplicate and not a slice anymore.

Steps to reproduce

Duplicating a sliced ByteBuf is enough.

Minimal yet complete reproducer code (or URL to code)

ByteBuf helloWorld = Unpooled.copiedBuffer("Hello World", StandardCharsets.UTF_8);
ByteBuf hello = helloWorld.slice(0, 5);
try {
  hello.writeByte('_');
  fail();
} catch (Exception ignore) {
}
try {
  hello.duplicate().writeByte('_');
  fail(); // Fails here
} catch (Exception ignore) {
}

Netty version

Most I'd say

JVM version (e.g. java -version)

N/A

OS version (e.g. uname -a)

N/A

Activity

  1. franz1981 commented on Oct 21, 2022

    @franz1981
    franz1981
    on Oct 21, 2022
    Contributor
    More actions

    I would expect as well that It won't be expanded too

  2. chrisvest commented on Oct 21, 2022

    @chrisvest
    chrisvest
    on Oct 21, 2022
    Member
    More actions

    It might be illogical or unexpected, but it is specified to behave this way. The javadocs for duplicate() say that it returns…

    A buffer whose readable content is equivalent to the buffer returned by {@link #slice()}.
    However this buffer will share the capacity of the underlying buffer,
    and therefore allows access to all of the underlying content if necessary.
    

    So duplicate() always gives you duplicates, and slice() always gives you slices.

    Reacted by gamecraft and Sergii Tkachenko
  3. vietj commented on Oct 24, 2022

    @vietj
    vietj
    on Oct 24, 2022
    ContributorAuthor
    More actions

    @chrisvest I hear that, however it is not possible from the user code perspective to determine whether the ByteBuf is a duplicate or a slice, if it needs to gets a duplicate of a slice with the same semantic, which makes the API not convenient for such use case (and I know that slices are going away in Netty 5 of course)

  4. vietj
    closed this as not plannedon Oct 24, 2022
  5. lhotari commented on Jun 3, 2024

    @lhotari
    lhotari
    on Jun 3, 2024
    Contributor
    More actions

    I made #14093 with the title "Duplicate of slice should have the same capacity as the original slice so that it's not writable" to address this issue.

  6. normanmaurer commented on Jun 3, 2024

    @normanmaurer
    normanmaurer
    on Jun 3, 2024
    Member
    More actions

    @chrisvest I actually agree with @lhotari here... If you duplicate a slice it should share the same capacity as the buffer you called duplicate on, which in this case is a slice. It should not share the capacity of the unwrapped slice. So with this I would say #14093 is correct. Also this is inline with the javadoc you posted as the "underlying buffer" would be the sliced buffer and not the original buffer.

  7. normanmaurer
    reopened this on Jun 3, 2024
  8. normanmaurer
    added this to the 4.1.111.Final milestone on Jun 3, 2024
  9. chrisvest
    closed this as completedin #14093on Jun 3, 2024
  10. chrisvest
    added 2 commits that reference this issue on Jun 3, 2024

    Duplicate of slice should have the same capacity as the original slic…

    e9caa97

    Duplicate of slice should have the same capacity as the original slic…

    21e5411
  11. sergiitk commented on Jun 14, 2024

    @sergiitk
    sergiitk
    on Jun 14, 2024
    Contributor
    More actions

    For posterity: looks like this behavioral change caused grpc/grpc-java#11284, as we relied on "duplicate() always gives you duplicates, and slice() always gives you slices." behavior (#12919 (comment)) to get around the issue with component() of a double-wrapped CompositeByteBuf returning a slice with incorrect indexes, #12844.

    Reacted by Francesco Nigro and Julien Viet
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      • 4.1.111.Final

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      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.