-
Notifications
You must be signed in to change notification settings - Fork 0
[FEAT] Encoding Interfaces #14
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
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 |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| package enumify | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| "strings" | ||
| ) | ||
|
|
@@ -47,6 +48,11 @@ func ParseFactory[T ~uint8, Names []string | [][]string](names Names) Parser[T] | |
| unknown := T(0) | ||
|
|
||
| return func(val any) (T, error) { | ||
| // Convert byte slices to strings. | ||
| if v, ok := val.([]byte); ok && len(v) > 1 { | ||
| val = string(v) | ||
| } | ||
|
|
||
| switch v := val.(type) { | ||
| case string: | ||
| v = normalize(v) | ||
|
|
@@ -65,6 +71,15 @@ func ParseFactory[T ~uint8, Names []string | [][]string](names Names) Parser[T] | |
|
|
||
| // If no match is found, return an error. | ||
| return unknown, fmt.Errorf("invalid %T value: %q", unknown, v) | ||
| case []byte: | ||
| switch len(v) { | ||
| case 0: | ||
| return unknown, nil | ||
| case 1: | ||
| return T(v[0]), nil | ||
|
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. Single-byte slice parsing skips range validationHigh Severity The Reviewed by Cursor Bugbot for commit 9dccb90. Configure here. |
||
| default: | ||
| panic(errors.New("byte slices should be parsed as strings: this code should be unreachable")) | ||
| } | ||
| case T: | ||
| if v >= T(len(normalizedNames)) { | ||
| return unknown, fmt.Errorf("invalid %T value: %d", unknown, v) | ||
|
|
||


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.
Generated UnmarshalBinary accepts out-of-range byte values
High Severity
The
UnmarshalBinarymethod, when handling single-byte input, directly casts the byte to the enum type. This bypasses the validation logic used by other unmarshalers, allowing invalid byte values to create invalid enum instances.Reviewed by Cursor Bugbot for commit 9dccb90. Configure here.