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

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

Open
zhulik opened this issue Jan 17, 2024 · 0 comments
Open

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

zhulik opened this issue Jan 17, 2024 · 0 comments

Comments

@zhulik
Copy link

zhulik commented Jan 17, 2024

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:

  • It I try to print out len(u.value) inside String() before returning it, I actually see 4989544637250142208 in the output.
  • it a assign value an empty string instead of "value", it does not crash, instead I see this output:
(main.FooWrapper) (PANIC=runtime error: invalid memory address or nil pointer dereference){
 Foo: (*main.Foo)(0xc000014100)()
}
  • if I try to print out u.value inside String() before returning it(fmt.Println(u.value)), it does not crash, instead I see this output:
value
(main.FooWrapper) (PANIC=runtime error: growslice: len out of range){
 Foo: (*main.Foo)(0xc0000960b0)(value)
}

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

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

1 participant