Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

encoding/json: SyntaxError's Offset is wrong after decoding some valid json #32399

Open
itchyny opened this issue Jun 3, 2019 · 4 comments

Comments

Projects
None yet
5 participants
@itchyny
Copy link

commented Jun 3, 2019

What version of Go are you using (go version)?

$ go version
go version go1.12.5 darwin/amd64

Does this issue reproduce with the latest release?

Yes

What operating system and processor architecture are you using (go env)?

go env Output
$ go env
GOARCH="amd64"
GOBIN=""
GOCACHE="/Users/itchyny/Library/Caches/go-build"
GOEXE=""
GOFLAGS=""
GOHOSTARCH="amd64"
GOHOSTOS="darwin"
GOOS="darwin"
GOPATH="/Users/itchyny/.go"
GOPROXY=""
GORACE=""
GOROOT="/usr/local/Cellar/go/1.12.5/libexec"
GOTMPDIR=""
GOTOOLDIR="/usr/local/Cellar/go/1.12.5/libexec/pkg/tool/darwin_amd64"
GCCGO="gccgo"
CC="clang"
CXX="clang++"
CGO_ENABLED="1"
GOMOD=""
CGO_CFLAGS="-g -O2"
CGO_CPPFLAGS=""
CGO_CXXFLAGS="-g -O2"
CGO_FFLAGS="-g -O2"
CGO_LDFLAGS="-g -O2"
PKG_CONFIG="pkg-config"
GOGCCFLAGS="-fPIC -m64 -pthread -fno-caret-diagnostics -Qunused-arguments -fmessage-length=0 -fdebug-prefix-map=/var/folders/mg/nqgjy_l53n15b3_dfrjw9_600000gn/T/go-build713456458=/tmp/go-build -gno-record-gcc-switches -fno-common"

What did you do?

Run the following code (https://play.golang.org/p/gBI1qRBJvPa).

package main

import (
	"encoding/json"
	"fmt"
	"io"
	"os"
	"strings"
)

func main() {
	in := strings.NewReader(`false true [ "foo" : "bar" ]`)
	dec := json.NewDecoder(in)
	var vs []interface{}
	for {
		var v interface{}
		if err := dec.Decode(&v); err != nil {
			if err != io.EOF {
				fmt.Printf("%#v\n", err)
				os.Exit(1)
			}
			break
		}
		vs = append(vs, v)
	}
	fmt.Println(vs)
}

What did you expect to see?

Outputs &json.SyntaxError{msg:"invalid character ':' after array element", Offset:20}.

What did you see instead?

Outputs &json.SyntaxError{msg:"invalid character ':' after array element", Offset:22}

@agnivade

This comment has been minimized.

Copy link
Member

commented Jun 4, 2019

@mvdan

This comment has been minimized.

Copy link
Member

commented Jun 4, 2019

Could you please clarify why you expected the offset to be 20?

@itchyny

This comment has been minimized.

Copy link
Author

commented Jun 4, 2019

I expect because the 20th character of the source string false true [ "foo" : "bar" ] is :. In addition, I expect the Offset does not exceed the source length (see https://play.golang.org/p/W73hGXHZIxq).

@dmitshur dmitshur added this to the Go1.14 milestone Jun 10, 2019

@itchyny itchyny referenced a pull request that will close this issue Jun 13, 2019

Open

encoding/json: fix scanner byte offset on scanEnd #32598

@gopherbot

This comment has been minimized.

Copy link

commented Jun 13, 2019

Change https://golang.org/cl/182117 mentions this issue: encoding/json: fix scanner byte offset on scanEnd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.