davecgh / go-spew

Implements a deep pretty printer for Go data structures to aid in debugging

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Panic on customized Stringer of map

wwwjfy opened this issue · comments

Hi, I've come across this issue, that with the pointer method feature, the address is updated to 0x1.

Demonstration:

package main

import (
	"fmt"

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

type myMap map[int64]int64

func (c myMap) String() string {
	fmt.Printf("pointer changed: %p\n", c)
	for k := range c {
		fmt.Println(k)
	}

	return "custom string"
}

func main() {
	spew.Printf("%v\n", myMap{1: 1})
}

It gives me

$ go run main.go
pointer changed: 0x1
(PANIC=runtime error: invalid memory address or nil pointer dereference)map[1:1]

With spew.Config.DisablePointerMethods = true, the panic is gone.

This is also fixed by building with the safe build tag. Appears to be a memory unsafety issue introduced by spew making some assumptions about the newer Go release.

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