Skip to content

Commit

Permalink
In RegExp ctor, we should replace \n into \\n
Browse files Browse the repository at this point in the history
Signed-off-by: Seonghyun Kim <[email protected]>
  • Loading branch information
ksh8281 authored and clover2123 committed Jul 24, 2024
1 parent 7762be6 commit 21903f9
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions src/runtime/RegExpObject.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,27 @@ void RegExpObject::initRegExpObject(ExecutionState& state, bool hasLastIndex)
}
}

static String* escapeSlashInPattern(String* patternStr)
static String* escapePattern(String* patternStr)
{
if (patternStr->length() == 0) {
return patternStr;
}

// replace \n into \\n
if (patternStr->contains("\n")) {
StringBuilder builder;
auto accessData = patternStr->bufferAccessData();
for (size_t i = 0; i < accessData.length; i++) {
if (accessData.charAt(i) == '\n' && (i == 0 || accessData.charAt(i - 1) != '\\')) {
builder.appendChar('\\');
builder.appendChar('n');
} else {
builder.appendChar(accessData.charAt(i));
}
}
patternStr = builder.finalize();
}

auto accessData = patternStr->bufferAccessData();

const size_t& len = accessData.length;
Expand Down Expand Up @@ -148,7 +163,7 @@ void RegExpObject::internalInit(ExecutionState& state, String* source, Option op

setOptionValueForGC(option);
m_source = source->length() ? source : defaultRegExpString;
m_source = escapeSlashInPattern(m_source);
m_source = escapePattern(m_source);

auto& entry = getCacheEntryAndCompileIfNeeded(state, m_source, this->option());
if (entry.m_yarrError) {
Expand Down

0 comments on commit 21903f9

Please sign in to comment.