Skip to content

Stop using MustCompile, and instead return an error if any regex fails to compile #67

@veqryn

Description

@veqryn

The use of regex.MustCompile (see:

uap-go/uaparser/parser.go

Lines 354 to 360 in daf92ba

func compileRegex(flags, expr string) *regexp.Regexp {
if flags == "" {
return regexp.MustCompile(expr)
} else {
return regexp.MustCompile(fmt.Sprintf("(?%s)%s", flags, expr))
}
}
) means that if any regex fails, there is a panic.

We pull down the master regex url daily, so that our regexes are always up to date, and load it right into our servers. The panic cause all our servers to crash last night, until we pinned the master regex url to a working version.

NewFromBytes already has the option to return an error, so why not return the error there?

uap-go/uaparser/parser.go

Lines 209 to 219 in daf92ba

func NewFromBytes(data []byte) (*Parser, error) {
var definitions RegexesDefinitions
if err := yaml.Unmarshal(data, &definitions); err != nil {
return nil, err
}
parser := &Parser{definitions, 0, 0, 0, (EOsLookUpMode|EUserAgentLookUpMode|EDeviceLookUpMode), false, false}
parser.mustCompile()
return parser, nil
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions