Skip to content
This repository was archived by the owner on Mar 31, 2023. It is now read-only.

JSONFormat outputs non-JSON on some non-printable characters #31

Closed
itchyny opened this issue Mar 15, 2022 · 0 comments · Fixed by #33
Closed

JSONFormat outputs non-JSON on some non-printable characters #31

itchyny opened this issue Mar 15, 2022 · 0 comments · Fixed by #33
Labels

Comments

@itchyny
Copy link
Contributor

itchyny commented Mar 15, 2022

Describe the bug
JSONFormat is expected to output JSON regardless of the input data, but outputs non-JSON on some special characters; \x00, \a, \v, \U0001FBFF.

Environments

  • Version: 1.6.1
  • OS: macOS 12.2.1
  • Go: go1.17.8 darwin/amd64

To Reproduce
Steps to reproduce the behavior:
Run following code.

package main

import (
	"encoding/json"
	"fmt"
	"os"
	"time"

	"github.com/cybozu-go/log"
)

func main() {
	var err error
	l := log.NewLogger()
	f := log.JSONFormat{Utsname: "test"}
	t := time.Now()

	buf := make([]byte, 0, 256)
	buf, err = f.Format(buf, l, t, log.LvDebug, "test", map[string]interface{}{
		"x": "\x00\a\v\U0001FBFF",
	})
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(1)
	}
	fmt.Printf("%s\n", buf)

	var data interface{}
	err = json.Unmarshal(buf, &data)
	if err != nil {
		fmt.Fprintf(os.Stderr, "%s\n", err)
		os.Exit(1)
	}
	fmt.Printf("%v\n", data)
}

Get following output.

{"topic":"main","logged_at":"2022-03-15T06:39:16.059075Z","severity":"debug","utsname":"test","message":"test","x":"\x00\a\v\U0001fbff"}

invalid character 'x' in string escape code
exit status 1

Expected behavior
JSONFormat emits valid JSON even if the input data contains non-printable characters, and the output is safely decoded back to the data.

Additional context
Currently this formatter uses strconv.AppendQuote to encode string values. This method is intended to output Go string literal representation, which allows wider range of character representation than JSON. For example, "\x00\a\v\U0001FBFF" is a valid Go string literal, but its quoted value "\"\\x00\\a\\v\\U0001fbff\"" is not a valid JSON.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant