You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Infinite recursion can also be triggered by self-referential data structures in "fmt" pkg, 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.
It's true that fmt doesn't try to protect against cyclic data structures, but I think it's reasonable for pretty to be more defensive and to do something about it.
Maybe we should have a limit on depth of traversal.
If so, I guess there should also be a way for the caller to adjust the limit in cases where that's necessary. For example, maybe the default limit is 1000, and most callers never hit it. But if you have a cycle, it will prevent a crash. If you have a deeply nested non-cyclic structure and you want to print all of it, you can increase the limit. Or if you're in a resource-constrained environment, you can lower the limit.
But hopefully almost everyone would never have to adjust the limit or even know it exists.
zyga
pushed a commit
to zyga/spread
that referenced
this issue
Jul 4, 2021
Gustavo's fork (github.com/niemeyer/pretty) contains additional fixes on
recursive elements, so that they don't hang the printer. This is tracked
as issue kr/pretty#72
While the extra patches in the fork are nice, they are not essential and
not needing the fork makes packaging easier.
Signed-off-by: Zygmunt Krynicki <zygmunt.krynicki@huawei.com>
package main
import (
"fmt"
)
func main() {
type MyStruct struct {
S []MyStruct
str string
}
}
it will fatal error: stack overflow
The text was updated successfully, but these errors were encountered: