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

Fix panic on unmarshalling JSON #68

Merged
merged 1 commit into from May 3, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 4 additions & 0 deletions id.go
Expand Up @@ -253,6 +253,10 @@ func (id *ID) UnmarshalJSON(b []byte) error {
*id = nilID
return nil
}
// Check the slice length to prevent panic on passing it to UnmarshalText()
if len(b) < 2 {
return ErrInvalidID
}
return id.UnmarshalText(b[1 : len(b)-1])
}

Expand Down
4 changes: 4 additions & 0 deletions id_test.go
Expand Up @@ -176,6 +176,10 @@ func TestIDJSONUnmarshalingError(t *testing.T) {
if err != ErrInvalidID {
t.Errorf("json.Unmarshal() err=%v, want %v", err, ErrInvalidID)
}
err = json.Unmarshal([]byte(`{"ID":1}`), &v)
if err != ErrInvalidID {
t.Errorf("json.Unmarshal() err=%v, want %v", err, ErrInvalidID)
}
}

func TestIDDriverValue(t *testing.T) {
Expand Down