Skip to content

Commit

Permalink
kversion: fix overflow panic for SetMaxKeyVersion
Browse files Browse the repository at this point in the history
  • Loading branch information
twmb committed Feb 20, 2021
1 parent 4aa4187 commit 8a42602
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/kversion/kversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ func (vs *Versions) SetMaxKeyVersion(k, v int16) {
if k < 0 || v < 0 && int(k) >= len(vs.k2v)+1 {
return
}
needLen := int(k + 1)
needLen := int(k) + 1
for len(vs.k2v) < needLen {
vs.k2v = append(vs.k2v, -1)
}
Expand Down
13 changes: 13 additions & 0 deletions pkg/kversion/kversion_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,22 @@
package kversion

import (
"math"
"testing"
)

func TestSetMaxKeyVersion(t *testing.T) {
var vs Versions
for i := int16(0); i < math.MaxInt16; i++ {
vs.SetMaxKeyVersion(i, i)
}
for i, v := range vs.k2v {
if int16(i) != v {
t.Errorf("set incorrect: at %d got %d != exp %d", i, v, i)
}
}
}

func TestVersionGuess(t *testing.T) {
// Cases where last can be empty.
{
Expand Down

0 comments on commit 8a42602

Please sign in to comment.