Skip to content

Commit

Permalink
Merge pull request #12 from satta/broken-crash
Browse files Browse the repository at this point in the history
improve robustness to broken input files
  • Loading branch information
Robert Haist committed Jan 10, 2019
2 parents a96d8e8 + 8a53c5c commit 691ea61
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
language: go
sudo: false
before_script:
- go get ./...
- go get -t ./...
- go vet ./...
script: make release
script: make release test
deploy:
provider: releases
api_key:
Expand Down
5 changes: 4 additions & 1 deletion bloom.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,10 @@ func (s *BloomFilter) Read(input io.Reader) error {
}

s.k = binary.LittleEndian.Uint64(bs8)
maxInt := uint64(int(^uint(0) >> 1))
if s.k >= maxInt {
return fmt.Errorf("value of k (number of hash functions) is too high (%d), must be less than maximum int (%d)", s.k, maxInt)
}

if _, err := io.ReadFull(input, bs8); err != nil {
return err
Expand Down Expand Up @@ -189,7 +193,6 @@ const g uint64 = 18446744073709550147
// Fingerprint returns the fingerprint of a given value, as an array of index
// values.
func (s *BloomFilter) Fingerprint(value []byte, fingerprint []uint64) {

hv := fnv.New64()
hv.Write(value)
hn := hv.Sum64() % m
Expand Down
17 changes: 17 additions & 0 deletions io_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ package bloom
import (
"io/ioutil"
"os"
"regexp"
"testing"
)

Expand Down Expand Up @@ -36,6 +37,22 @@ func TestFromReaderFile(t *testing.T) {
checkResults(t, bf)
}

func TestFromReaderCorruptFile(t *testing.T) {
f, err := os.Open("testdata/broken.bloom")
if err != nil {
t.Fatal(err)
}
defer f.Close()
_, err = LoadFromReader(f, false)
if err == nil {
t.Fatal("error expected")
}
r, _ := regexp.Compile("is too high")
if !r.MatchString(err.Error()) {
t.Fatalf("wrong error message: %s", err.Error())
}
}

func testFromSerialized(t *testing.T, gzip bool) {
bf := Initialize(100, 0.0001)
for _, v := range []string{"foo", "bar", "baz"} {
Expand Down
Binary file added testdata/broken.bloom
Binary file not shown.

0 comments on commit 691ea61

Please sign in to comment.