From 84afef20f5d960b033c9c8d84710331e6cacec70 Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Sun, 28 Jan 2018 10:35:34 -0700 Subject: [PATCH 1/2] common: fix BitArray.Update to avoid nil dereference Update previously only checked that the receiver was non-nil but didn't check that the input parameter to update "o" was non-nil causing a nil dereference in cases such as https://github.com/tendermint/tendermint/blob/fe632ea32a89c3d9804bbd6e3ce9391b1d5a0993/consensus/reactor.go#L306 Fixes https://github.com/tendermint/tendermint/issues/1169 --- common/bit_array.go | 2 +- common/bit_array_test.go | 23 +++++++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/common/bit_array.go b/common/bit_array.go index 848763b4..68201bad 100644 --- a/common/bit_array.go +++ b/common/bit_array.go @@ -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() diff --git a/common/bit_array_test.go b/common/bit_array_test.go index 1c72882c..9a787e44 100644 --- a/common/bit_array_test.go +++ b/common/bit_array_test.go @@ -164,3 +164,26 @@ 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(0), NewBitArray(10)}, + {nil, NewBitArray(10)}, + {nil, newRandBitArray(64)}, + {newRandBitArray(63), newRandBitArray(64)}, + } + + for _, pair := range pairs { + a, b := pair.a, pair.b + a.Update(b) + b.Update(a) + } +} From 85be26c675b05a2a75c856f7c22b446d8df1c944 Mon Sep 17 00:00:00 2001 From: Emmanuel Odeke Date: Sun, 28 Jan 2018 22:02:46 -0700 Subject: [PATCH 2/2] common: BitArray: feedback from @adrianbrink to simplify tests --- common/bit_array_test.go | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/common/bit_array_test.go b/common/bit_array_test.go index 9a787e44..e4ac8bf6 100644 --- a/common/bit_array_test.go +++ b/common/bit_array_test.go @@ -175,10 +175,9 @@ func TestUpdateNeverPanics(t *testing.T) { }{ {nil, nil}, {newRandBitArray(10), newRandBitArray(12)}, - {newRandBitArray(0), NewBitArray(10)}, + {newRandBitArray(23), newRandBitArray(23)}, + {newRandBitArray(37), nil}, {nil, NewBitArray(10)}, - {nil, newRandBitArray(64)}, - {newRandBitArray(63), newRandBitArray(64)}, } for _, pair := range pairs {