From ad2738076d9118ff7de25f3ade46dea0c8b5a429 Mon Sep 17 00:00:00 2001 From: sanaz Date: Thu, 20 Jul 2023 10:27:26 -0700 Subject: [PATCH 1/3] fixes mutability of the Repeat output --- pkg/namespace/namespace.go | 16 +++++++++++++++- pkg/namespace/namespace_test.go | 13 +++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/pkg/namespace/namespace.go b/pkg/namespace/namespace.go index 5da38bc398..135b8af686 100644 --- a/pkg/namespace/namespace.go +++ b/pkg/namespace/namespace.go @@ -148,7 +148,7 @@ func (n Namespace) IsPayForBlob() bool { func (n Namespace) Repeat(times int) []Namespace { ns := make([]Namespace, times) for i := 0; i < times; i++ { - ns[i] = n + ns[i] = n.deepCopyNamespace() } return ns } @@ -180,3 +180,17 @@ func leftPad(b []byte, size int) []byte { pad := make([]byte, size-len(b)) return append(pad, b...) } + +func (n Namespace) deepCopyNamespace() Namespace { + // Create a deep copy of the ID slice + copyID := make([]byte, len(n.ID)) + copy(copyID, n.ID) + + // Create a new Namespace object with the copied fields + copyNamespace := Namespace{ + Version: n.Version, + ID: copyID, + } + + return copyNamespace +} diff --git a/pkg/namespace/namespace_test.go b/pkg/namespace/namespace_test.go index f7b97487dc..199a5d0a79 100644 --- a/pkg/namespace/namespace_test.go +++ b/pkg/namespace/namespace_test.go @@ -73,6 +73,19 @@ func TestNew(t *testing.T) { } } +// TestRepeatNonMutability ensures that the output of Repeat method is not mutated when the original namespace is mutated. +func TestRepeatNonMutability(t *testing.T) { + n := 10 + namespace := Namespace{Version: NamespaceVersionMax, ID: []byte{1, 2, 3, 4}} + repeated := namespace.Repeat(n) + // mutate the original namespace + namespace.ID[0] = 5 + // ensure the repeated namespaces are not mutated + for i := 0; i < n; i++ { + assert.NotEqual(t, repeated[i], namespace) + } +} + func TestNewV0(t *testing.T) { type testCase struct { name string From 1e3a60799c5c744aa628c0e44b493e080f9a8a90 Mon Sep 17 00:00:00 2001 From: sanaz Date: Thu, 20 Jul 2023 10:29:16 -0700 Subject: [PATCH 2/3] renames deepCopyNamespace --- pkg/namespace/namespace.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/pkg/namespace/namespace.go b/pkg/namespace/namespace.go index 135b8af686..13f3b7c37e 100644 --- a/pkg/namespace/namespace.go +++ b/pkg/namespace/namespace.go @@ -148,7 +148,7 @@ func (n Namespace) IsPayForBlob() bool { func (n Namespace) Repeat(times int) []Namespace { ns := make([]Namespace, times) for i := 0; i < times; i++ { - ns[i] = n.deepCopyNamespace() + ns[i] = n.deepCopy() } return ns } @@ -181,7 +181,7 @@ func leftPad(b []byte, size int) []byte { return append(pad, b...) } -func (n Namespace) deepCopyNamespace() Namespace { +func (n Namespace) deepCopy() Namespace { // Create a deep copy of the ID slice copyID := make([]byte, len(n.ID)) copy(copyID, n.ID) From 0da10b6f8496e2cb55ac7de86d7f628cca97d97d Mon Sep 17 00:00:00 2001 From: sanaz Date: Thu, 20 Jul 2023 10:39:25 -0700 Subject: [PATCH 3/3] adds godoc for deepCopy --- pkg/namespace/namespace.go | 1 + 1 file changed, 1 insertion(+) diff --git a/pkg/namespace/namespace.go b/pkg/namespace/namespace.go index 13f3b7c37e..b6ab358d04 100644 --- a/pkg/namespace/namespace.go +++ b/pkg/namespace/namespace.go @@ -181,6 +181,7 @@ func leftPad(b []byte, size int) []byte { return append(pad, b...) } +// deepCopy returns a deep copy of the Namespace object. func (n Namespace) deepCopy() Namespace { // Create a deep copy of the ID slice copyID := make([]byte, len(n.ID))