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 3 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
26 changes: 21 additions & 5 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 @@ -220,12 +220,18 @@ 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 ize := s.Fields[i].FieldElem.IfZeroExpr(); ize != "" && isFieldOmitEmpty(s.Fields[i], s) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not robust. If IfZeroExpr() returns empty string it will validate unconditionally

u.p.printf("\nif validate && %s {", ize)
u.p.printf("\nerr = msgp.ErrNonCanonical(\"zero value for omitempty field\")")
u.p.printf("\nreturn")
u.p.printf("\n}")
}
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 +268,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 @@ -279,6 +289,12 @@ func (u *unmarshalGen) gBase(b *BaseElem) {
u.p.printf("\n}")
}
u.p.printf("\n%s, bts, err = msgp.ReadStringBytes(bts)", refname)
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 +382,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{}")
u.p.printf("\nreturn")
}
u.p.printf("\n}") // close if validate block
Expand Down
20 changes: 13 additions & 7 deletions msgp/errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,28 +344,34 @@ 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 }

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

// Error implements error
func (e *ErrMissingLessFn) Error() string {
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 (e ErrMissingLessFn) Resumable() bool { return false }

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