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

fatal error: out of memory when dumping a struct #145

Open zhulik opened 5 months ago

zhulik commented 5 months ago

I've recently faced a weird crash when debugging some tests that use https://github.com/stretchr/testify/mock. It turned out mock uses go-spew under the hood and it causes the tests to crash. I prepared a small program that reproduces the issue:

package main

import (
    "fmt"

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

type Foo struct {
    value string
}

func (u Foo) String() string {
    return u.value
}

type FooWrapper struct {
    *Foo
}

func main() {
    fmt.Println(spew.Sdump(FooWrapper{&Foo{value: "value"}}))
}

When I run the program using go run main.go it crashes with:

runtime: out of memory: cannot allocate 4989544637250142208-byte block (3899392 in use)
fatal error: out of memory

goroutine 1 [running]:
runtime.throw({0x4cbda3?, 0xc00008daa0?})
    /home/user/.asdf/installs/golang/1.21.0/go/src/runtime/panic.go:1077 +0x5c fp=0xc00008d9e8 sp=0xc00008d9b8 pc=0x43209c
runtime.(*mcache).allocLarge(0x3?, 0x453e6c696e3c8000, 0x0?)
    /home/user/.asdf/installs/golang/1.21.0/go/src/runtime/mcache.go:236 +0x176 fp=0xc00008da30 sp=0xc00008d9e8 pc=0x414576
runtime.mallocgc(0x453e6c696e3c8000, 0x0, 0x0)
    /home/user/.asdf/installs/golang/1.21.0/go/src/runtime/malloc.go:1123 +0x4f6 fp=0xc00008da98 sp=0xc00008da30 pc=0x40c556
runtime.rawbyteslice(0x453e6c696e3c6573)
    /home/user/.asdf/installs/golang/1.21.0/go/src/runtime/string.go:274 +0xea fp=0xc00008dae8 sp=0xc00008da98 pc=0x44c0ca
runtime.stringtoslicebyte(0x4b7700?, {0x6c616665756c6176, 0x453e6c696e3c6573})
    /home/user/.asdf/installs/golang/1.21.0/go/src/runtime/string.go:172 +0x46 fp=0xc00008db28 sp=0xc00008dae8 pc=0x44bb26
github.com/davecgh/go-spew/spew.handleMethods(0x57f0a0, {0x4ef5f8, 0xc000108300}, {0x4bb600?, 0xc0001040b0?, 0x4be260?})
    /home/user/golang/pkg/mod/github.com/davecgh/go-spew@v1.1.1/spew/common.go:137 +0x548 fp=0xc00008dc50 sp=0xc00008db28 pc=0x4a5b08
github.com/davecgh/go-spew/spew.(*dumpState).dump(0xc00008de60, {0x4bb600?, 0xc0001040b0?, 0x4be780?})
    /home/user/golang/pkg/mod/github.com/davecgh/go-spew@v1.1.1/spew/dump.go:306 +0x4cc fp=0xc00008de10 sp=0xc00008dc50 pc=0x4a850c
github.com/davecgh/go-spew/spew.fdump(0x57f0a0, {0x4ef5f8, 0xc000108300}, {0xc00008df10?, 0x1, 0x4bb560?})
    /home/user/golang/pkg/mod/github.com/davecgh/go-spew@v1.1.1/spew/dump.go:465 +0x126 fp=0xc00008dea0 sp=0xc00008de10 pc=0x4a9266
github.com/davecgh/go-spew/spew.Sdump({0xc00008df10, 0x1, 0x1})
    /home/user/golang/pkg/mod/github.com/davecgh/go-spew@v1.1.1/spew/dump.go:480 +0x57 fp=0xc00008dee8 sp=0xc00008dea0 pc=0x4a93f7
main.main()
    /home/user/Code/Programs/spew-crash-example/main.go:23 +0x59 fp=0xc00008df40 sp=0xc00008dee8 pc=0x4ab799
runtime.main()
    /home/user/.asdf/installs/golang/1.21.0/go/src/runtime/proc.go:267 +0x2bb fp=0xc00008dfe0 sp=0xc00008df40 pc=0x434a7b
runtime.goexit()
    /home/user/.asdf/installs/golang/1.21.0/go/src/runtime/asm_amd64.s:1650 +0x1 fp=0xc00008dfe8 sp=0xc00008dfe0 pc=0x45ef01
...

Some additional notes:

Go version: 1.21.0 OS: Ubuntu 22.04.3 LTS and macOS 14