Skip to content

unexpected behavior when buffer ends with backslash #384

@KimHyeonwoo

Description

@KimHyeonwoo

Version

v0.9.10

Summary

In double quote case (") of Stream.skipObject function, if s.buf ends with backslash (\), the character right next to backslash doesn't escaped.

Way to reproduce this issue

test.json

{"data": "--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------\""}
  • 511th character of the json is backslash
  • initial buffer size of the stream is 512, so 512th character of a buffer is \0
  • 512th character of the json is double quote, which is doesn't escaped and caused this issue

main.go

package main

import (
	"fmt"
	"os"

	"github.com/goccy/go-json"
)

type test struct{}

func (t test) UnmarshalJSON(bytes []byte) error {
	return nil
}

func main() {
	file, err := os.Open("./test.json")
	if err != nil {
		panic(fmt.Errorf("file open failed: %w", err))
	}
	defer file.Close()
	d := json.NewDecoder(file)
	var t test
	if err := d.Decode(&t); err != nil {
		panic(fmt.Errorf("decode failed: %w", err))
	}
}

output

panic: decode failed: json: string of object unexpected end of JSON input

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions