Skip to content

runtime: detect infinite recursion during stack growth #2556

Closed
@i3d

Description

@i3d
What steps will reproduce the problem?
This simple code reveals an infinite print loop: 
package main 
import "fmt" 
import "log" 
import "runtime/debug" 
var count = 0 
type foo struct { 
  i int 
} 

func (f foo) String() string { 
  count++ 
  if count > 10 { 
    debug.PrintStack() 
    panic("call stack too large") 
  } 
  return fmt.Sprintf("foo@%p value: %d", f, f.i) 
} 

func main() { 
  f := foo{i: 3} 
  // ok 
  log.Println(fmt.Sprintf("foo@%p value: %d", &f, f.i)) 
  // inf loop 
  log.Printf("foo@%p, value: %d\n", f, f.i) 
} 

If you take out the loop protection in the code, you will end up crashing with no system
memory left.

What is the expected output?
There should be a callstack protection so that the program wouldn't chew up all the
system memories.

What do you see instead?
hang and eat up all the memories.

Which compiler are you using (5g, 6g, 8g, gccgo)?
6g, 8g

Which operating system are you using?
Linux 386/amd64

Which revision are you using?  (hg identify)
tip

Please provide any additional information below.

Ian's comment:

I think we should use the depth we are already passing down to catch 
this kind of loop before we eat all of memory.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions