From 2b5c4b6975a493ccf12a48b78759a4cada7649f8 Mon Sep 17 00:00:00 2001 From: Robert Krimen Date: Wed, 21 Nov 2012 18:19:15 -0800 Subject: [PATCH] Safeguard RegExp.exec against non-RegExp --- otto_test.go | 5 +++++ type_regexp.go | 3 +++ 2 files changed, 8 insertions(+) diff --git a/otto_test.go b/otto_test.go index 26c108e..f8a38e2 100644 --- a/otto_test.go +++ b/otto_test.go @@ -1655,6 +1655,11 @@ func TestRegExp_exec(t *testing.T) { } [ ghi, lastIndex ]; `, "3,7") + + test(`raise: + var exec = RegExp.prototype.exec; + exec("Xyzzy"); + `, "TypeError: Calling RegExp.exec on a non-RegExp object") } func TestNewFunction(t *testing.T) { diff --git a/type_regexp.go b/type_regexp.go index a1ecf57..ea68cb9 100644 --- a/type_regexp.go +++ b/type_regexp.go @@ -68,6 +68,9 @@ func (runtime *_runtime) newRegExpObject(pattern string, flags string) *_object } func execRegExp(this *_object, target string) (match bool, result []int) { + if this.class != "RegExp" { + panic(newTypeError("Calling RegExp.exec on a non-RegExp object")) + } lastIndex := toInteger(this.get("lastIndex")) index := lastIndex global := toBoolean(this.get("global"))