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

fmt: Printf("%#v", v) stack overflow when print nested data #13429

Closed
bg5sbk opened this issue Nov 30, 2015 · 2 comments
Closed

fmt: Printf("%#v", v) stack overflow when print nested data #13429

bg5sbk opened this issue Nov 30, 2015 · 2 comments
Milestone

Comments

@bg5sbk
Copy link

@bg5sbk bg5sbk commented Nov 30, 2015

The code:

package main

import "fmt"

func main() {
    var a = make(map[int]interface{})
    a[1] = a
    fmt.Printf("%#v", a)
}

Error message:

runtime: goroutine stack exceeds 250000000-byte limit
fatal error: stack overflow

runtime stack:
runtime.throw(0x1710e0, 0xe)
    /usr/local/go/src/runtime/panic.go:527 +0x100
runtime.newstack()
    /usr/local/go/src/runtime/stack1.go:794 +0x1000
runtime.morestack()
    /usr/local/go/src/runtime/asm_amd64p32.s:283 +0xa0

goroutine 1 [stack growth]:
fmt.(*pp).printReflectValue(0x1045a120, 0x138900, 0x124e6620, 0x42, 0x76, 0x1ca49, 0x185005c0, 0x0)
    /usr/local/go/src/fmt/print.go:855 fp=0x18500558 sp=0x18500550
fmt.(*pp).printValue(0x1045a120, 0x138900, 0x124e6620, 0x42, 0x76, 0x1ca49, 0x1, 0x0)
    /usr/local/go/src/fmt/print.go:848 +0x4e0 fp=0x185005e0 sp=0x18500558
fmt.(*pp).printReflectValue(0x1045a120, 0x138ec0, 0x104342e0, 0x15, 0x76, 0x1ca48, 0x0, 0x41)
    /usr/local/go/src/fmt/print.go:902 +0x2e80 fp=0x185009d0 sp=0x185005e0
fmt.(*pp).printValue(0x1045a120, 0x138ec0, 0x104342e0, 0x15, 0x76, 0x1ca48, 0x15, 0x0)
    /usr/local/go/src/fmt/print.go:848 +0x4e0 fp=0x18500a58 sp=0x185009d0
fmt.(*pp).printReflectValue(0x1045a120, 0x138740, 0x104eea20, 0x54, 0x76, 0x1ca47, 0x33100, 0x66b9)
    /usr/local/go/src/fmt/print.go:945 +0x2680 fp=0x18500e48 sp=0x18500a58
fmt.(*pp).printValue(0x1045a120, 0x138740, 0x104eea20, 0x54, 0x76, 0x1ca47, 0x138740, 0x104eea20)
    /usr/local/go/src/fmt/print.go:848 +0x4e0 fp=0x18500ed0 sp=0x18500e48
fmt.(*pp).printReflectValue(0x1045a120, 0x138ec0, 0x104342e0, 0x15, 0x76, 0x1ca46, 0x0, 0x41)
    /usr/local/go/src/fmt/print.go:904 +0x2fc0 fp=0x185012c0 sp=0x18500ed0
fmt.(*pp).printValue(0x1045a120, 0x138ec0, 0x104342e0, 0x15, 0x76, 0x1ca46, 0x15, 0x0)
    /usr/local/go/src/fmt/print.go:848 +0x4e0 fp=0x18501348 sp=0x185012c0
fmt.(*pp).printReflectValue(0x1045a120, 0x138740, 0x104eea18, 0x54, 0x76, 0x1ca45, 0x33100, 0x66b9)
    /usr/local/go/src/fmt/print.go:945 +0x2680 fp=0x18501738 sp=0x18501348
fmt.(*pp).printValue(0x1045a120, 0x138740, 0x104eea18, 0x54, 0x76, 0x1ca45, 0x138740, 0x104eea18)
    /usr/local/go/src/fmt/print.go:848 +0x4e0 fp=0x185017c0 sp=0x18501738
fmt.(*pp).printReflectValue(0x1045a120, 0x138ec0, 0x104342e0, 0x15, 0x76, 0x1ca44, 0x0, 0x41)
    /usr/local/go/src/fmt/print.go:904 +0x2fc0 fp=0x18501bb0 sp=0x185017c0
fmt.(*pp).printValue(0x1045a120, 0x138ec0, 0x104342e0, 0x15, 0x76, 0x1ca44, 0x15, 0x0)
    /usr/local/go/src/fmt/print.go:848 +0x4e0 fp=0x18501c38 sp=0x18501bb0

I think fmt.Printf() can learn from reflect.DeepEqual(), use a make(map[visit]bool) to avoid infinite loop on nested data.

@ianlancetaylor ianlancetaylor changed the title fmt.Printf("%#v", v) stack overflow when print nested data fmt: Printf("%#v", v) stack overflow when print nested data Nov 30, 2015
@ianlancetaylor ianlancetaylor added this to the Unplanned milestone Nov 30, 2015
@ALTree
Copy link
Member

@ALTree ALTree commented Nov 30, 2015

This is #8241 and this behaviour is documented

Infinite recursion can also be triggered by self-referential data structures, such as a slice that contains itself as an element, if that type has a String method. Such pathologies are rare, however, and the package does not protect against them.

here.

@ianlancetaylor
Copy link
Contributor

@ianlancetaylor ianlancetaylor commented Nov 30, 2015

Closing as dup.

@golang golang locked and limited conversation to collaborators Dec 1, 2016
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Linked pull requests

Successfully merging a pull request may close this issue.

None yet
4 participants
You can’t perform that action at this time.