Skip to content

Commit

Permalink
Merge pull request #67728 from cockroachdb/blathers/backport-release-…
Browse files Browse the repository at this point in the history
…21.1-67707

release-21.1: kv: fix aliasing bug in leaseHistory.get
  • Loading branch information
nvanbenschoten authored Aug 3, 2021
2 parents f395e17 + 9cd39d3 commit 391ec3d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/kv/kvserver/lease_history.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ func (lh *leaseHistory) get() []roachpb.Lease {
if len(lh.history) < leaseHistoryMaxEntries || lh.index == 0 {
result := make([]roachpb.Lease, len(lh.history))
copy(result, lh.history)
return lh.history
return result
}
first := lh.history[lh.index:]
second := lh.history[:lh.index]
Expand Down
5 changes: 5 additions & 0 deletions pkg/kv/kvserver/lease_history_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/leaktest"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/stretchr/testify/require"
)

func TestLeaseHistory(t *testing.T) {
Expand All @@ -35,6 +36,9 @@ func TestLeaseHistory(t *testing.T) {
if e, a := int64(i-1), leases[len(leases)-1].Epoch; e != a {
t.Errorf("%d: expected newest lease to have epoch of %d , actual %d:\n%+v", i, e, a, leases)
}
require.NotSame(t, &history.history[0], &leases[0], "expected slice copy")
} else {
require.Nil(t, leases)
}

history.add(roachpb.Lease{
Expand All @@ -54,6 +58,7 @@ func TestLeaseHistory(t *testing.T) {
if e, a := int64(i+leaseHistoryMaxEntries-1), leases[leaseHistoryMaxEntries-1].Epoch; e != a {
t.Errorf("%d: expected newest lease to have epoch of %d , actual %d:\n%+v", i, e, a, leases)
}
require.NotSame(t, &history.history[0], &leases[0], "expected slice copy")

history.add(roachpb.Lease{
Epoch: int64(i + leaseHistoryMaxEntries),
Expand Down

0 comments on commit 391ec3d

Please sign in to comment.