aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTyler Christensen <tyler9xp@gmail.com>2022-04-30 16:06:10 -0600
committerGitHub <noreply@github.com>2022-05-01 00:06:10 +0200
commit69aa78ab169e8fa5d96561462b0a07aa5030bad6 (patch)
tree1760b8a8231b453e5e28f8d1a4f941985801f90e
parent4f916225cb2f5f96c4f046b139d2a597387b8f8e (diff)
downloadgo-git-69aa78ab169e8fa5d96561462b0a07aa5030bad6.tar.gz
plumbing: packp, Avoid duplicate encoding when overriding a Capability value. (#521)
Previously, calling `Set($CAPABILITY, ...)` on a `capability.List` where `$CAPABILITY` was already present would correctly replace the existing value of that capability, but would also result in that capability being listed twice in the internal `l.sort` slice. This manifested publicly when the `List` was encoded as the same capability appearing twice with the same value in the encoded output.
-rw-r--r--plumbing/protocol/packp/capability/list.go4
-rw-r--r--plumbing/protocol/packp/capability/list_test.go11
2 files changed, 14 insertions, 1 deletions
diff --git a/plumbing/protocol/packp/capability/list.go b/plumbing/protocol/packp/capability/list.go
index f41ec79..553d81c 100644
--- a/plumbing/protocol/packp/capability/list.go
+++ b/plumbing/protocol/packp/capability/list.go
@@ -86,7 +86,9 @@ func (l *List) Get(capability Capability) []string {
// Set sets a capability removing the previous values
func (l *List) Set(capability Capability, values ...string) error {
- delete(l.m, capability)
+ if _, ok := l.m[capability]; ok {
+ l.m[capability].Values = l.m[capability].Values[:0]
+ }
return l.Add(capability, values...)
}
diff --git a/plumbing/protocol/packp/capability/list_test.go b/plumbing/protocol/packp/capability/list_test.go
index 61b0b13..71181cb 100644
--- a/plumbing/protocol/packp/capability/list_test.go
+++ b/plumbing/protocol/packp/capability/list_test.go
@@ -122,6 +122,17 @@ func (s *SuiteCapabilities) TestSetEmpty(c *check.C) {
c.Assert(cap.Get(Agent), check.HasLen, 1)
}
+func (s *SuiteCapabilities) TestSetDuplicate(c *check.C) {
+ cap := NewList()
+ err := cap.Set(Agent, "baz")
+ c.Assert(err, check.IsNil)
+
+ err = cap.Set(Agent, "bar")
+ c.Assert(err, check.IsNil)
+
+ c.Assert(cap.String(), check.Equals, "agent=bar")
+}
+
func (s *SuiteCapabilities) TestGetEmpty(c *check.C) {
cap := NewList()
c.Assert(cap.Get(Agent), check.HasLen, 0)