Merge pull request #338 from orisano/fix/#337

fix: avoid reading the next character in buffer to nul consideration
This commit is contained in:
Masaaki Goshima 2022-03-04 20:51:40 +09:00 committed by GitHub
commit 58b524e43e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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++
} }
} }