on main help sign in
fix(tools): fix a potential nill crash in cached glob results
5aad790Kieran Klukas
internal/agent/tools/grep.go+13−8
@@ -38,14 +38,16 @@ func newRegexCache() *regexCache {
38
38
39// get retrieves a compiled regex from cache or compiles and caches it
39// get retrieves a compiled regex from cache or compiles and caches it
40func (rc *regexCache) get(pattern string) (*regexp.Regexp, error) {
40func (rc *regexCache) get(pattern string) (*regexp.Regexp, error) {
41− var rerr error
41+ re, ok := rc.Get(pattern)
42− return rc.GetOrSet(pattern, func() *regexp.Regexp {
42+ if ok && re != nil {
43+ return re, nil
44+ }
43− regex, err := regexp.Compile(pattern)
45+ re, err := regexp.Compile(pattern)
44− if err != nil {
46+ if err != nil {
45− rerr = err
47+ return nil, err
46− }
48+ }
47− return regex
49+ rc.Set(pattern, re)
48− }), rerr
50+ return re, nil
49}
51}
50
52
51// ResetCache clears compiled regex caches to prevent unbounded growth across sessions.
53// ResetCache clears compiled regex caches to prevent unbounded growth across sessions.
@@ -339,6 +341,9 @@ func searchFilesWithRegex(pattern, rootPath, include string) ([]grepMatch, error
339}
341}
340
342
341func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, int, string, error) {
343func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, int, string, error) {
344+ if pattern == nil {
345+ return false, 0, 0, "", nil
346+ }
342 // Only search text files.
347 // Only search text files.
343 if !isTextFile(filePath) {
348 if !isTextFile(filePath) {
344 return false, 0, 0, "", nil
349 return false, 0, 0, "", nil