bump tinylib/msgp v1.1.0

full diff: 3b556c6454...v1.1.0

Signed-off-by: Sebastiaan van Stijn <github@gone.nl>
This commit is contained in:
Sebastiaan van Stijn 2019-04-15 02:15:25 +02:00
parent 688e67e1d3
commit 048bd8d179
No known key found for this signature in database
GPG Key ID: 76698F39D527CE8C
9 changed files with 410 additions and 35 deletions

View File

@ -97,7 +97,7 @@ github.com/Graylog2/go-gelf 4143646226541087117ff2f83334
# fluent-logger-golang deps
github.com/fluent/fluent-logger-golang 7a6c9dcd7f14c2ed5d8c55c11b894e5455ee311b # v1.4.0
github.com/philhofer/fwd bb6d471dc95d4fe11e432687f8b70ff496cf3136 # v1.0.0
github.com/tinylib/msgp 3b556c64540842d4f82967be066a7f7fffc3adad
github.com/tinylib/msgp af6442a0fcf6e2a1b824f70dd0c734f01e817751 # v1.1.0
# fsnotify
github.com/fsnotify/fsnotify 1485a34d5d5723fea214f5710708e19a831720e4 # v1.4.7-11-g1485a34

View File

@ -198,6 +198,7 @@ func resizeMap(raw []byte, delta int64) []byte {
if cap(raw)-len(raw) >= 2 {
raw = raw[0 : len(raw)+2]
copy(raw[5:], raw[3:])
raw[0] = mmap32
big.PutUint32(raw[1:], uint32(sz+delta))
return raw
}

View File

@ -5,6 +5,8 @@ import (
"reflect"
)
const resumableDefault = false
var (
// ErrShortBytes is returned when the
// slice being decoded is too short to
@ -26,84 +28,240 @@ type Error interface {
// Resumable returns whether
// or not the error means that
// the stream of data is malformed
// and the information is unrecoverable.
// and the information is unrecoverable.
Resumable() bool
}
// contextError allows msgp Error instances to be enhanced with additional
// context about their origin.
type contextError interface {
Error
// withContext must not modify the error instance - it must clone and
// return a new error with the context added.
withContext(ctx string) error
}
// Cause returns the underlying cause of an error that has been wrapped
// with additional context.
func Cause(e error) error {
out := e
if e, ok := e.(errWrapped); ok && e.cause != nil {
out = e.cause
}
return out
}
// Resumable returns whether or not the error means that the stream of data is
// malformed and the information is unrecoverable.
func Resumable(e error) bool {
if e, ok := e.(Error); ok {
return e.Resumable()
}
return resumableDefault
}
// WrapError wraps an error with additional context that allows the part of the
// serialized type that caused the problem to be identified. Underlying errors
// can be retrieved using Cause()
//
// The input error is not modified - a new error should be returned.
//
// ErrShortBytes is not wrapped with any context due to backward compatibility
// issues with the public API.
//
func WrapError(err error, ctx ...interface{}) error {
switch e := err.(type) {
case errShort:
return e
case contextError:
return e.withContext(ctxString(ctx))
default:
return errWrapped{cause: err, ctx: ctxString(ctx)}
}
}
// ctxString converts the incoming interface{} slice into a single string.
func ctxString(ctx []interface{}) string {
out := ""
for idx, cv := range ctx {
if idx > 0 {
out += "/"
}
out += fmt.Sprintf("%v", cv)
}
return out
}
func addCtx(ctx, add string) string {
if ctx != "" {
return add + "/" + ctx
} else {
return add
}
}
// errWrapped allows arbitrary errors passed to WrapError to be enhanced with
// context and unwrapped with Cause()
type errWrapped struct {
cause error
ctx string
}
func (e errWrapped) Error() string {
if e.ctx != "" {
return fmt.Sprintf("%s at %s", e.cause, e.ctx)
} else {
return e.cause.Error()
}
}
func (e errWrapped) Resumable() bool {
if e, ok := e.cause.(Error); ok {
return e.Resumable()
}
return resumableDefault
}
type errShort struct{}
func (e errShort) Error() string { return "msgp: too few bytes left to read object" }
func (e errShort) Resumable() bool { return false }
type errFatal struct{}
type errFatal struct {
ctx string
}
func (f errFatal) Error() string {
out := "msgp: fatal decoding error (unreachable code)"
if f.ctx != "" {
out += " at " + f.ctx
}
return out
}
func (f errFatal) Error() string { return "msgp: fatal decoding error (unreachable code)" }
func (f errFatal) Resumable() bool { return false }
func (f errFatal) withContext(ctx string) error { f.ctx = addCtx(f.ctx, ctx); return f }
// ArrayError is an error returned
// when decoding a fix-sized array
// of the wrong size
type ArrayError struct {
Wanted uint32
Got uint32
ctx string
}
// Error implements the error interface
func (a ArrayError) Error() string {
return fmt.Sprintf("msgp: wanted array of size %d; got %d", a.Wanted, a.Got)
out := fmt.Sprintf("msgp: wanted array of size %d; got %d", a.Wanted, a.Got)
if a.ctx != "" {
out += " at " + a.ctx
}
return out
}
// Resumable is always 'true' for ArrayErrors
func (a ArrayError) Resumable() bool { return true }
func (a ArrayError) withContext(ctx string) error { a.ctx = addCtx(a.ctx, ctx); return a }
// IntOverflow is returned when a call
// would downcast an integer to a type
// with too few bits to hold its value.
type IntOverflow struct {
Value int64 // the value of the integer
FailedBitsize int // the bit size that the int64 could not fit into
ctx string
}
// Error implements the error interface
func (i IntOverflow) Error() string {
return fmt.Sprintf("msgp: %d overflows int%d", i.Value, i.FailedBitsize)
str := fmt.Sprintf("msgp: %d overflows int%d", i.Value, i.FailedBitsize)
if i.ctx != "" {
str += " at " + i.ctx
}
return str
}
// Resumable is always 'true' for overflows
func (i IntOverflow) Resumable() bool { return true }
func (i IntOverflow) withContext(ctx string) error { i.ctx = addCtx(i.ctx, ctx); return i }
// UintOverflow is returned when a call
// would downcast an unsigned integer to a type
// with too few bits to hold its value
type UintOverflow struct {
Value uint64 // value of the uint
FailedBitsize int // the bit size that couldn't fit the value
ctx string
}
// Error implements the error interface
func (u UintOverflow) Error() string {
return fmt.Sprintf("msgp: %d overflows uint%d", u.Value, u.FailedBitsize)
str := fmt.Sprintf("msgp: %d overflows uint%d", u.Value, u.FailedBitsize)
if u.ctx != "" {
str += " at " + u.ctx
}
return str
}
// Resumable is always 'true' for overflows
func (u UintOverflow) Resumable() bool { return true }
func (u UintOverflow) withContext(ctx string) error { u.ctx = addCtx(u.ctx, ctx); return u }
// UintBelowZero is returned when a call
// would cast a signed integer below zero
// to an unsigned integer.
type UintBelowZero struct {
Value int64 // value of the incoming int
ctx string
}
// Error implements the error interface
func (u UintBelowZero) Error() string {
str := fmt.Sprintf("msgp: attempted to cast int %d to unsigned", u.Value)
if u.ctx != "" {
str += " at " + u.ctx
}
return str
}
// Resumable is always 'true' for overflows
func (u UintBelowZero) Resumable() bool { return true }
func (u UintBelowZero) withContext(ctx string) error {
u.ctx = ctx
return u
}
// A TypeError is returned when a particular
// decoding method is unsuitable for decoding
// a particular MessagePack value.
type TypeError struct {
Method Type // Type expected by method
Encoded Type // Type actually encoded
ctx string
}
// Error implements the error interface
func (t TypeError) Error() string {
return fmt.Sprintf("msgp: attempted to decode type %q with method for %q", t.Encoded, t.Method)
out := fmt.Sprintf("msgp: attempted to decode type %q with method for %q", t.Encoded, t.Method)
if t.ctx != "" {
out += " at " + t.ctx
}
return out
}
// Resumable returns 'true' for TypeErrors
func (t TypeError) Resumable() bool { return true }
func (t TypeError) withContext(ctx string) error { t.ctx = addCtx(t.ctx, ctx); return t }
// returns either InvalidPrefixError or
// TypeError depending on whether or not
// the prefix is recognized
@ -133,10 +291,24 @@ func (i InvalidPrefixError) Resumable() bool { return false }
// to a function that takes `interface{}`.
type ErrUnsupportedType struct {
T reflect.Type
ctx string
}
// Error implements error
func (e *ErrUnsupportedType) Error() string { return fmt.Sprintf("msgp: type %q not supported", e.T) }
func (e *ErrUnsupportedType) Error() string {
out := fmt.Sprintf("msgp: type %q not supported", e.T)
if e.ctx != "" {
out += " at " + e.ctx
}
return out
}
// Resumable returns 'true' for ErrUnsupportedType
func (e *ErrUnsupportedType) Resumable() bool { return true }
func (e *ErrUnsupportedType) withContext(ctx string) error {
o := *e
o.ctx = addCtx(o.ctx, ctx)
return &o
}

View File

@ -445,26 +445,27 @@ func AppendExtension(b []byte, e Extension) ([]byte, error) {
o[n] = mfixext16
o[n+1] = byte(e.ExtensionType())
n += 2
}
switch {
case l < math.MaxUint8:
o, n = ensure(b, l+3)
o[n] = mext8
o[n+1] = byte(uint8(l))
o[n+2] = byte(e.ExtensionType())
n += 3
case l < math.MaxUint16:
o, n = ensure(b, l+4)
o[n] = mext16
big.PutUint16(o[n+1:], uint16(l))
o[n+3] = byte(e.ExtensionType())
n += 4
default:
o, n = ensure(b, l+6)
o[n] = mext32
big.PutUint32(o[n+1:], uint32(l))
o[n+5] = byte(e.ExtensionType())
n += 6
switch {
case l < math.MaxUint8:
o, n = ensure(b, l+3)
o[n] = mext8
o[n+1] = byte(uint8(l))
o[n+2] = byte(e.ExtensionType())
n += 3
case l < math.MaxUint16:
o, n = ensure(b, l+4)
o[n] = mext16
big.PutUint16(o[n+1:], uint16(l))
o[n+3] = byte(e.ExtensionType())
n += 4
default:
o, n = ensure(b, l+6)
o[n] = mext32
big.PutUint32(o[n+1:], uint32(l))
o[n+5] = byte(e.ExtensionType())
n += 6
}
}
return o, e.MarshalBinaryTo(o[n:])
}

View File

@ -1,4 +1,4 @@
// +build appengine
// +build purego appengine
package msgp

View File

@ -583,6 +583,14 @@ func (m *Reader) ReadInt64() (i int64, err error) {
i = int64(getMint8(p))
return
case muint8:
p, err = m.R.Next(2)
if err != nil {
return
}
i = int64(getMuint8(p))
return
case mint16:
p, err = m.R.Next(3)
if err != nil {
@ -591,6 +599,14 @@ func (m *Reader) ReadInt64() (i int64, err error) {
i = int64(getMint16(p))
return
case muint16:
p, err = m.R.Next(3)
if err != nil {
return
}
i = int64(getMuint16(p))
return
case mint32:
p, err = m.R.Next(5)
if err != nil {
@ -599,6 +615,14 @@ func (m *Reader) ReadInt64() (i int64, err error) {
i = int64(getMint32(p))
return
case muint32:
p, err = m.R.Next(5)
if err != nil {
return
}
i = int64(getMuint32(p))
return
case mint64:
p, err = m.R.Next(9)
if err != nil {
@ -607,6 +631,19 @@ func (m *Reader) ReadInt64() (i int64, err error) {
i = getMint64(p)
return
case muint64:
p, err = m.R.Next(9)
if err != nil {
return
}
u := getMuint64(p)
if u > math.MaxInt64 {
err = UintOverflow{Value: u, FailedBitsize: 64}
return
}
i = int64(u)
return
default:
err = badPrefix(IntType, lead)
return
@ -678,6 +715,19 @@ func (m *Reader) ReadUint64() (u uint64, err error) {
return
}
switch lead {
case mint8:
p, err = m.R.Next(2)
if err != nil {
return
}
v := int64(getMint8(p))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
return
case muint8:
p, err = m.R.Next(2)
if err != nil {
@ -686,6 +736,19 @@ func (m *Reader) ReadUint64() (u uint64, err error) {
u = uint64(getMuint8(p))
return
case mint16:
p, err = m.R.Next(3)
if err != nil {
return
}
v := int64(getMint16(p))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
return
case muint16:
p, err = m.R.Next(3)
if err != nil {
@ -694,6 +757,19 @@ func (m *Reader) ReadUint64() (u uint64, err error) {
u = uint64(getMuint16(p))
return
case mint32:
p, err = m.R.Next(5)
if err != nil {
return
}
v := int64(getMint32(p))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
return
case muint32:
p, err = m.R.Next(5)
if err != nil {
@ -702,6 +778,19 @@ func (m *Reader) ReadUint64() (u uint64, err error) {
u = uint64(getMuint32(p))
return
case mint64:
p, err = m.R.Next(9)
if err != nil {
return
}
v := int64(getMint64(p))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
return
case muint64:
p, err = m.R.Next(9)
if err != nil {
@ -711,7 +800,11 @@ func (m *Reader) ReadUint64() (u uint64, err error) {
return
default:
err = badPrefix(UintType, lead)
if isnfixint(lead) {
err = UintBelowZero{Value: int64(rnfixint(lead))}
} else {
err = badPrefix(UintType, lead)
}
return
}

View File

@ -79,6 +79,9 @@ func (r *Raw) UnmarshalMsg(b []byte) ([]byte, error) {
return b, err
}
rlen := l - len(out)
if IsNil(b[:rlen]) {
rlen = 0
}
if cap(*r) < rlen {
*r = make(Raw, rlen)
} else {
@ -104,7 +107,11 @@ func (r Raw) EncodeMsg(w *Writer) error {
// next object on the wire.
func (r *Raw) DecodeMsg(f *Reader) error {
*r = (*r)[:0]
return appendNext(f, (*[]byte)(r))
err := appendNext(f, (*[]byte)(r))
if IsNil(*r) {
*r = (*r)[:0]
}
return err
}
// Msgsize implements msgp.Sizer
@ -368,6 +375,15 @@ func ReadInt64Bytes(b []byte) (i int64, o []byte, err error) {
o = b[2:]
return
case muint8:
if l < 2 {
err = ErrShortBytes
return
}
i = int64(getMuint8(b))
o = b[2:]
return
case mint16:
if l < 3 {
err = ErrShortBytes
@ -377,6 +393,15 @@ func ReadInt64Bytes(b []byte) (i int64, o []byte, err error) {
o = b[3:]
return
case muint16:
if l < 3 {
err = ErrShortBytes
return
}
i = int64(getMuint16(b))
o = b[3:]
return
case mint32:
if l < 5 {
err = ErrShortBytes
@ -386,12 +411,35 @@ func ReadInt64Bytes(b []byte) (i int64, o []byte, err error) {
o = b[5:]
return
case muint32:
if l < 5 {
err = ErrShortBytes
return
}
i = int64(getMuint32(b))
o = b[5:]
return
case mint64:
if l < 9 {
err = ErrShortBytes
return
}
i = getMint64(b)
i = int64(getMint64(b))
o = b[9:]
return
case muint64:
if l < 9 {
err = ErrShortBytes
return
}
u := getMuint64(b)
if u > math.MaxInt64 {
err = UintOverflow{Value: u, FailedBitsize: 64}
return
}
i = int64(u)
o = b[9:]
return
@ -477,6 +525,20 @@ func ReadUint64Bytes(b []byte) (u uint64, o []byte, err error) {
}
switch lead {
case mint8:
if l < 2 {
err = ErrShortBytes
return
}
v := int64(getMint8(b))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
o = b[2:]
return
case muint8:
if l < 2 {
err = ErrShortBytes
@ -486,6 +548,20 @@ func ReadUint64Bytes(b []byte) (u uint64, o []byte, err error) {
o = b[2:]
return
case mint16:
if l < 3 {
err = ErrShortBytes
return
}
v := int64(getMint16(b))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
o = b[3:]
return
case muint16:
if l < 3 {
err = ErrShortBytes
@ -495,6 +571,20 @@ func ReadUint64Bytes(b []byte) (u uint64, o []byte, err error) {
o = b[3:]
return
case mint32:
if l < 5 {
err = ErrShortBytes
return
}
v := int64(getMint32(b))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
o = b[5:]
return
case muint32:
if l < 5 {
err = ErrShortBytes
@ -504,6 +594,20 @@ func ReadUint64Bytes(b []byte) (u uint64, o []byte, err error) {
o = b[5:]
return
case mint64:
if l < 9 {
err = ErrShortBytes
return
}
v := int64(getMint64(b))
if v < 0 {
err = UintBelowZero{Value: v}
return
}
u = uint64(v)
o = b[9:]
return
case muint64:
if l < 9 {
err = ErrShortBytes
@ -514,7 +618,11 @@ func ReadUint64Bytes(b []byte) (u uint64, o []byte, err error) {
return
default:
err = badPrefix(UintType, lead)
if isnfixint(lead) {
err = UintBelowZero{Value: int64(rnfixint(lead))}
} else {
err = badPrefix(UintType, lead)
}
return
}
}

View File

@ -1,4 +1,4 @@
// +build !appengine
// +build !purego,!appengine
package msgp

View File

@ -685,7 +685,7 @@ func (mw *Writer) WriteIntf(v interface{}) error {
case reflect.Map:
return mw.writeMap(val)
}
return &ErrUnsupportedType{val.Type()}
return &ErrUnsupportedType{T: val.Type()}
}
func (mw *Writer) writeMap(v reflect.Value) (err error) {