support another String.prototype.replace pattern

This commit is contained in:
Erik Krogh Kristensen
2021-03-16 13:25:13 +01:00
parent c08230ce1e
commit 1bf259beef
2 changed files with 16 additions and 1 deletions

View File

@@ -149,6 +149,19 @@ class StringReplaceCall extends DataFlow::MethodCallNode {
pr.flowsTo(replacer.getAReturn()) and
map.hasPropertyWrite(old, any(DataFlow::Node repl | repl.getStringValue() = new))
)
or
exists(
DataFlow::FunctionNode replacer, ConditionGuardNode guard, EqualityTest test,
DataFlow::Node ret
|
replacer = getCallback(1) and
guard.getTest() = test and
replacer.getParameter(0).flowsToExpr(test.getAnOperand()) and
test.getAnOperand().getStringValue() = old and
ret = replacer.getAReturn() and
guard.dominates(ret.getBasicBlock()) and
new = ret.getStringValue()
)
}
}

View File

@@ -51,7 +51,9 @@ class StringReplaceCallSequence extends DataFlow::CallNode {
/** Gets a string that is the replacement of this call. */
string getAReplacementString() {
// this is more restrictive than `StringReplaceCall::replaces/2`, in the name of precision
getAMember().replaces(_, result)
or
// StringReplaceCall::replaces/2 can't always find the `old` string, so this is added as a falback.
getAMember().getRawReplacement().getStringValue() = result
}