From 6637fa4657d85e9b80bbcb4c6839f384d69912b3 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Tue, 30 Jun 2026 07:33:22 +0000 Subject: [PATCH] fix(commits): avoid mutating cache key inputs --- commits/cache.go | 5 +++-- commits/cache_test.go | 13 +++++++++++++ 2 files changed, 16 insertions(+), 2 deletions(-) diff --git a/commits/cache.go b/commits/cache.go index 0c754dd..5b00ef7 100644 --- a/commits/cache.go +++ b/commits/cache.go @@ -31,9 +31,10 @@ func init() { } func hashSlice(in []string) string { - sort.Strings(in) + sorted := append([]string(nil), in...) + sort.Strings(sorted) sb := strings.Builder{} - for _, s := range in { + for _, s := range sorted { sb.WriteString(s) } h := md5.New() diff --git a/commits/cache_test.go b/commits/cache_test.go index c7df96c..583870e 100644 --- a/commits/cache_test.go +++ b/commits/cache_test.go @@ -15,6 +15,19 @@ func TestHashSliceDeterministic(t *testing.T) { } } +func TestHashSliceDoesNotMutateInput(t *testing.T) { + input := []string{"foo", "bar", "baz"} + want := []string{"foo", "bar", "baz"} + + _ = hashSlice(input) + + for i := range want { + if input[i] != want[i] { + t.Fatalf("hashSlice mutated input at index %d: got %q, want %q", i, input[i], want[i]) + } + } +} + func TestHashSliceDifferentInputs(t *testing.T) { a := hashSlice([]string{"foo", "bar"}) b := hashSlice([]string{"foo", "baz"})