From e9d98bc8f959928467ee6e13dde14e1df3cf0669 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Fri, 17 Nov 2023 12:59:28 +0200 Subject: [PATCH 1/3] Ensure bogus regexps are caught --- evaluator/evaluator_test.go | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) diff --git a/evaluator/evaluator_test.go b/evaluator/evaluator_test.go index 454e4b0..d5c9a11 100644 --- a/evaluator/evaluator_test.go +++ b/evaluator/evaluator_test.go @@ -688,3 +688,36 @@ return total; } } + +// TestIssue94 tests #94 - that malformed regexps are caught +func TestIssue94(t *testing.T) { + literal := ` +name = "Steve" + +if ( name ~= /+/i ) { puts( "Hello\n" ); } +` + ev1 := testEval(literal) + er1, ok1 := ev1.(*object.Error) + if !ok1 { + t.Errorf("Expected an error wit hab ogus regexp, got none") + } + + if !strings.Contains(er1.Inspect(), "parsing regexp") { + t.Errorf("Got an error, but not the right one:%v", er1.Inspect()) + } + + match := ` +name = "Steve" + +if (match( "+", name) ) { puts( "Hello\n" ); } +` + ev2 := testEval(match) + er2, ok2 := ev2.(*object.Error) + if !ok2 { + t.Errorf("Expected an error with a bogus regexp, got none") + } + if !strings.Contains(er2.Inspect(), "parsing regexp") { + t.Errorf("Got an error, but not the right one:%v", er2.Inspect()) + } + +} From f658a86224b2728dbc40afe799106ecda8c81f59 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Fri, 17 Nov 2023 13:03:53 +0200 Subject: [PATCH 2/3] Correct the stdlib match function --- evaluator/stdlib_core.go | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/evaluator/stdlib_core.go b/evaluator/stdlib_core.go index 3823354..21593ac 100644 --- a/evaluator/stdlib_core.go +++ b/evaluator/stdlib_core.go @@ -171,7 +171,11 @@ func matchFun(args ...object.Object) object.Object { // // Compile and match // - reg := regexp.MustCompile(args[0].(*object.String).Value) + reg, err := regexp.Compile(args[0].(*object.String).Value) + if err != nil { + return newError("failed to compile regexp %s: %s", + args[0].Inspect(), err) + } res := reg.FindStringSubmatch(args[1].(*object.String).Value) if len(res) > 0 { From d7c77bd84e5562755ced78464d5442fc32882c33 Mon Sep 17 00:00:00 2001 From: Steve Kemp Date: Fri, 17 Nov 2023 13:04:57 +0200 Subject: [PATCH 3/3] Corrected spelling on test-log --- evaluator/evaluator_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/evaluator/evaluator_test.go b/evaluator/evaluator_test.go index d5c9a11..757efbc 100644 --- a/evaluator/evaluator_test.go +++ b/evaluator/evaluator_test.go @@ -699,7 +699,7 @@ if ( name ~= /+/i ) { puts( "Hello\n" ); } ev1 := testEval(literal) er1, ok1 := ev1.(*object.Error) if !ok1 { - t.Errorf("Expected an error wit hab ogus regexp, got none") + t.Errorf("Expected an error with a bogus regexp, got none") } if !strings.Contains(er1.Inspect(), "parsing regexp") {