-
-
Notifications
You must be signed in to change notification settings - Fork 36.7k
buffer: fix UCS2 search on unaligned buffer views #65968
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
Open
bitpshr
wants to merge
1
commit into
nodejs:main
Choose a base branch
from
bitpshr:buffer/ucs2-unaligned-indexof
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+99
−8
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,67 @@ | ||
| 'use strict'; | ||
|
|
||
| // A Buffer view can start at an odd byte offset, so its data is not | ||
| // necessarily aligned for two byte reads. The UCS2 search must still work | ||
| // rather than hang or report the wrong offset. | ||
| // Refs: https://github.com/nodejs/node/issues/65959 | ||
|
|
||
| require('../common'); | ||
| const assert = require('assert'); | ||
|
|
||
| const text = 'abc'; | ||
|
|
||
| for (const prefixLength of [0, 1, 2, 3]) { | ||
| const data = Buffer.from(text, 'utf16le'); | ||
| const backing = Buffer.alloc(data.length + prefixLength); | ||
| data.copy(backing, prefixLength); | ||
| const view = backing.subarray(prefixLength); | ||
| const label = `prefixLength=${prefixLength}`; | ||
|
|
||
| // A view at an odd offset holds the same bytes as an aligned one, so every | ||
| // lookup below has the same answer either way. | ||
| for (let i = 0; i < text.length; i++) { | ||
| const needle = text[i]; | ||
| const expected = i * 2; | ||
|
|
||
| assert.strictEqual( | ||
| view.indexOf(needle, 0, 'utf16le'), | ||
| expected, | ||
| `indexOf(${needle}) ${label}`, | ||
| ); | ||
| assert.strictEqual( | ||
| view.indexOf(Buffer.from(needle, 'utf16le'), 0, 'utf16le'), | ||
| expected, | ||
| `indexOf(Buffer ${needle}) ${label}`, | ||
| ); | ||
| assert.strictEqual( | ||
| view.lastIndexOf(needle, undefined, 'utf16le'), | ||
| expected, | ||
| `lastIndexOf(${needle}) ${label}`, | ||
| ); | ||
| assert.ok( | ||
| view.includes(needle, 0, 'utf16le'), | ||
| `includes(${needle}) ${label}`, | ||
| ); | ||
| } | ||
|
|
||
| // A needle that is not present is still reported as missing. | ||
| assert.strictEqual(view.indexOf('z', 0, 'utf16le'), -1, `indexOf(z) ${label}`); | ||
| assert.strictEqual( | ||
| view.lastIndexOf('z', undefined, 'utf16le'), | ||
| -1, | ||
| `lastIndexOf(z) ${label}`, | ||
| ); | ||
| assert.ok(!view.includes('z', 0, 'utf16le'), `includes(z) ${label}`); | ||
| } | ||
|
|
||
| // A multi character needle, so the search does more than one comparison. | ||
| { | ||
| const data = Buffer.from('hello world', 'utf16le'); | ||
| const backing = Buffer.alloc(data.length + 1); | ||
| data.copy(backing, 1); | ||
| const view = backing.subarray(1); | ||
|
|
||
| assert.strictEqual(view.indexOf('world', 0, 'utf16le'), 12); | ||
| assert.strictEqual(view.lastIndexOf('o', undefined, 'utf16le'), 14); | ||
| assert.strictEqual(view.indexOf('worlds', 0, 'utf16le'), -1); | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Could we avoid copying the full
[0, search_end)range, at least for forward searches with a large starting offset?For a large unaligned Buffer, even a search starting near the end would currently allocate and copy almost the entire search range. Have you considered limiting the copy to the range that can actually be searched, or benchmarked the cost of the current approach?