davecgh / go-spew

Implements a deep pretty printer for Go data structures to aid in debugging
ISC License
5.98k stars 361 forks source link

Panic on customized Stringer of map #115

Open wwwjfy opened 4 years ago

wwwjfy commented 4 years ago

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.

jrick commented 4 years ago

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.

dwlnetnl commented 1 year ago

I've fixed this in my fork of the package: https://github.com/spewerspew/spew/commit/62a6506637ab035cd8626d40cf3d7f74d8dcd4d6