From 81519c48d8dd664cd52ccd5e77ec2a7cc1580e09 Mon Sep 17 00:00:00 2001 From: Nao Yonashiro Date: Tue, 22 Feb 2022 19:10:26 +0900 Subject: [PATCH] fix: avoid reading the next character in buffer to nul consideration fix #337 --- decode_test.go | 11 +++++++++++ internal/decoder/map.go | 7 ++++--- 2 files changed, 15 insertions(+), 3 deletions(-) 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++ } }