Skip to content

Commit

Permalink
Fix panic in regex execution (#1283)
Browse files Browse the repository at this point in the history
* Fix panic with regex exec

* Add toLength comment
  • Loading branch information
0x7D2B authored May 27, 2021
1 parent 3acef7b commit d009fa2
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions boa/src/builtins/regexp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -466,7 +466,8 @@ impl RegExp {
/// [spec]: https://tc39.es/ecma262/#sec-regexp.prototype.test
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/test
pub(crate) fn test(this: &Value, args: &[Value], context: &mut Context) -> Result<Value> {
let mut last_index = this.get_field("lastIndex", context)?.to_index(context)?;
// 22.2.5.2.2.4 really says to use "toLength" and not "toIndex"
let mut last_index = this.get_field("lastIndex", context)?.to_length(context)?;
let result = if let Some(object) = this.as_object() {
// 3. Let string be ? ToString(S).
let arg_str = args
Expand Down Expand Up @@ -520,7 +521,8 @@ impl RegExp {
/// [mdn]: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/RegExp/exec
pub(crate) fn exec(this: &Value, args: &[Value], context: &mut Context) -> Result<Value> {
// 4. Return ? RegExpBuiltinExec(R, S).
let mut last_index = this.get_field("lastIndex", context)?.to_index(context)?;
// 22.2.5.2.2.4 really says to use "toLength" and not "toIndex"
let mut last_index = this.get_field("lastIndex", context)?.to_length(context)?;
let result = if let Some(object) = this.as_object() {
let object = object.borrow();
if let Some(regex) = object.as_regexp() {
Expand All @@ -532,7 +534,14 @@ impl RegExp {
.to_string(context)?;

let result = {
if let Some(m) = regex.matcher.find_from(arg_str.as_str(), last_index).next() {
if last_index > arg_str.len() {
if regex.use_last_index {
last_index = 0;
}
Value::null()
} else if let Some(m) =
regex.matcher.find_from(arg_str.as_str(), last_index).next()
{
if regex.use_last_index {
last_index = m.end();
}
Expand Down

0 comments on commit d009fa2

Please sign in to comment.