fix(ext/node): align input validation of Buffer.compare (#29275)
Some checks are pending
ci / test debug windows-x86_64 (push) Blocked by required conditions
ci / test release macos-x86_64 (push) Blocked by required conditions
ci / test release windows-x86_64 (push) Blocked by required conditions
ci / test debug macos-x86_64 (push) Blocked by required conditions
ci / pre-build (push) Waiting to run
ci / test debug linux-aarch64 (push) Blocked by required conditions
ci / test release linux-aarch64 (push) Blocked by required conditions
ci / test debug macos-aarch64 (push) Blocked by required conditions
ci / test release macos-aarch64 (push) Blocked by required conditions
ci / bench release linux-x86_64 (push) Blocked by required conditions
ci / lint debug linux-x86_64 (push) Blocked by required conditions
ci / lint debug macos-x86_64 (push) Blocked by required conditions
ci / lint debug windows-x86_64 (push) Blocked by required conditions
ci / test debug linux-x86_64 (push) Blocked by required conditions
ci / test release linux-x86_64 (push) Blocked by required conditions
ci / build wasm32 (push) Blocked by required conditions
ci / publish canary (push) Blocked by required conditions

This commit is contained in:
Yoshiya Hinosawa 2025-05-13 17:14:19 +09:00 committed by GitHub
parent acd0c94b46
commit 1022decc79
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 62 additions and 19 deletions

View file

@ -382,25 +382,14 @@ const BufferIsBuffer = Buffer.isBuffer = function isBuffer(b) {
}; };
const BufferCompare = Buffer.compare = function compare(a, b) { const BufferCompare = Buffer.compare = function compare(a, b) {
if (isUint8Array(a)) { if (!isUint8Array(a)) {
a = BufferFrom( throw new codes.ERR_INVALID_ARG_TYPE("buf1", ["Buffer", "Uint8Array"], a);
a,
TypedArrayPrototypeGetByteOffset(a),
TypedArrayPrototypeGetByteLength(a),
);
} }
if (isUint8Array(b)) {
b = BufferFrom( if (!isUint8Array(b)) {
b, throw new ERR_INVALID_ARG_TYPE("buf2", ["Buffer", "Uint8Array"], b);
TypedArrayPrototypeGetByteOffset(b),
TypedArrayPrototypeGetByteLength(b),
);
}
if (!BufferIsBuffer(a) || !BufferIsBuffer(b)) {
throw new TypeError(
'The "buf1", "buf2" arguments must be one of type Buffer or Uint8Array',
);
} }
if (a === b) { if (a === b) {
return 0; return 0;
} }

View file

@ -271,6 +271,7 @@
"test-buffer-bigint64.js", "test-buffer-bigint64.js",
"test-buffer-bytelength.js", "test-buffer-bytelength.js",
"test-buffer-compare-offset.js", "test-buffer-compare-offset.js",
"test-buffer-compare.js",
"test-buffer-concat.js", "test-buffer-concat.js",
"test-buffer-constants.js", "test-buffer-constants.js",
"test-buffer-copy.js", "test-buffer-copy.js",

View file

@ -1,7 +1,7 @@
<!-- deno-fmt-ignore-file --> <!-- deno-fmt-ignore-file -->
# Remaining Node Tests # Remaining Node Tests
1168 tests out of 3993 have been ported from Node 23.9.0 (29.25% ported, 71.27% remaining). 1169 tests out of 3993 have been ported from Node 23.9.0 (29.28% ported, 71.25% remaining).
NOTE: This file should not be manually edited. Please edit `tests/node_compat/config.json` and run `deno task setup` in `tests/node_compat/runner` dir instead. NOTE: This file should not be manually edited. Please edit `tests/node_compat/config.json` and run `deno task setup` in `tests/node_compat/runner` dir instead.
@ -282,7 +282,6 @@ NOTE: This file should not be manually edited. Please edit `tests/node_compat/co
- [parallel/test-blocklist-clone.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-blocklist-clone.js) - [parallel/test-blocklist-clone.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-blocklist-clone.js)
- [parallel/test-bootstrap-modules.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-bootstrap-modules.js) - [parallel/test-bootstrap-modules.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-bootstrap-modules.js)
- [parallel/test-broadcastchannel-custom-inspect.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-broadcastchannel-custom-inspect.js) - [parallel/test-broadcastchannel-custom-inspect.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-broadcastchannel-custom-inspect.js)
- [parallel/test-buffer-compare.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-compare.js)
- [parallel/test-buffer-constructor-deprecation-error.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-constructor-deprecation-error.js) - [parallel/test-buffer-constructor-deprecation-error.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-constructor-deprecation-error.js)
- [parallel/test-buffer-constructor-node-modules-paths.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-constructor-node-modules-paths.js) - [parallel/test-buffer-constructor-node-modules-paths.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-constructor-node-modules-paths.js)
- [parallel/test-buffer-constructor-node-modules.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-constructor-node-modules.js) - [parallel/test-buffer-constructor-node-modules.js](https://github.com/nodejs/node/tree/v23.9.0/test/parallel/test-buffer-constructor-node-modules.js)

View file

@ -0,0 +1,54 @@
// deno-fmt-ignore-file
// deno-lint-ignore-file
// Copyright Joyent and Node contributors. All rights reserved. MIT license.
// Taken from Node 23.9.0
// This file is automatically generated by `tests/node_compat/runner/setup.ts`. Do not modify this file manually.
'use strict';
require('../common');
const assert = require('assert');
const b = Buffer.alloc(1, 'a');
const c = Buffer.alloc(1, 'c');
const d = Buffer.alloc(2, 'aa');
const e = new Uint8Array([ 0x61, 0x61 ]); // ASCII 'aa', same as d
assert.strictEqual(b.compare(c), -1);
assert.strictEqual(c.compare(d), 1);
assert.strictEqual(d.compare(b), 1);
assert.strictEqual(d.compare(e), 0);
assert.strictEqual(b.compare(d), -1);
assert.strictEqual(b.compare(b), 0);
assert.strictEqual(Buffer.compare(b, c), -1);
assert.strictEqual(Buffer.compare(c, d), 1);
assert.strictEqual(Buffer.compare(d, b), 1);
assert.strictEqual(Buffer.compare(b, d), -1);
assert.strictEqual(Buffer.compare(c, c), 0);
assert.strictEqual(Buffer.compare(e, e), 0);
assert.strictEqual(Buffer.compare(d, e), 0);
assert.strictEqual(Buffer.compare(d, b), 1);
assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(0)), 0);
assert.strictEqual(Buffer.compare(Buffer.alloc(0), Buffer.alloc(1)), -1);
assert.strictEqual(Buffer.compare(Buffer.alloc(1), Buffer.alloc(0)), 1);
assert.throws(() => Buffer.compare(Buffer.alloc(1), 'abc'), {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "buf2" argument must be an instance of Buffer or Uint8Array. ' +
"Received type string ('abc')"
});
assert.throws(() => Buffer.compare('abc', Buffer.alloc(1)), {
code: 'ERR_INVALID_ARG_TYPE',
message: 'The "buf1" argument must be an instance of Buffer or Uint8Array. ' +
"Received type string ('abc')"
});
assert.throws(() => Buffer.alloc(1).compare('abc'), {
code: 'ERR_INVALID_ARG_TYPE',
name: 'TypeError',
message: 'The "target" argument must be an instance of ' +
"Buffer or Uint8Array. Received type string ('abc')"
});