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: make Buffer.from throw on DataView #48410

Closed
wants to merge 2 commits into from

Conversation

LiviaMedeiros
Copy link
Contributor

Passing DataView instance or arbitrary object with any ArrayBuffer in its buffer property and without numeric length property results in a new empty Buffer.

This behaviour isn't documented, and is confusing since buffer property is checked but not used.

@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. needs-ci PRs that need a full CI run. labels Jun 10, 2023
@LiviaMedeiros LiviaMedeiros added the request-ci Add this label to start a Jenkins CI on a PR. label Jun 10, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 10, 2023
@nodejs-github-bot

This comment was marked as outdated.

@anonrig
Copy link
Member

anonrig commented Jun 10, 2023

This is a breaking change right? I think we should add semver-major label.

@LiviaMedeiros LiviaMedeiros added the semver-major PRs that contain breaking changes and should be released in the next major version. label Jun 10, 2023
@LiviaMedeiros LiviaMedeiros added the request-ci Add this label to start a Jenkins CI on a PR. label Jun 11, 2023
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Jun 11, 2023
@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot

This comment was marked as outdated.

@nodejs-github-bot
Copy link
Collaborator

Copy link
Member

@tniessen tniessen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I can tell, the existing behavior of Buffer.from() is compatible with Uint8Array.from() in this regard, and since Buffer extends Uint8Array, it seems wrong to intentionally deviate from spec'd behavior.

Uint8Array.from(new DataView(new ArrayBuffer(100))).byteLength === 0
Uint8Array.from({ buffer: new ArrayBuffer(100) }).byteLength === 0

@LiviaMedeiros
Copy link
Contributor Author

As far as I can tell, the existing behavior of Buffer.from() is compatible with Uint8Array.from() in this regard, and since Buffer extends Uint8Array, it seems wrong to intentionally deviate from spec'd behavior.

The specs of both 23.2.2.1 %TypedArray%.from() and 23.2.5.1 TypedArray constructor don't mention buffer property nor DataView.

Uint8Array.from() returns empty buffer for arbitrary argument (as long as it's not null):

assert.deepStrictEqual(new Uint8Array(), Uint8Array.from({ buffer: new ArrayBuffer(8) }));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from(new DataView(new ArrayBuffer(8))));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from({ buffer: null }));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from({}));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from(123));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from(123n));
assert.deepStrictEqual(new Uint8Array(), Uint8Array.from(true));

Does that mean that Buffer.from() should do the same?

@tniessen
Copy link
Member

Does that mean that Buffer.from() should do the same?

I don't know. I am not generally in favor of adopting more web APIs where they don't make sense, but intentionally reducing compatibility with web APIs (as in this PR) does not seem helpful to me either.

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. needs-ci PRs that need a full CI run. semver-major PRs that contain breaking changes and should be released in the next major version.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants