Skip to content

Commit 92f87d0

Browse files
authored
Fix panic on unmarshalling JSON (#68)
1 parent efa678f commit 92f87d0

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

id.go

+4
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,10 @@ func (id *ID) UnmarshalJSON(b []byte) error {
253253
*id = nilID
254254
return nil
255255
}
256+
// Check the slice length to prevent panic on passing it to UnmarshalText()
257+
if len(b) < 2 {
258+
return ErrInvalidID
259+
}
256260
return id.UnmarshalText(b[1 : len(b)-1])
257261
}
258262

id_test.go

+4
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,10 @@ func TestIDJSONUnmarshalingError(t *testing.T) {
176176
if err != ErrInvalidID {
177177
t.Errorf("json.Unmarshal() err=%v, want %v", err, ErrInvalidID)
178178
}
179+
err = json.Unmarshal([]byte(`{"ID":1}`), &v)
180+
if err != ErrInvalidID {
181+
t.Errorf("json.Unmarshal() err=%v, want %v", err, ErrInvalidID)
182+
}
179183
}
180184

181185
func TestIDDriverValue(t *testing.T) {

0 commit comments

Comments
 (0)