From 05245586967e3064fffcecc75d4035e4249d792e Mon Sep 17 00:00:00 2001 From: lklimek <842586+lklimek@users.noreply.github.com> Date: Tue, 12 Oct 2021 22:22:57 +0200 Subject: [PATCH] refactor: assignment copies lock value (#7108) Co-authored-by: M. J. Fromberger --- CHANGELOG_PENDING.md | 2 ++ libs/bits/bit_array.go | 30 ++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/CHANGELOG_PENDING.md b/CHANGELOG_PENDING.md index fafb9cea7..0f8ba5ea7 100644 --- a/CHANGELOG_PENDING.md +++ b/CHANGELOG_PENDING.md @@ -36,3 +36,5 @@ Special thanks to external contributors on this release: ### IMPROVEMENTS ### BUG FIXES + +- fix: assignment copies lock value in `BitArray.UnmarshalJSON()` (@lklimek) diff --git a/libs/bits/bit_array.go b/libs/bits/bit_array.go index b78fafddd..a0258521c 100644 --- a/libs/bits/bit_array.go +++ b/libs/bits/bit_array.go @@ -30,9 +30,21 @@ func NewBitArray(bits int) *BitArray { if bits <= 0 { return nil } - return &BitArray{ - Bits: bits, - Elems: make([]uint64, numElems(bits)), + bA := &BitArray{} + bA.reset(bits) + return bA +} + +// reset changes size of BitArray to `bits` and re-allocates (zeroed) data buffer +func (bA *BitArray) reset(bits int) { + bA.mtx.Lock() + defer bA.mtx.Unlock() + + bA.Bits = bits + if bits == 0 { + bA.Elems = nil + } else { + bA.Elems = make([]uint64, numElems(bits)) } } @@ -399,8 +411,7 @@ func (bA *BitArray) UnmarshalJSON(bz []byte) error { if b == "null" { // This is required e.g. for encoding/json when decoding // into a pointer with pre-allocated BitArray. - bA.Bits = 0 - bA.Elems = nil + bA.reset(0) return nil } @@ -410,16 +421,15 @@ func (bA *BitArray) UnmarshalJSON(bz []byte) error { return fmt.Errorf("bitArray in JSON should be a string of format %q but got %s", bitArrayJSONRegexp.String(), b) } bits := match[1] - - // Construct new BitArray and copy over. numBits := len(bits) - bA2 := NewBitArray(numBits) + + bA.reset(numBits) for i := 0; i < numBits; i++ { if bits[i] == 'x' { - bA2.SetIndex(i, true) + bA.SetIndex(i, true) } } - *bA = *bA2 //nolint:govet + return nil }