Skip to content

Commit

Permalink
Fixes #212.
Browse files Browse the repository at this point in the history
Empty buffers have `nullptr` as data. Empty strings have some address.
The pointer math doesn't work with `nullptr`.
  • Loading branch information
uhop committed Jun 13, 2024
1 parent 07c2eb3 commit 391af15
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/addon.cc
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ void WrappedRE2::dropCache()
lastStringValue.clear();
}

const StrVal& WrappedRE2::prepareArgument(const v8::Local<v8::Value> &arg, bool ignoreLastIndex)
const StrVal &WrappedRE2::prepareArgument(const v8::Local<v8::Value> &arg, bool ignoreLastIndex)
{
size_t startFrom = ignoreLastIndex ? 0 : lastIndex;

Expand Down Expand Up @@ -204,12 +204,14 @@ void StrVal::setIndex(size_t newIndex)
index = newIndex;
}

static char null_buffer[] = {'\0'};

void StrVal::reset(const v8::Local<v8::Value> &arg, size_t argSize, size_t argLength, size_t newIndex, bool buffer)
{
clear();
isBuffer = buffer;
size = argSize;
length = argLength;
data = node::Buffer::Data(arg);
data = size ? node::Buffer::Data(arg) : null_buffer;
setIndex(newIndex);
}
12 changes: 12 additions & 0 deletions tests/test_exec.js
Original file line number Diff line number Diff line change
Expand Up @@ -455,5 +455,17 @@ xy2 (at start of line)

eval(t.TEST('result1.index === 2'));
eval(t.TEST('result2.index === 2'));
},

function test_foundEmptyString(t) {
'use strict';

const re2 = new RE2('^.*?'),
match = re2.exec('');

eval(t.TEST("match[0] === ''"));
eval(t.TEST("match.index === 0"));
eval(t.TEST("match.input === ''"));
eval(t.TEST('match.groups === undefined'));
}
]);

0 comments on commit 391af15

Please sign in to comment.