diff --git a/decode_test.go b/decode_test.go index b416f7b..a3cb746 100644 --- a/decode_test.go +++ b/decode_test.go @@ -3842,3 +3842,14 @@ func TestIssue327(t *testing.T) { 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) + } +} diff --git a/internal/decoder/map.go b/internal/decoder/map.go index bb18ef9..cb55ef0 100644 --- a/internal/decoder/map.go +++ b/internal/decoder/map.go @@ -87,13 +87,13 @@ func (d *mapDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) erro if mapValue == nil { mapValue = makemap(d.mapType, 0) } - if s.buf[s.cursor+1] == '}' { + s.cursor++ + if s.equalChar('}') { *(*unsafe.Pointer)(p) = mapValue - s.cursor += 2 + s.cursor++ return nil } for { - s.cursor++ k := unsafe_New(d.keyType) if err := d.keyDecoder.DecodeStream(s, depth, k); err != nil { return err @@ -117,6 +117,7 @@ func (d *mapDecoder) DecodeStream(s *Stream, depth int64, p unsafe.Pointer) erro if !s.equalChar(',') { return errors.ErrExpected("comma after object value", s.totalOffset()) } + s.cursor++ } }