Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

94 - Ensure bogus regexps are caught #95

Merged
merged 3 commits into from
Nov 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions evaluator/evaluator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 with a bogus 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())
}

}
6 changes: 5 additions & 1 deletion evaluator/stdlib_core.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down