From a64ca94c0d9f5c56ba8ad4a70fba2d6b0a8df8f1 Mon Sep 17 00:00:00 2001 From: Colin Cross Date: Thu, 21 Jan 2021 13:47:59 -0800 Subject: [PATCH] Return a copy of glob lists Callers to glob methods may do in-place modifications on the returned list of globs, return a copy instead of the cached value. Test: m nothing && m nothing Change-Id: Ic9140d1e1900e8724ba0a484f27786e5c15dea90 --- glob.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/glob.go b/glob.go index 4f7e978..6a3fd58 100644 --- a/glob.go +++ b/glob.go @@ -57,7 +57,8 @@ func (c *Context) glob(pattern string, excludes []string) ([]string, error) { if exists { // Glob has already been done, double check it is identical verifyGlob(fileName, pattern, excludes, g) - return g.Files, nil + // Return a copy so that modifications don't affect the cached value. + return append([]string(nil), g.Files...), nil } // Get a globbed file list @@ -76,10 +77,12 @@ func (c *Context) glob(pattern string, excludes []string) ([]string, error) { // Getting the list raced with another goroutine, throw away the results and use theirs if exists { verifyGlob(fileName, pattern, excludes, g) - return g.Files, nil + // Return a copy so that modifications don't affect the cached value. + return append([]string(nil), g.Files...), nil } - return files, nil + // Return a copy so that modifications don't affect the cached value. + return append([]string(nil), files...), nil } func (c *Context) Globs() []GlobPath {