Skip to content

bad Buffer conversion behaviour due to Uint8Array inheritance #28725

Description

@Fishrock123

tl;dr - TypedArrays of lower-order truncate high-order values, which is is confusing (and hard to work around).

Ok so Buffer.from(new Uint32Array([0x4701c993])) returns a buffer with a length of 1 and a value in index 0 of 0x93.

This is a bad conversion and seems obviously wrong to me. An explicit 32-bit type was provided, and not just [0x4701c993] (for which the docs says interpreted as octets).

I don't possibly see how this could be the desired behavior, the resulting values are nothing like the input at all. But, here's the spec: https://www.ecma-international.org/ecma-262/6.0/#sec-touint8

Also, the Buffer#write<U><Type><Size><endian>() APIs help little, since they are awkwardly designed when used for such a purpose. (They return bytes written and not the buffer.) Presumably, if I had a whole array of int32s I'd have to write some kind of custom converter, which seems wrong considering we are converting to a runtime standard object from a primitive type.

Suggestion: fix this somehow, or, provide a nicer API to do a reasonable conversion.

Activity

  1. added
    bufferIssues and PRs related to the buffer subsystem.
    on Jul 17, 2019
  2. devsnek commented on Jul 17, 2019

    @devsnek
    Member

    Buffer.from(new Uint32Array([0x4701c993]).buffer) 😄

  3. gfx commented on Jul 19, 2019

    @gfx
    Contributor

    How about introducing a new API like Buffer.fromBufferView(bufferView: ArrayBufferView) to a shorthand of Buffer.from(bufferView.buffer, bufferView.byteOffset, bufferView.byteLength)?

    I saw a lot of Buffer.from(uint8array) for Uint8Array-to-Buffer conversion, but because it copies the underlying buffer it is not the best solution for the most cases.

    (ArrayBufferView here is an object that ArrayBuffer.isView() returns true).

  4. Fishrock123 commented on Jul 22, 2019

    @Fishrock123
    ContributorAuthor

    Also, maybe we could emit a warning for this? That's platform independent and if someone does Buffer.from(new Uint32Array(...)) they almost certainly have a bug.

  5. added a commit that references this issue on May 31, 2023
  6. added a commit that references this issue on Jun 4, 2023
  7. added a commit that references this issue on Jul 6, 2023
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.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions