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

Custom String() method of maps got invalid receiver value #141

Open
chenzhuoyu opened this issue Jul 7, 2022 · 4 comments
Open

Custom String() method of maps got invalid receiver value #141

chenzhuoyu opened this issue Jul 7, 2022 · 4 comments

Comments

@chenzhuoyu
Copy link

Repro:

package main

import (
	"fmt"

	"github.com/davecgh/go-spew/spew"
)

type X map[int]int

func (x X) String() string {
	return fmt.Sprintf("{M1:%d}", len(x))
}

func main() {
	spew.Dump(X{1: 2})
}

Output:

(main.X) (len=1) (PANIC=runtime error: invalid memory address or nil pointer dereference){
 (int) 1: (int) 2
}

Panic occures when taking the len() of the receiver x, which is an invalid pointer.

This happens on all versions of go I installed (1.16 to 1.18).

@zavla
Copy link

zavla commented Jul 24, 2022

A workaround is:
func main() {
spew.Dump(&X{1: 2})
}

@chenzhuoyu
Copy link
Author

A workaround is: func main() { spew.Dump(&X{1: 2}) }

Not really. Sometimes the map is not directly accessable, and maybe nested in a deep structure.

@dwlnetnl
Copy link

I've fixed this issue in my fork of the package: spewerspew/spew@62a6506

@dwlnetnl
Copy link

Dupe of #115

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

No branches or pull requests

3 participants