on main help sign in
fix(tools): fix a potential nill crash in cached glob results
commit5aad7904dba7dab2607d96cbdb3e62483316cea3 authorKieran Klukas <kieran@dunkirk.sh> dateThu 7 May 2026, 09:51 (5 months ago) parent8bc4a75
1 file changed+13−8
internal/agent/tools/grep.go+13−8
internal/agent/tools/grep.go+13−8
@@ -38,14 +38,16 @@ func newRegexCache() *regexCache {
3838
3939// get retrieves a compiled regex from cache or compiles and caches it
4040func (rc *regexCache) get(pattern string) (*regexp.Regexp, error) {
41− var rerr error
42− return rc.GetOrSet(pattern, func() *regexp.Regexp {
43− regex, err := regexp.Compile(pattern)
44− if err != nil {
45− rerr = err
46− }
47− return regex
48− }), rerr
41+ re, ok := rc.Get(pattern)
42+ if ok && re != nil {
43+ return re, nil
44+ }
45+ re, err := regexp.Compile(pattern)
46+ if err != nil {
47+ return nil, err
48+ }
49+ rc.Set(pattern, re)
50+ return re, nil
4951}
5052
5153// ResetCache clears compiled regex caches to prevent unbounded growth across sessions.
@@ -339,6 +341,9 @@ func searchFilesWithRegex(pattern, rootPath, include string) ([]grepMatch, error
339341}
340342
341343func fileContainsPattern(filePath string, pattern *regexp.Regexp) (bool, int, int, string, error) {
344+ if pattern == nil {
345+ return false, 0, 0, "", nil
346+ }
342347 // Only search text files.
343348 if !isTextFile(filePath) {
344349 return false, 0, 0, "", nil