kr / diff

Print differences between two Go values.

Home Page:https://kr.dev/diff

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

stack overflow when transforming from T to slice/struct with a T

bmizerany opened this issue · comments

These tests fail due to stack overflows:

func TestSOs(t *testing.T) {     
        t.Run("T to []T", func(t *testing.T) {
                diff.Test(t, t.Errorf, "a", "b", diff.Transform(func(s string) any { 
                        return []string{s}     
                }))              
        })                  
        t.Run("T to struct{V T}", func(t *testing.T) {
                diff.Test(t, t.Errorf, "a", "b", diff.Transform(func(s string) any { 
                        return struct{ S string }{s}
                }))                            
        })                                    
} 

The correct solution to this is "don't do that." but leaving up for @kr to close at will.

commented

Yeah, this is a recursive transform, which is fine; the problem is it specifies an unbounded recursion. The transform says "convert each string into a slice of strings to be compared". The diff function then traverses the slice, and needs to compare the contents, which leads to the transform being applied again on the string inside the slice, which yields another slice of strings. This process repeats indefinitely.

There wouldn't be a problem if at some point this transform returned a different type or an empty slice. It eventually needs to return something that doesn't result in itself being applied again at a later step. More precisely, it needs to (eventually) return a value that doesn't contain a string value anywhere, since string is the type it accepts as input.

There's no easy way for diff to guard against this problem, since recursive transforms in general are ok. They just need to bottom out somewhere. This is analogous to a programming language allowing recursive functions. There's no good way to statically prevent unbounded recursion; the best you can do is report an error at runtime if the stack gets too large. That's what we do too.

commented

Having said all that, it would be worth mentioning this in the docs somewhere.