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

C.JSON not work on go 1.18 #1820

Closed
luyouxin84 opened this issue Mar 17, 2022 · 18 comments · Fixed by #1851
Closed

C.JSON not work on go 1.18 #1820

luyouxin84 opened this issue Mar 17, 2022 · 18 comments · Fixed by #1851

Comments

@luyouxin84
Copy link

in go 1.17.8 c.json work fine,but after i upgrade go version , c.json not work report "runtime error empty pointer"

@efectn
Copy link
Member

efectn commented Mar 17, 2022

Can you send issue to https://github.com/goccy/go-json

@limjinyung
Copy link

same here, but I get unexpected fault address instead

@efectn
Copy link
Member

efectn commented Mar 17, 2022

Sorry 😅😅

@efectn
Copy link
Member

efectn commented Mar 18, 2022

@luyouxin84 @limjinyung What version of fiber are you using?

@luyouxin84
Copy link
Author

@luyouxin84 @limjinyung What version of fiber are you using?

github.com/gofiber/fiber/v2 v2.23.0

@efectn
Copy link
Member

efectn commented Mar 18, 2022

@luyouxin84 @limjinyung What version of fiber are you using?

github.com/gofiber/fiber/v2 v2.23.0

Update version. It will be solved probably. Also i recommend everybody to use latest version of Fiber. They include most bug fixes and features.

@kokizzu
Copy link

kokizzu commented Mar 21, 2022

can't reproduce

go version
go version go1.18 linux/amd64
dpkg -l | grep golang
ii  golang-1.18                                   1.18-1longsleep1+focal                                 all          Go programming language compiler - metapackage
ii  golang-1.18-doc                               1.18-1longsleep1+focal                                 all          Go programming language - documentation
ii  golang-1.18-go                                1.18-1longsleep1+focal                                 amd64        Go programming language compiler, linker, compiled stdlib
ii  golang-1.18-src                               1.18-1longsleep1+focal                                 all          Go programming language - source files

image

@efectn
Copy link
Member

efectn commented Mar 21, 2022

@luyouxin84 @limjinyung What version of fiber are you using?

github.com/gofiber/fiber/v2 v2.23.0

Update version. It will be solved probably. Also i recommend everybody to use latest version of Fiber. They include most bug fixes and features.

I'm closing the issue because of this. If you problem not solved, feel free to re-open the issue.

@luisgarciaalanis
Copy link

I am seeing this crash on v2.30.0

the JSON encoder is crashing.
github.com/gofiber/fiber/v2/internal/go-json/encoder.copyOpcode(0x0)
/Users/luis/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/opcode.go:322 +0x33
github.com/gofiber/fiber/v2/internal/go-json/encoder.(*Compiler).linkRecursiveCode(0xc00016f3b0?, 0xc0002cb950)
/Users/luis/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/compiler.go:892 +0x13c
github.com/gofiber/fiber/v2/internal/go-json/encoder.(*Compiler).codeToOpcode(0x0?, 0xc0002cb950, 0x1617c80, {0x17b2320?, 0xc00016f3b0?})
/Users/luis/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/compiler.go:883 +0xe5
github.com/gofiber/fiber/v2/internal/go-json/encoder.(*Compiler).codeToOpcodeSet(0x8?, 0x1617c80, {0x17b2320?, 0xc00016f3b0})
/Users/luis/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/compiler.go:108 +0xc9
github.com/gofiber/fiber/v2/internal/go-json/encoder.(*Compiler).compile(0x8?, 0x1617c80)
/Users/luis/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/compiler.go:104 +0x54
github.com/gofiber/fiber/v2/internal/go-json/encoder.CompileToGetCodeSet(0xc00061d488?, 0x107a08c?)
/Users/luis/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/compiler_norace.go:18 +0x125
github.com/gofiber/fiber/v2/internal/go-json.encode(0xc000112820, {0x1617c80, 0xc00016f380})
/Users/luis/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encode.go:224 +0xd0
github.com/gofiber/fiber/v2/internal/go-json.marshal({0x1617c80, 0xc00016f380}, {0x0, 0x0, 0x30?})
/Users/luis/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encode.go:148 +0xba
github.com/gofiber/fiber/v2/internal/go-json.MarshalWithOption(...)
/Users/luis/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/json.go:186
github.com/gofiber/fiber/v2/internal/go-json.Marshal({0x1617c80?, 0xc00016f380?})
/Users/luis/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/json.go:171 +0x2a
github.com/gofiber/fiber/v2.(*Ctx).JSON(0xc0000222c0, {0x1617c80?, 0xc00016f380?})

@kokizzu
Copy link

kokizzu commented Mar 24, 2022

what's the payload that causes that?
or at least reproducible code..

@luisgarciaalanis
Copy link

luisgarciaalanis commented Mar 25, 2022

I removed lots of code while it was still crashing and ended up with this:

using:

module crashRepro

go 1.18

require (
	github.com/gofiber/fiber/v2 v2.30.0
	github.com/oklog/ulid/v2 v2.0.2
)

require (
	github.com/andybalholm/brotli v1.0.4 // indirect
	github.com/klauspost/compress v1.15.0 // indirect
	github.com/valyala/bytebufferpool v1.0.0 // indirect
	github.com/valyala/fasthttp v1.34.0 // indirect
	github.com/valyala/tcplisten v1.0.0 // indirect
	golang.org/x/sys v0.0.0-20220227234510-4e6760a101f9 // indirect
)

Please see NOTES in code.

package main

import (
	"encoding/json"
	"fmt"

	"github.com/gofiber/fiber/v2"
)

const payload = `
{
  "contacts": [
	{
	  "name": "Amy"
	}
  ]
}
`

type Contact struct {
	Name string `json:"name"`

	// NOTE 1: commenting Friends makes the unmarshall not crash.
	Friends []Contact `json:"friends,omitempty"`
}

type ContactRow struct {
	Contact
}

var response struct {
	// NOTE 2: Changing ContactRow to Contact on the arrat makes the unmarshall not crash irregardless of NOTE 1.
	Contacts []ContactRow `json:"contacts"`
}

func main() {
	fmt.Println("http://127.0.0.1:8000/crash")

	var app = fiber.New()
	app.Get("/crash", func(ctx *fiber.Ctx) error {
		if err := json.Unmarshal([]byte(payload), &response); err != nil {
			panic("Did not unmarshall using std library")
		}

		// NOTE 3: the next line will crash.
		return ctx.JSON(response)
	})
	if err := app.Listen(":8000"); err != nil {
		fmt.Println(err)
	}
}

@kokizzu
Copy link

kokizzu commented Mar 25, 2022

confirmed

github.com/gofiber/fiber/v2/internal/go-json/encoder.copyOpcode(0x0)
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/opcode.go:322 +0x4a
github.com/gofiber/fiber/v2/internal/go-json/encoder.(*Compiler).linkRecursiveCode(0xc0000c0098, 0xc0000c6690)
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/compiler.go:892 +0x135
github.com/gofiber/fiber/v2/internal/go-json/encoder.(*Compiler).codeToOpcode(0xc0000c0098, 0xc0000c6690, 0x91b1a0, {0x9b1668, 0xc0000c6450})
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/compiler.go:883 +0xb1
github.com/gofiber/fiber/v2/internal/go-json/encoder.(*Compiler).codeToOpcodeSet(0xc0000c0098, 0x91b1a0, {0x9b1668, 0xc0000c6450})
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/compiler.go:108 +0x196
github.com/gofiber/fiber/v2/internal/go-json/encoder.(*Compiler).compile(0xc0000c0098, 0x91b1a0)
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/compiler.go:104 +0x12f
github.com/gofiber/fiber/v2/internal/go-json/encoder.CompileToGetCodeSet(0xc0000e6750, 0x91b1a0)
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encoder/compiler_norace.go:18 +0x25b
github.com/gofiber/fiber/v2/internal/go-json.encode(0xc0000e6750, {0x91b1a0, 0xc0000e4150})
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encode.go:224 +0xed
github.com/gofiber/fiber/v2/internal/go-json.marshal({0x91b1a0, 0xc0000e4150}, {0x0, 0x0, 0x0})
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/encode.go:148 +0x189
github.com/gofiber/fiber/v2/internal/go-json.MarshalWithOption({0x91b1a0, 0xc0000e4150}, {0x0, 0x0, 0x0})
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/json.go:186 +0xa5
github.com/gofiber/fiber/v2/internal/go-json.Marshal({0x91b1a0, 0xc0000e4150})
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/internal/go-json/json.go:171 +0x7c
github.com/gofiber/fiber/v2.(*Ctx).JSON(0xc0000de000, {0x91b1a0, 0xc0000e4150})
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/ctx.go:695 +0x87
main.main.func1(0xc0000de000)
        /home/kyz/go/src/fibercrashexample/a.go:46 +0x18c
github.com/gofiber/fiber/v2.(*App).next(0xc0000001e0, 0xc0000de000)
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/router.go:132 +0x39a
github.com/gofiber/fiber/v2.(*App).handler(0xc0000001e0, 0xc0000d0000)
        /home/kyz/go/pkg/mod/github.com/gofiber/fiber/v2@v2.30.0/router.go:160 +0x91
github.com/valyala/fasthttp.(*Server).serveConn(0xc00013e480, {0x9b2318, 0xc0000c0008})
        /home/kyz/go/pkg/mod/github.com/valyala/fasthttp@v1.34.0/server.go:2341 +0x1612
github.com/valyala/fasthttp.(*workerPool).workerFunc(0xc000128c80, 0xc0000ce000)
        /home/kyz/go/pkg/mod/github.com/valyala/fasthttp@v1.34.0/workerpool.go:224 +0xe7
github.com/valyala/fasthttp.(*workerPool).getCh.func1()
        /home/kyz/go/pkg/mod/github.com/valyala/fasthttp@v1.34.0/workerpool.go:196 +0x47
created by github.com/valyala/fasthttp.(*workerPool).getCh
        /home/kyz/go/pkg/mod/github.com/valyala/fasthttp@v1.34.0/workerpool.go:195 +0x2d1

@kokizzu
Copy link

kokizzu commented Mar 25, 2022

anonymous struct is the culprit, without anonymous struct it doesn't crash
goccy/go-json#339

@hi019
Copy link
Contributor

hi019 commented Mar 25, 2022

As a workaround, you can set JSONEncoder in fiber.Config to json.Marshal from the stdlib (or from https://github.com/segmentio/encoding for more performance)

@shahriar350
Copy link

Same problem i already faced in my project and change gofiber default json to ffjson.

@luisgarciaalanis
Copy link

luisgarciaalanis commented Mar 28, 2022

It works with json.Marshal

@ReneWerner87
Copy link
Member

@luisgarciaalanis can you test it with the version 2.31.0 or the current master commit

require github.com/gofiber/fiber/v2 v2.31.1-0.20220405064022-a8f9d4acfb2c

@efectn efectn linked a pull request Apr 9, 2022 that will close this issue
@coreybutler
Copy link

I ran into this issue using 2.30.0. Upgrading to 2.31.0 resolved the issue.

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

Successfully merging a pull request may close this issue.

9 participants