Merge pull request #136 from tendermint/fix-bitArray-nil-update

common: fix BitArray.Update to avoid nil dereference
This commit is contained in:
Ethan Buchman 2018-01-29 17:17:18 -05:00 committed by GitHub
commit 13f009bf68
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 1 deletions

View File

@ -306,7 +306,7 @@ func (bA *BitArray) Bytes() []byte {
// so if necessary, caller must copy or lock o prior to calling Update.
// If bA is nil, does nothing.
func (bA *BitArray) Update(o *BitArray) {
if bA == nil {
if bA == nil || o == nil {
return
}
bA.mtx.Lock()

View File

@ -164,3 +164,25 @@ func TestEmptyFull(t *testing.T) {
}
}
}
func TestUpdateNeverPanics(t *testing.T) {
newRandBitArray := func(n int) *BitArray {
ba, _ := randBitArray(n)
return ba
}
pairs := []struct {
a, b *BitArray
}{
{nil, nil},
{newRandBitArray(10), newRandBitArray(12)},
{newRandBitArray(23), newRandBitArray(23)},
{newRandBitArray(37), nil},
{nil, NewBitArray(10)},
}
for _, pair := range pairs {
a, b := pair.a, pair.b
a.Update(b)
b.Update(a)
}
}