Skip to content

Commit

Permalink
fix: lower lookbehind size to needleLength - 1
Browse files Browse the repository at this point in the history
  • Loading branch information
gurgunday committed Nov 28, 2024
1 parent 9104e51 commit 6879090
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
17 changes: 9 additions & 8 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 @@ -52,18 +53,17 @@ function SBMH (needle) {
this.maxMatches = Infinity
this.matches = 0

this._needle = needle
this._occ = new Array(256)
.fill(needleLength) // Initialize occurrence table.
this._lookbehind = Buffer.alloc(needleLastCharIndex)
this._lookbehind_size = 0
this._needle = needle
this._bufpos = 0

this._lookbehind = Buffer.alloc(needleLength)

// 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 (let i = 0; i < needleLastCharIndex; ++i) {
this._occ[needle[i]] = needleLastCharIndex - i
}
}
inherits(SBMH, EventEmitter)
Expand All @@ -89,7 +89,8 @@ SBMH.prototype._sbmh_feed = function (data) {
const len = data.length
const needle = this._needle
const needleLength = needle.length
const lastNeedleChar = needle[needleLength - 1]
const needleLastCharIndex = needleLength - 1
const lastNeedleChar = needle[needleLastCharIndex]

// Positive: points to a position in `data`
// pos == 3 points to data[3]
Expand All @@ -112,11 +113,11 @@ SBMH.prototype._sbmh_feed = function (data) {
// or until
// the character to look at lies outside the haystack.
while (pos < 0 && pos <= len - needleLength) {
ch = this._sbmh_lookup_char(data, pos + needleLength - 1)
ch = this._sbmh_lookup_char(data, pos + needleLastCharIndex)

if (
ch === lastNeedleChar &&
this._sbmh_memcmp(data, pos, needleLength - 1)
this._sbmh_memcmp(data, pos, needleLastCharIndex)
) {
this._lookbehind_size = 0
++this.matches
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

0 comments on commit 6879090

Please sign in to comment.