-
Notifications
You must be signed in to change notification settings - Fork 29.9k
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
[v10.x] buffer: fix crash for invalid index types #23795
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,17 @@ let cntr = 0; | |
} | ||
} | ||
|
||
{ | ||
// Current behavior is to coerce values to integers. | ||
b.fill(++cntr); | ||
c.fill(++cntr); | ||
const copied = b.copy(c, '0', '0', '512'); | ||
assert.strictEqual(copied, 512); | ||
for (let i = 0; i < c.length; i++) { | ||
assert.strictEqual(c[i], b[i]); | ||
} | ||
} | ||
|
||
{ | ||
// copy c into b, without specifying sourceEnd | ||
b.fill(++cntr); | ||
|
@@ -144,3 +155,18 @@ assert.strictEqual(b.copy(c, 512, 0, 10), 0); | |
assert.strictEqual(c[i], e[i]); | ||
} | ||
} | ||
|
||
// https://github.com/nodejs/node/issues/23668: Do not crash for invalid input. | ||
c.fill('c'); | ||
b.copy(c, 'not a valid offset'); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nit: An assertion after an invalid copy which ensures that nothing actually copied would be good. If the behaviour changes later on, this assertion would be able to catch that. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @thefourtheye I’ve documented the behaviour – it is not not copying anything, it is coercing, values to integer, i.e. |
||
// Make sure this acted like a regular copy with `0` offset. | ||
assert.deepStrictEqual(c, b.slice(0, c.length)); | ||
|
||
{ | ||
c.fill('C'); | ||
assert.throws(() => { | ||
b.copy(c, { [Symbol.toPrimitive]() { throw new Error('foo'); } }); | ||
}, /foo/); | ||
// No copying took place: | ||
assert.deepStrictEqual(c.toString(), 'C'.repeat(c.length)); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: If my understanding is correct, this
return;
would make theCopy
return withundefined
. Would it be better to include a test which checks that?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would not return any value – this is the case where coercing to integer throws an exception (i.e. the last part of the test)