-
Notifications
You must be signed in to change notification settings - Fork 72
Improve UnknownFieldError message #669
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -207,11 +207,13 @@ func (e *DupMapKeyError) Error() string { | |||||
|
|
||||||
| // UnknownFieldError describes detected unknown field in CBOR map when decoding to Go struct. | ||||||
| type UnknownFieldError struct { | ||||||
| Index int | ||||||
| Struct string // name of the struct being decoded | ||||||
| Field string // field of the CBOR map that was unexpected | ||||||
| Index int // index of the field in the CBOR map | ||||||
| } | ||||||
|
|
||||||
| func (e *UnknownFieldError) Error() string { | ||||||
| return fmt.Sprintf("cbor: found unknown field at map element index %d", e.Index) | ||||||
| return fmt.Sprintf("cbor: found unknown field: struct '%s' has no field '%s', at map element index %d", e.Struct, e.Field, e.Index) | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Using In this PR, field name is decoded from untrusted data, so we need to consider the possibility it might contain non-printable characters. We can use Please also reword the error message to be more consistent with the old message, like this:
Suggested change
|
||||||
| } | ||||||
|
|
||||||
| // UnacceptableDataItemError is returned when unmarshaling a CBOR input that contains a data item | ||||||
|
|
@@ -2617,8 +2619,8 @@ MapEntryLoop: | |||||
| var k any | ||||||
|
|
||||||
| t := d.nextCBORType() | ||||||
| var keyBytes []byte | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. These changes don't cover all use cases in Valid map keys in this function can be byte string, text string, or integer (to support So we also need to handle unknown field when map key is integer. Also, please add a test for unknown field of integer type. |
||||||
| if t == cborTypeTextString || (t == cborTypeByteString && d.dm.fieldNameByteString == FieldNameByteStringAllowed) { | ||||||
| var keyBytes []byte | ||||||
| if t == cborTypeTextString { | ||||||
| keyBytes, lastErr = d.parseTextString() | ||||||
| if lastErr != nil { | ||||||
|
|
@@ -2767,7 +2769,7 @@ MapEntryLoop: | |||||
|
|
||||||
| if f == nil { | ||||||
| if errOnUnknownField { | ||||||
| err = &UnknownFieldError{j} | ||||||
| err = &UnknownFieldError{Struct: tInfo.typ.String(), Field: string(keyBytes), Index: j} | ||||||
| d.skip() // Skip value | ||||||
| j++ | ||||||
| // skip the rest of the map | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please rename two fields: