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

valueCtx call Value can cause panic #41254

Closed
Wang-Jun-Chao opened this issue Sep 7, 2020 · 4 comments
Closed

valueCtx call Value can cause panic #41254

Wang-Jun-Chao opened this issue Sep 7, 2020 · 4 comments

Comments

@Wang-Jun-Chao
Copy link

@Wang-Jun-Chao Wang-Jun-Chao commented Sep 7, 2020

in context/context.go, I find some sence can lead valueCtx.Value panic

// create valueCtx, pass nil parent Context
func WithValue(parent Context, key, val interface{}) Context {
	if key == nil {
		panic("nil key")
	}
	if !reflectlite.TypeOf(key).Comparable() {
		panic("key is not comparable")
	}
	return &valueCtx{parent, key, val}
}

// get value
func (c *valueCtx) Value(key interface{}) interface{} {
	if c.key == key {
		return c.val
	}
	return c.Context.Value(key) //  if c.Context is nil, then call Value will panic !!!
}
@davecheney
Copy link
Contributor

@davecheney davecheney commented Sep 7, 2020

Thank you for raising this issue. Would you please supply a runnable code sample that shows the problem. Thank you

@Wang-Jun-Chao
Copy link
Author

@Wang-Jun-Chao Wang-Jun-Chao commented Sep 7, 2020

@davecheney
case code

package main

import (
	"context"
	"fmt"
)

func main() {
	ctx := context.WithValue(nil, "test-key", "test-val")
	val := ctx.Value("not-exist-key")

	fmt.Println(val)
}

result

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x30 pc=0x106ff9f]

goroutine 1 [running]:
context.(*valueCtx).Value(0xc00006a180, 0x10ac320, 0x10e9f50, 0x10e9f30, 0x10ac320)
        /usr/local/go/src/context/context.go:553 +0x3f
main.main()
        /Users/wangjunchao/go/src/wangjunchao/value_context_panic/value_context_panic.go:10 +0x82
@davecheney
Copy link
Contributor

@davecheney davecheney commented Sep 7, 2020

Please don’t pass nil as the parent context. This is documented in the package preamble.

Do not pass a nil Context, even if a function permits it. Pass context.TODO if you are unsure about which Context to use.

https://godoc.org/context

@mvdan
Copy link
Member

@mvdan mvdan commented Sep 7, 2020

The Go project doesn't use the issue tracker for questions. See https://golang.org/wiki/Questions.

It seems like Dave clarified your problem above, so I'm closing this for now. In the future, please try one of the forums for questions.

@mvdan mvdan closed this Sep 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
3 participants
You can’t perform that action at this time.