diff --git a/lib/addon.cc b/lib/addon.cc index 318250a..0dc7f73 100644 --- a/lib/addon.cc +++ b/lib/addon.cc @@ -112,7 +112,7 @@ void WrappedRE2::dropCache() lastStringValue.clear(); } -const StrVal& WrappedRE2::prepareArgument(const v8::Local &arg, bool ignoreLastIndex) +const StrVal &WrappedRE2::prepareArgument(const v8::Local &arg, bool ignoreLastIndex) { size_t startFrom = ignoreLastIndex ? 0 : lastIndex; @@ -204,12 +204,14 @@ void StrVal::setIndex(size_t newIndex) index = newIndex; } +static char null_buffer[] = {'\0'}; + void StrVal::reset(const v8::Local &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); } diff --git a/tests/test_exec.js b/tests/test_exec.js index 21796bf..e1a89b5 100644 --- a/tests/test_exec.js +++ b/tests/test_exec.js @@ -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')); } ]);