fix error when unmarshal empty array

This commit is contained in:
IncSW 2021-06-15 18:19:39 +03:00
parent faa4de918e
commit 1037421a83
No known key found for this signature in database
GPG Key ID: 89876FA64BFB2D57
2 changed files with 27 additions and 1 deletions

View File

@ -1958,3 +1958,19 @@ func TestEncodeContextOption(t *testing.T) {
} }
}) })
} }
func TestIssue251(t *testing.T) {
array := [3]int{1, 2, 3}
err := stdjson.Unmarshal([]byte("[ ]"), &array)
if err != nil {
t.Fatal(err)
}
t.Log(array)
array = [3]int{1, 2, 3}
err = json.Unmarshal([]byte("[ ]"), &array)
if err != nil {
t.Fatal(err)
}
t.Log(array)
}

View File

@ -111,8 +111,17 @@ func (d *arrayDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe
return cursor, nil return cursor, nil
case '[': case '[':
idx := 0 idx := 0
for { cursor++
cursor = skipWhiteSpace(buf, cursor)
if buf[cursor] == ']' {
for idx < d.alen {
*(*unsafe.Pointer)(unsafe.Pointer(uintptr(p) + uintptr(idx)*d.size)) = d.zeroValue
idx++
}
cursor++ cursor++
return cursor, nil
}
for {
if idx < d.alen { if idx < d.alen {
c, err := d.valueDecoder.Decode(ctx, cursor, depth, unsafe.Pointer(uintptr(p)+uintptr(idx)*d.size)) c, err := d.valueDecoder.Decode(ctx, cursor, depth, unsafe.Pointer(uintptr(p)+uintptr(idx)*d.size))
if err != nil { if err != nil {
@ -137,6 +146,7 @@ func (d *arrayDecoder) Decode(ctx *RuntimeContext, cursor, depth int64, p unsafe
cursor++ cursor++
return cursor, nil return cursor, nil
case ',': case ',':
cursor++
continue continue
default: default:
return 0, errors.ErrInvalidCharacter(buf[cursor], "array", cursor) return 0, errors.ErrInvalidCharacter(buf[cursor], "array", cursor)