Skip to content

Commit

Permalink
Fix panic on unmarshalling JSON (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
baibaratsky committed May 3, 2021
1 parent efa678f commit 92f87d0
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 0 deletions.
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

0 comments on commit 92f87d0

Please sign in to comment.