From 1a8ba0918f81f04c27a49e0f8bf80c4145efcbed Mon Sep 17 00:00:00 2001 From: Cole Brown Date: Thu, 29 Nov 2018 17:59:38 -0500 Subject: [PATCH 1/3] Fix bug in VarintSize :100644 100644 79ebe2f b0ced77 M varint.go --- varint.go | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/varint.go b/varint.go index 79ebe2f..0b8bf11 100644 --- a/varint.go +++ b/varint.go @@ -8,7 +8,13 @@ import ( // VarintSize returns the size (in bytes) of `num` encoded as a varint. func VarintSize(num int) int { - return bits.Len(uint(num))/7 + 1 + bits := bits.Len(uint(num)) + q, r := bits/7, bits%7 + size := q + if r > 0 { + size++ + } + return size } // CodeToVarint converts an integer to a varint-encoded []byte From d462e342b73aed8a8737a9ef17bd78a21cd639a9 Mon Sep 17 00:00:00 2001 From: Cole Brown Date: Thu, 29 Nov 2018 18:58:36 -0500 Subject: [PATCH 2/3] Fix yet another bug in VarintSize, add test --- varint.go | 4 ++-- varint_test.go | 16 ++++++++++++++++ 2 files changed, 18 insertions(+), 2 deletions(-) create mode 100644 varint_test.go diff --git a/varint.go b/varint.go index 0b8bf11..b8b1507 100644 --- a/varint.go +++ b/varint.go @@ -11,7 +11,7 @@ func VarintSize(num int) int { bits := bits.Len(uint(num)) q, r := bits/7, bits%7 size := q - if r > 0 { + if r > 0 || size == 0 { size++ } return size @@ -19,7 +19,7 @@ func VarintSize(num int) int { // CodeToVarint converts an integer to a varint-encoded []byte func CodeToVarint(num int) []byte { - buf := make([]byte, bits.Len(uint(num))/7+1) + buf := make([]byte, VarintSize(num)) n := binary.PutUvarint(buf, uint64(num)) return buf[:n] } diff --git a/varint_test.go b/varint_test.go new file mode 100644 index 0000000..db0b167 --- /dev/null +++ b/varint_test.go @@ -0,0 +1,16 @@ +package multiaddr + +import "testing" + +func expectVarint(t *testing.T, x, expected int) { + size := VarintSize(x) + if size != expected { + t.Fatalf("expected varintsize of %d to be %d, got %d", x, expected, size) + } +} + +func TestVarintSize(t *testing.T) { + expectVarint(t, (1<<7)-1, 1) + expectVarint(t, 0, 1) + expectVarint(t, 1<<7, 2) +} From 11c75668d38eb97a842276707a65cf7dcdb99cac Mon Sep 17 00:00:00 2001 From: Steven Allen Date: Thu, 29 Nov 2018 16:08:46 -0800 Subject: [PATCH 3/3] test: test all varints less than 2**16 againt VarintSize --- varint_test.go | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/varint_test.go b/varint_test.go index db0b167..817b3dd 100644 --- a/varint_test.go +++ b/varint_test.go @@ -1,8 +1,14 @@ package multiaddr -import "testing" +import ( + "encoding/binary" + "testing" +) + +func checkVarint(t *testing.T, x int) { + buf := make([]byte, binary.MaxVarintLen64) + expected := binary.PutUvarint(buf, uint64(x)) -func expectVarint(t *testing.T, x, expected int) { size := VarintSize(x) if size != expected { t.Fatalf("expected varintsize of %d to be %d, got %d", x, expected, size) @@ -10,7 +16,8 @@ func expectVarint(t *testing.T, x, expected int) { } func TestVarintSize(t *testing.T) { - expectVarint(t, (1<<7)-1, 1) - expectVarint(t, 0, 1) - expectVarint(t, 1<<7, 2) + max := 1 << 16 + for x := 0; x < max; x++ { + checkVarint(t, x) + } }