-
Notifications
You must be signed in to change notification settings - Fork 17.8k
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: encode panic #55829
Comments
Please provide a way to reproduce this issue. Ideally, a small standalone program with the command you used to run it, or a working example on https://go.dev/play/. |
I can't. The code is a segment in a complex server, and it doesn't happen every time. |
The stack trace makes little sense because it points to https://cs.opensource.google/go/go/+/refs/tags/go1.19:src/encoding/json/encode.go;l=1034, which does not use any pointers at all, so it shouldn't end up in a nil pointer dereference. The stack does show Another option is a data race causing memory corruption. Please try running your program with |
Whatever the case, unless I'm missing something obvious here, this doesn't look like a bug in the json package. I think you just have a corrupted string value. |
no race, dataMap is a local variable in function. you mean wrong string format will cause json marshal panic? more relative codes: `
} logger.Debug("send dyn accounts info to center") cmd := common.Cmd{ pkg := common.CreatePkg(cmd, common.PKG_TARGET_CENTER) // function common.CreatePkg:
} func EncodePkgData(dataMap map[string]interface{}, format uint8) []byte {
} // Cmd structure: func (cmd Cmd) ToMap() map[string]interface{} { type cmdMapI interface { |
The |
I don't know why code tag (<Ctrl+e>) didn't work, so, I use picture instead, hope it be clear: |
@xtxy See https://docs.github.com/en/get-started/writing-on-github/working-with-advanced-formatting/creating-and-highlighting-code-blocks for instructions on how to format code blocks in GitHub issues. |
I think it's time to move this out of the issue tracker; so far there is little evidence that there is a bug in encoding/json. See https://golang.org/wiki/Questions. |
thanks a lot |
@xtxy same issue for me. did you find the root cause? if so, could you share with me, thank a lot.
|
What version of Go are you using (
go version
)?Does this issue reproduce with the latest release?
I think so
What operating system and processor architecture are you using (
go env
)?go env
OutputWhat did you do?
json encode map[string]interface{}
data, err = json.Marshal(dataMap)
if err != nil {
logger.Error("msg.encode.error:", err)
return nil
}
What did you expect to see?
return data or error
What did you see instead?
occasionally, it panic:
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x50f704]
goroutine 1 [running]:
encoding/json.(*encodeState).marshal.func1()
/usr/local/go/src/encoding/json/encode.go:327 +0x6e
panic({0x81e580, 0xba1d20})
/usr/local/go/src/runtime/panic.go:884 +0x212
encoding/json.(*encodeState).string(0xc07876e080, {0x0, 0x20f}, 0x1)
/usr/local/go/src/encoding/json/encode.go:1034 +0x64
encoding/json.stringEncoder(0xc07876e080, {0x8008c0?, 0xc07510d2b0?, 0x1?}, {0x0?, 0x0?})
/usr/local/go/src/encoding/json/encode.go:646 +0x20a
encoding/json.mapEncoder.encode({0xc071f41680?}, 0xc07876e080, {0x81c260?, 0xc084d3b650?, 0x81c260?}, {0x60?, 0x65?})
/usr/local/go/src/encoding/json/encode.go:813 +0x5f5
encoding/json.(*encodeState).reflectValue(0x812080?, {0x81c260?, 0xc084d3b650?, 0xc084d3b6b0?}, {0xc0?, 0x55?})
/usr/local/go/src/encoding/json/encode.go:359 +0x78
encoding/json.interfaceEncoder(0xc07876e080, {0x812080?, 0xc074c4f2d0?, 0x1?}, {0x0?, 0x4?})
/usr/local/go/src/encoding/json/encode.go:715 +0xc8
encoding/json.mapEncoder.encode({0x20301d?}, 0xc07876e080, {0x81bf60?, 0xc084d3b680?, 0x81bf60?}, {0x21?, 0x30?})
/usr/local/go/src/encoding/json/encode.go:813 +0x5f5
encoding/json.(*encodeState).reflectValue(0xc0731f7958?, {0x81bf60?, 0xc084d3b680?, 0xc0731f79a8?}, {0xa0?, 0x2e?})
/usr/local/go/src/encoding/json/encode.go:359 +0x78
encoding/json.(*encodeState).marshal(0x8006c0?, {0x81bf60?, 0xc084d3b680?}, {0x7?, 0xd8?})
/usr/local/go/src/encoding/json/encode.go:331 +0xfa
encoding/json.Marshal({0x81bf60, 0xc084d3b680})
/usr/local/go/src/encoding/json/encode.go:160 +0x45
The text was updated successfully, but these errors were encountered: