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

fix: lower lookbehind size to needleLength - 1 #175

Merged
merged 2 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions deps/streamsearch/sbmh.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ function SBMH (needle) {
}

const needleLength = needle.length
const needleLastCharIndex = needleLength - 1

if (needleLength === 0) {
throw new Error('The needle cannot be an empty String/Buffer.')
Expand All @@ -58,12 +59,12 @@ function SBMH (needle) {
this._needle = needle
this._bufpos = 0

this._lookbehind = Buffer.alloc(needleLength)
this._lookbehind = Buffer.alloc(needleLastCharIndex)

// Populate occurrence table with analysis of the needle,
// ignoring last letter.
for (var i = 0; i < needleLength - 1; ++i) { // eslint-disable-line no-var
this._occ[needle[i]] = needleLength - 1 - i
for (var i = 0; i < needleLastCharIndex; ++i) { // eslint-disable-line no-var
this._occ[needle[i]] = needleLastCharIndex - i
}
}
inherits(SBMH, EventEmitter)
Expand Down
8 changes: 4 additions & 4 deletions test/streamsearch.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ test('streamsearch', async t => {

const expected = [
[false, Buffer.from('bar\r'), 0, 3],
[false, Buffer.from('\r\0\0'), 0, 1],
[false, Buffer.from('\r\0'), 0, 1],
[false, Buffer.from('\n\r\nhello'), 0, 8]
]
const needle = '\r\n\n'
Expand Down Expand Up @@ -339,7 +339,7 @@ test('streamsearch', async t => {
t.plan(13)

const expected = [
[false, Buffer.from('\n\n\0'), 0, 1],
[false, Buffer.from('\n\n'), 0, 1],
[true, undefined, undefined, undefined],
[false, Buffer.from('\r\nhello'), 1, 7]
]
Expand Down Expand Up @@ -374,8 +374,8 @@ test('streamsearch', async t => {

const expected = [
[false, Buffer.from('bar\r'), 0, 3],
[false, Buffer.from('\r\n\0'), 0, 2],
[false, Buffer.from('\r\n\0'), 0, 1],
[false, Buffer.from('\r\n'), 0, 2],
[false, Buffer.from('\r\n'), 0, 1],
[false, Buffer.from('hello'), 0, 5]
]
const needle = '\r\n\n'
Expand Down