Skip to content

Commit

Permalink
Merge pull request from GHSA-2h6c-j3gf-xp9r
Browse files Browse the repository at this point in the history
refactor: return errors instead of panics
  • Loading branch information
Jorropo authored Feb 9, 2023
2 parents f7bb4e7 + 87b0717 commit 345bb29
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 24 deletions.
23 changes: 14 additions & 9 deletions bitfield.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,33 @@ package bitfield
// NOTE: Don't bother replacing the divisions/modulo with shifts/ands, go is smart.

import (
"fmt"
"math/bits"
)

// NewBitfield creates a new fixed-sized Bitfield (allocated up-front).
//
// Panics if size is not a multiple of 8.
func NewBitfield(size int) Bitfield {
func NewBitfield(size int) (Bitfield, error) {
if size < 0 {
return nil, fmt.Errorf("bitfield size must be positive; got %d", size)
}
if size%8 != 0 {
panic("Bitfield size must be a multiple of 8")
return nil, fmt.Errorf("bitfield size must be a multiple of 8; got %d", size)
}
return make([]byte, size/8)
return make([]byte, size/8), nil
}

// FromBytes constructs a new bitfield from a serialized bitfield.
func FromBytes(size int, bits []byte) Bitfield {
bf := NewBitfield(size)
func FromBytes(size int, bits []byte) (Bitfield, error) {
bf, err := NewBitfield(size)
if err != nil {
return nil, err
}
start := len(bf) - len(bits)
if start < 0 {
panic("bitfield too small")
return nil, fmt.Errorf("bitfield too small: got %d; need %d", size, len(bits)*8)
}
copy(bf[start:], bits)
return bf
return bf, nil
}

func (bf Bitfield) offset(i int) (uint, uint8) {
Expand Down
58 changes: 44 additions & 14 deletions bitfield_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,8 @@ import (
)

func TestExhaustive24(t *testing.T) {
bf := NewBitfield(24)
bf, err := NewBitfield(24)
assertNoError(t, err)
max := 1 << 24

bint := new(big.Int)
Expand Down Expand Up @@ -58,7 +59,8 @@ func TestExhaustive24(t *testing.T) {
}

func TestBitfield(t *testing.T) {
bf := NewBitfield(128)
bf, err := NewBitfield(128)
assertNoError(t, err)
if bf.OnesBefore(20) != 0 {
t.Fatal("expected no bits set")
}
Expand Down Expand Up @@ -91,10 +93,20 @@ func TestBitfield(t *testing.T) {
}
}

func TestBadSizeFails(t *testing.T) {
for _, size := range [...]int{-8, 2, 1337, -3} {
_, err := NewBitfield(size)
if err == nil {
t.Fatalf("missing error for %d sized bitfield", size)
}
}
}

var benchmarkSize = 512

func BenchmarkBitfield(t *testing.B) {
bf := NewBitfield(benchmarkSize)
bf, err := NewBitfield(benchmarkSize)
assertNoError(t, err)
t.ResetTimer()
for i := 0; i < t.N; i++ {
if bf.Bit(i % benchmarkSize) {
Expand Down Expand Up @@ -123,13 +135,14 @@ func BenchmarkBitfield(t *testing.B) {
}
}

func BenchmarkOnes(t *testing.B) {
bf := NewBitfield(benchmarkSize)
t.ResetTimer()
for i := 0; i < t.N; i++ {
func BenchmarkOnes(b *testing.B) {
bf, err := NewBitfield(benchmarkSize)
assertNoError(b, err)
b.ResetTimer()
for i := 0; i < b.N; i++ {
for j := 0; j*4 < benchmarkSize; j++ {
if bf.Ones() != j {
t.Fatal("bad", i)
b.Fatal("bad", i)
}
bf.SetBit(j * 4)
}
Expand All @@ -139,14 +152,16 @@ func BenchmarkOnes(t *testing.B) {
}
}

func BenchmarkBytes(t *testing.B) {
bfa := NewBitfield(211)
bfb := NewBitfield(211)
for j := 0; j*4 < 211; j++ {
func BenchmarkBytes(b *testing.B) {
bfa, err := NewBitfield(216)
assertNoError(b, err)
bfb, err := NewBitfield(216)
assertNoError(b, err)
for j := 0; j*4 < 216; j++ {
bfa.SetBit(j * 4)
}
t.ResetTimer()
for i := 0; i < t.N; i++ {
b.ResetTimer()
for i := 0; i < b.N; i++ {
bfb.SetBytes(bfa.Bytes())
}
}
Expand Down Expand Up @@ -180,3 +195,18 @@ func BenchmarkBigInt(t *testing.B) {
}
}
}

func FuzzFromBytes(f *testing.F) {
f.Fuzz(func(_ *testing.T, size int, bytes []byte) {
if size > 1<<20 { // We relly on consumers for limit checks, hopefully they understand that a New... factory allocates memory.
return
}
FromBytes(size, bytes)
})
}

func assertNoError(t testing.TB, e error) {
if e != nil {
t.Fatal(e)
}
}
2 changes: 1 addition & 1 deletion version.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
{
"version": "v1.0.0"
"version": "v1.1.0"
}

0 comments on commit 345bb29

Please sign in to comment.