Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
njor opened this issue Aug 18, 2018 · 5 comments
Closed

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

njor opened this issue Aug 18, 2018 · 5 comments
Labels
buffer Issues and PRs related to the buffer subsystem. invalid Issues and PRs that are invalid.

Comments

@njor
Copy link

njor commented Aug 18, 2018

  • 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>
@ghost
Copy link

ghost commented Aug 18, 2018

"hello world" is not an ArrayBuffer
For strings see https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_string_encoding
There is no byteOffset parameter.

Also, the Buffer constructor is deprecated, you should use Buffer.from

@njor
Copy link
Author

njor commented Aug 18, 2018

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

@ghost
Copy link

ghost commented Aug 18, 2018

Also I get the same behavior if I use another type of Buffer than a string.

String is not a Buffer.
Buffer is not ArrayBuffer.
fs.readFileSync("image.jpg") is not ArrayBuffer.

The doc is clear about it. Second and third parameters represent byte offset and length respectively iff the first parameter is an instance of ArrayBuffer.

> Buffer.from(new ArrayBuffer(10), 2, 3)
<Buffer 00 00 00>

@mscdex
Copy link
Contributor

mscdex commented Aug 18, 2018

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.

@njor
Copy link
Author

njor commented Aug 18, 2018

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

@njor njor closed this as completed Aug 18, 2018
@ChALkeR ChALkeR added invalid Issues and PRs that are invalid. buffer Issues and PRs related to the buffer subsystem. labels Aug 18, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
buffer Issues and PRs related to the buffer subsystem. invalid Issues and PRs that are invalid.
Projects
None yet
Development

No branches or pull requests

3 participants