Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions memuvarint.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ package zap

import (
"fmt"
"io"
)

type memUvarintReader struct {
Expand Down Expand Up @@ -50,6 +51,10 @@ func (r *memUvarintReader) ReadUvarint() (uint64, error) {
var S = r.S

for {
if C >= len(S) {
r.C = C
return 0, io.ErrUnexpectedEOF
}
b := S[C]
C++

Expand Down
15 changes: 15 additions & 0 deletions memuvarint_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,25 @@ package zap
import (
"bytes"
"encoding/binary"
"errors"
"io"
"math"
"testing"
)

func TestMemUvarintReaderReturnsUnexpectedEOFForTruncatedValue(t *testing.T) {
for _, input := range [][]byte{{0x80}, {0x80, 0x80}, {0xff, 0xff, 0xff}} {
reader := newMemUvarintReader(input)
_, err := reader.ReadUvarint()
if !errors.Is(err, io.ErrUnexpectedEOF) {
t.Fatalf("ReadUvarint(%x) error = %v, want %v", input, err, io.ErrUnexpectedEOF)
}
if reader.C != len(input) {
t.Fatalf("ReadUvarint(%x) consumed %d bytes, want %d", input, reader.C, len(input))
}
}
}

func BenchmarkUvarint(b *testing.B) {
n, buf := generateCommonUvarints(64, 512)

Expand Down