-
-
Notifications
You must be signed in to change notification settings - Fork 35.3k
[v22.x] src: clamp WriteUtf8 capacity to INT_MAX in EncodeInto #62621
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
base: v22.x-staging
Are you sure you want to change the base?
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 | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,41 @@ | ||||||||||||||||||||||||||
| 'use strict'; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const common = require('../common'); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| common.skipIf32Bits(); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const assert = require('assert'); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const encoder = new TextEncoder(); | ||||||||||||||||||||||||||
| const source = 'a\xFF\u6211\u{1D452}'; | ||||||||||||||||||||||||||
| const expected = encoder.encode(source); | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const size = 2 ** 31 + expected.length; | ||||||||||||||||||||||||||
| const offset = expected.length - 1; | ||||||||||||||||||||||||||
| let dest; | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||||
| dest = new Uint8Array(size); | ||||||||||||||||||||||||||
| } catch (e) { | ||||||||||||||||||||||||||
| if (e.code === 'ERR_MEMORY_ALLOCATION_FAILED' || | ||||||||||||||||||||||||||
| /Array buffer allocation failed/.test(e.message)) { | ||||||||||||||||||||||||||
| common.skip('insufficient space for Uint8Array allocation'); | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
| throw e; | ||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const large = encoder.encodeInto(source, dest.subarray(offset)); | ||||||||||||||||||||||||||
| assert.deepStrictEqual(large, { | ||||||||||||||||||||||||||
| read: source.length, | ||||||||||||||||||||||||||
| written: expected.length, | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| assert.deepStrictEqual(dest.slice(offset, offset + expected.length), expected); | ||||||||||||||||||||||||||
|
Comment on lines
+27
to
+32
Member
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. There's really no need to mess around with creating a typed array and then deriving a slightly shorter array that's still larger than INT_MAX. Just make the typed array with size
Suggested change
|
||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| const bounded = encoder.encodeInto(source, | ||||||||||||||||||||||||||
| dest.subarray(offset, | ||||||||||||||||||||||||||
| offset + expected.length)); | ||||||||||||||||||||||||||
| assert.deepStrictEqual(bounded, { | ||||||||||||||||||||||||||
| read: source.length, | ||||||||||||||||||||||||||
| written: expected.length, | ||||||||||||||||||||||||||
| }); | ||||||||||||||||||||||||||
| assert.deepStrictEqual(dest.slice(offset, offset + expected.length), expected); | ||||||||||||||||||||||||||
|
Comment on lines
+34
to
+41
Member
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. This test is unnecessary – it isn't testing passing a large typed array to encodeInto, and succeeds both before and after the change. |
||||||||||||||||||||||||||
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.
Checking for a specific error condition is unnecessary here – there is no other error that can occur when calling
new TypedArray(n)with a safe integer.