Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement remaining canonicity checks #31

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
46 changes: 39 additions & 7 deletions gen/unmarshal.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ func (u *unmarshalGen) mapstruct(s *Struct) {

u.assignAndCheck(sz, isnil, arrayHeader)

u.p.print("\nif validate {") // map encoded as array => non canonical
u.p.print("\nerr = &msgp.ErrNonCanonical{}")
u.p.print("\nif validate {") // map encoded as array => non canonical
u.p.print("\nerr = msgp.ErrNonCanonical(\"map encoded as array\")")
u.p.print("\nreturn")
u.p.print("\n}")

Expand Down Expand Up @@ -207,7 +207,12 @@ func (u *unmarshalGen) mapstruct(s *Struct) {
u.p.printf("\n}")

u.p.printf("\nfor %s > 0 {", sz)
u.p.printf("\n%s--; field, bts, err = msgp.ReadMapKeyZC(bts)", sz)
u.p.printf("\n%s--", sz)
u.p.printf("\n if validate {")
u.p.printf("\nfield, bts, err = msgp.ReadMapKeyZCCanonical(bts)")
u.p.printf("\n} else {")
u.p.printf("\nfield, bts, err = msgp.ReadMapKeyZC(bts)")
u.p.printf("\n}")
u.p.wrapErrCheck(u.ctx.ArgsStr())
u.p.print("\nswitch string(field) {")
for i := range s.Fields {
Expand All @@ -220,12 +225,25 @@ func (u *unmarshalGen) mapstruct(s *Struct) {
}
u.p.printf("\ncase \"%s\":", s.Fields[i].FieldTag)
u.p.printf("\nif validate && %s && \"%s\" < %s {", lastIsSet, s.Fields[i].FieldTag, last)
u.p.print("\nerr = &msgp.ErrNonCanonical{}")
u.p.print("\nerr = msgp.ErrNonCanonical(\"struct fields out of order\")")
u.p.printf("\nreturn")
u.p.print("\n}")
u.ctx.PushString(s.Fields[i].FieldName)
next(u, s.Fields[i].FieldElem)
u.ctx.Pop()
if isFieldOmitEmpty(s.Fields[i], s) {
u.p.printf("\nif validate {")
if ize := s.Fields[i].FieldElem.IfZeroExpr(); ize != "" {
u.p.printf("\nif %s {", ize)
u.p.printf("\nerr = msgp.ErrNonCanonical(\"zero value for omitempty field\")")
u.p.printf("\nreturn")
u.p.printf("\n}") // close if ize
} else {
u.p.printf("\nerr = msgp.ErrMissingZeroExpr(\"%s\")", s.Fields[i].FieldElem.TypeName())
u.p.printf("\nreturn")
}
u.p.printf("\n}") // close if validate
}
u.p.printf("\n%s = \"%s\"", last, s.Fields[i].FieldTag)
}
u.p.print("\ndefault:\nerr = msgp.ErrNoField(string(field))")
Expand Down Expand Up @@ -262,7 +280,11 @@ func (u *unmarshalGen) gBase(b *BaseElem) {
u.p.printf("\nreturn")
u.p.printf("\n}")
}
u.p.printf("\n if validate {")
u.p.printf("\n%s, bts, err = msgp.ReadBytesBytesCanonical(bts, %s)", refname, lowered)
u.p.printf("\n} else {")
u.p.printf("\n%s, bts, err = msgp.ReadBytesBytes(bts, %s)", refname, lowered)
u.p.printf("\n}")
case Ext:
u.p.printf("\nbts, err = msgp.ReadExtensionBytes(bts, %s)", lowered)
case IDENT:
Expand All @@ -278,7 +300,17 @@ func (u *unmarshalGen) gBase(b *BaseElem) {
u.p.printf("\nreturn")
u.p.printf("\n}")
}
u.p.printf("\n%s, bts, err = msgp.ReadStringBytes(bts)", refname)
u.p.printf("\nif validate {")
u.p.printf("\n%s, bts, err = msgp.ReadStringBytesCanonical(bts)", refname)
u.p.printf("\n} else {")
u.p.printf("\n %s, bts, err = msgp.ReadStringBytes(bts)", refname)
u.p.printf("\n}")
case Uint, Uint8, Uint16, Uint32, Uint64, Int, Int8, Int16, Int32, Int64:
u.p.printf("\nif validate {")
u.p.printf("\n%s, bts, err = msgp.Read%sBytesCanonical(bts)", refname, b.BaseName())
u.p.printf("\n} else {")
u.p.printf("\n %s, bts, err = msgp.Read%sBytes(bts)", refname, b.BaseName())
u.p.printf("\n}")
default:
u.p.printf("\n%s, bts, err = msgp.Read%sBytes(bts)", refname, b.BaseName())
}
Expand Down Expand Up @@ -366,11 +398,11 @@ func (u *unmarshalGen) gMap(m *Map) {
u.p.printf("\nif validate {")
if m.Key.LessFunction() != "" {
u.p.printf("\nif %s && %s(%s, %s) {", lastSet, m.Key.LessFunction(), m.Keyidx, last)
u.p.printf("\nerr = &msgp.ErrNonCanonical{}")
u.p.printf("\nerr = msgp.ErrNonCanonical(\"map keys out of order\")")
u.p.printf("\nreturn")
u.p.printf("\n}")
} else {
u.p.printf("\nerr = &msgp.ErrMissingLessFn{}")
u.p.printf("\nerr = msgp.ErrMissingLessFn(\"%s\")", m.Key.TypeName())
u.p.printf("\nreturn")
}
u.p.printf("\n}") // close if validate block
Expand Down
5 changes: 5 additions & 0 deletions msgp/defs.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,11 @@ const last5 = 0x1f
const first3 = 0xe0
const last7 = 0x7f

const minFixnInt = -32
const maxFixInt = 127

const maxFixStrLen = 31

func isfixint(b byte) bool {
return b>>7 == 0
}
Expand Down
49 changes: 40 additions & 9 deletions msgp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,28 +344,59 @@ func (e *ErrUnsupportedType) withContext(ctx string) error {
return &o
}

// ErrNonCanonical is returned
// errNonCanonical is returned
// when unmarshaller detects that
// the message is not canonically encoded (pre-sorted)
type ErrNonCanonical struct{}
type errNonCanonical struct {
reason string
}

// Error implements error
func (e *ErrNonCanonical) Error() string {
return fmt.Sprintf("msgp: non-canonical encoding detected")
func (e errNonCanonical) Error() string {
return fmt.Sprintf("msgp: non-canonical encoding: %s", e.reason)
}

// Resumable returns false for errNonCanonical
func (e *ErrNonCanonical) Resumable() bool { return false }
func (e errNonCanonical) Resumable() bool { return false }

func ErrNonCanonical(reason string) error {
return errNonCanonical{reason}
}

// ErrNonCanonical is returned
// errMissingLessFn is returned
// when unmarshaller detects that
// the LessFunc is not provided for the type and the canonicity cannot be validated
type errMissingLessFn struct {
typeName string
}

// Error implements error
func (e errMissingLessFn) Error() string {
return fmt.Sprintf("msgp: can't validate canonicity: missing LessFn")
}

// Resumable returns false for errNonCanonical
func (e errMissingLessFn) Resumable() bool { return false }

func ErrMissingLessFn(typeName string) error {
return errMissingLessFn{typeName}
}

// errMissingZeroExpr is returned
// when unmarshaller detects that
// the message is not canonically encoded (pre-sorted)
type ErrMissingLessFn struct{}
type errMissingZeroExpr struct {
typeName string
}

// Error implements error
func (e *ErrMissingLessFn) Error() string {
func (e errMissingZeroExpr) Error() string {
return fmt.Sprintf("msgp: can't validate canonicity: missing LessFn")
}

// Resumable returns false for errNonCanonical
func (e *ErrMissingLessFn) Resumable() bool { return false }
func (e errMissingZeroExpr) Resumable() bool { return false }

func ErrMissingZeroExpr(typeName string) error {
return errMissingZeroExpr{typeName}
}
Loading