Skip to content

Commit eece7f3

Browse files
authored
fix: lower lookbehind size to needleLength - 1 (#175)
* lower lookbehind length * use needleLastCharIndex
1 parent 74c3e23 commit eece7f3

File tree

2 files changed

+8
-7
lines changed

2 files changed

+8
-7
lines changed

deps/streamsearch/sbmh.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ function SBMH (needle) {
4040
}
4141

4242
const needleLength = needle.length
43+
const needleLastCharIndex = needleLength - 1
4344

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

61-
this._lookbehind = Buffer.alloc(needleLength)
62+
this._lookbehind = Buffer.alloc(needleLastCharIndex)
6263

6364
// Populate occurrence table with analysis of the needle,
6465
// ignoring last letter.
65-
for (var i = 0; i < needleLength - 1; ++i) { // eslint-disable-line no-var
66-
this._occ[needle[i]] = needleLength - 1 - i
66+
for (var i = 0; i < needleLastCharIndex; ++i) { // eslint-disable-line no-var
67+
this._occ[needle[i]] = needleLastCharIndex - i
6768
}
6869
}
6970
inherits(SBMH, EventEmitter)

test/streamsearch.test.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -244,7 +244,7 @@ test('streamsearch', async t => {
244244

245245
const expected = [
246246
[false, Buffer.from('bar\r'), 0, 3],
247-
[false, Buffer.from('\r\0\0'), 0, 1],
247+
[false, Buffer.from('\r\0'), 0, 1],
248248
[false, Buffer.from('\n\r\nhello'), 0, 8]
249249
]
250250
const needle = '\r\n\n'
@@ -339,7 +339,7 @@ test('streamsearch', async t => {
339339
t.plan(13)
340340

341341
const expected = [
342-
[false, Buffer.from('\n\n\0'), 0, 1],
342+
[false, Buffer.from('\n\n'), 0, 1],
343343
[true, undefined, undefined, undefined],
344344
[false, Buffer.from('\r\nhello'), 1, 7]
345345
]
@@ -374,8 +374,8 @@ test('streamsearch', async t => {
374374

375375
const expected = [
376376
[false, Buffer.from('bar\r'), 0, 3],
377-
[false, Buffer.from('\r\n\0'), 0, 2],
378-
[false, Buffer.from('\r\n\0'), 0, 1],
377+
[false, Buffer.from('\r\n'), 0, 2],
378+
[false, Buffer.from('\r\n'), 0, 1],
379379
[false, Buffer.from('hello'), 0, 5]
380380
]
381381
const needle = '\r\n\n'

0 commit comments

Comments
 (0)