Skip to content

Commit

Permalink
Make argsKV more predictable
Browse files Browse the repository at this point in the history
Fixes #932
  • Loading branch information
erikdubbelboer committed Dec 26, 2020
1 parent d0dfbd4 commit 4e63057
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 2 deletions.
10 changes: 9 additions & 1 deletion args.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,13 @@ func visitArgs(args []argsKV, f func(k, v []byte)) {
func copyArgs(dst, src []argsKV) []argsKV {
if cap(dst) < len(src) {
tmp := make([]argsKV, len(src))
dst = dst[:cap(dst)] // copy all of dst.
copy(tmp, dst)
for i := len(dst); i < len(tmp); i++ {
// Make sure nothing is nil.
tmp[i].key = []byte{}
tmp[i].value = []byte{}
}
dst = tmp
}
n := len(src)
Expand Down Expand Up @@ -442,7 +448,9 @@ func allocArg(h []argsKV) ([]argsKV, *argsKV) {
if cap(h) > n {
h = h[:n+1]
} else {
h = append(h, argsKV{})
h = append(h, argsKV{
value: []byte{},
})
}
return h, &h[n]
}
Expand Down
26 changes: 25 additions & 1 deletion args_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -596,4 +596,28 @@ func TestArgsDeleteAll(t *testing.T) {
if a.Len() != 1 || a.Has("q1") {
t.Fatalf("Expected q1 arg to be completely deleted. Current Args: %s", a.String())
}
}
}

func TestIssue932(t *testing.T) {
t.Parallel()
var a []argsKV

a = setArg(a, "t1", "ok", argsHasValue)
a = setArg(a, "t2", "", argsHasValue)
a = setArg(a, "t1", "", argsHasValue)
a = setArgBytes(a, s2b("t3"), []byte{}, argsHasValue)
a = setArgBytes(a, s2b("t4"), nil, argsHasValue)

if peekArgStr(a, "t1") == nil {
t.Error("nil not expected for t1")
}
if peekArgStr(a, "t2") == nil {
t.Error("nil not expected for t2")
}
if peekArgStr(a, "t3") == nil {
t.Error("nil not expected for t3")
}
if peekArgStr(a, "t4") != nil {
t.Error("nil expected for t4")
}
}

0 comments on commit 4e63057

Please sign in to comment.