fix: avoid reading the next character in buffer to nul consideration

fix #337
This commit is contained in:
Nao Yonashiro 2022-02-22 19:10:26 +09:00
parent 116e62dc84
commit 81519c48d8
2 changed files with 15 additions and 3 deletions

View File

@ -3842,3 +3842,14 @@ func TestIssue327(t *testing.T) {
t.Fatalf("failed to decode. expected %q but got %q", expected, got) t.Fatalf("failed to decode. expected %q but got %q", expected, got)
} }
} }
func TestIssue337(t *testing.T) {
in := strings.Repeat(" ", 510) + "{}"
var m map[string]string
if err := json.NewDecoder(strings.NewReader(in)).Decode(&m); err != nil {
t.Fatal("unexpected error:", err)
}
if len(m) != 0 {
t.Fatal("unexpected result", m)
}
}

View File

@ -87,13 +87,13 @@ func (d *mapDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) erro
if mapValue == nil { if mapValue == nil {
mapValue = makemap(d.mapType, 0) mapValue = makemap(d.mapType, 0)
} }
if s.buf[s.cursor+1] == '}' { s.cursor++
if s.equalChar('}') {
*(*unsafe.Pointer)(p) = mapValue *(*unsafe.Pointer)(p) = mapValue
s.cursor += 2 s.cursor++
return nil return nil
} }
for { for {
s.cursor++
k := unsafe_New(d.keyType) k := unsafe_New(d.keyType)
if err := d.keyDecoder.DecodeStream(s, depth, k); err != nil { if err := d.keyDecoder.DecodeStream(s, depth, k); err != nil {
return err return err
@ -117,6 +117,7 @@ func (d *mapDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) erro
if !s.equalChar(',') { if !s.equalChar(',') {
return errors.ErrExpected("comma after object value", s.totalOffset()) return errors.ErrExpected("comma after object value", s.totalOffset())
} }
s.cursor++
} }
} }