Skip to content
This repository was archived by the owner on Sep 10, 2025. It is now read-only.
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