Skip to content

Buffer.from() ignores byteOffset and length parameters #22387

Description

@njor
  • Version: v10.9.0
  • Platform: Linux jordan-MS-7817 4.15.0-32-generic net: give better error messages #35-Ubuntu SMP Fri Aug 10 17:58:07 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux
  • Subsystem:

According to the documentation, the Buffer.from() function should accept byteOffset and length parameters, but those are actually ignored:

Buffer.from( "hello world" )
<Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>
Buffer.from( "hello world", 2, 3 )
<Buffer 68 65 6c 6c 6f 20 77 6f 72 6c 64>

Expected output:

Buffer.from( "hello world", 2, 3 )
<Buffer 6c 6c 6f>

Activity

  1. njor commented on Aug 18, 2018

    @njor
    Author

    Sorry, I got my copy / paste wrong. I really meant Buffer.from (I just made another test with the Buffer constructor to check, and copied it by mistake).
    Also I get the same behavior if I use another type of Buffer than a string. For example:

    > buffer = fs.readFileSync( "image.jpg" );
    <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e1 18 4c 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 09 00 00 01 09 00 01 00 00 00 94 04 ... >
    > Buffer.from( buffer, 5, 10 )
    <Buffer ff d8 ff e0 00 10 4a 46 49 46 00 01 01 00 00 01 00 01 00 00 ff e1 18 4c 45 78 69 66 00 00 49 49 2a 00 08 00 00 00 09 00 00 01 09 00 01 00 00 00 94 04 ... >
    
  2. mscdex commented on Aug 18, 2018

    @mscdex
    Contributor

    What you're showing agrees with the documentation. Buffer arguments do not support offset and length, however ArrayBuffer objects do. If you add .buffer to your Buffer instance, you will get an ArrayBuffer.

  3. njor commented on Aug 18, 2018

    @njor
    Author

    Oh, OK I understand. Sorry for the false bug report.

  4. added
    invalidIssues and PRs that are invalid.
    bufferIssues and PRs related to the buffer subsystem.
    on Aug 18, 2018
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

    bufferIssues and PRs related to the buffer subsystem.invalidIssues and PRs that are invalid.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions