flosch / pongo2

Django-syntax like template-engine for Go
https://www.schlachter.tech/pongo2
MIT License
2.86k stars 269 forks source link

ifchanged panic if there's no else #237

Open mlaventure opened 4 years ago

mlaventure commented 4 years ago

I am not sure what's special about my template code, but I get this panic when trying to use ifchanged without an else close.

I fixed it locally by check if node.elseWrapper != nil on tags_ifchanged.go:59 but I didn't dig into the details to figure out why it can happens when the similar code in the template_tests directory seems to do just fine.

This was on go version go1.14 linux/amd64, the relevant panic part was:

panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x18 pc=0x5253f6]

goroutine 1 [running]:
github.com/flosch/pongo2.(*NodeWrapper).Execute(0x0, 0xc0003a9980, 0x72da60, 0xc0003a98
90, 0x0)
        /home/mlaventure/go/pkg/mod/github.com/flosch/pongo2@v0.0.0-20200529170236-5aba
cdfa4915/nodes_wrapper.go:9 +0x26
github.com/flosch/pongo2.(*tagIfchangedNode).Execute(0xc0000130e0, 0xc0003a9980, 0x72da
60, 0xc0003a9890, 0x0)
        /home/mlaventure/go/pkg/mod/github.com/flosch/pongo2@v0.0.0-20200529170236-5aba
cdfa4915/tags_ifchanged.go:61 +0x404
github.com/flosch/pongo2.(*NodeWrapper).Execute(0xc0003a8ff0, 0xc0003a9980, 0x72da60, 0
xc0003a9890, 0xc0003a1f08)
        /home/mlaventure/go/pkg/mod/github.com/flosch/pongo2@v0.0.0-20200529170236-5aba
cdfa4915/nodes_wrapper.go:10 +0x84
github.com/flosch/pongo2.(*tagForNode).Execute.func1(0x1, 0x36, 0xc000361c20, 0x0, 0x6a
a801)
        /home/mlaventure/go/pkg/mod/github.com/flosch/pongo2@v0.0.0-20200529170236-5aba
cdfa4915/tags_for.go:67 +0x125
github.com/flosch/pongo2.(*Value).IterateOrder(0xc000361be0, 0xc00017f7a0, 0xc00017f770
, 0x0)
        /home/mlaventure/go/pkg/mod/github.com/flosch/pongo2@v0.0.0-20200529170236-5aba
cdfa4915/value.go:436 +0x33c
github.com/flosch/pongo2.(*tagForNode).Execute(0xc0003d21e0, 0xc0003a9920, 0x72da60, 0x
c0003a9890, 0x0)
        /home/mlaventure/go/pkg/mod/github.com/flosch/pongo2@v0.0.0-20200529170236-5aba
cdfa4915/tags_for.go:47 +0x276
github.com/flosch/pongo2.(*nodeDocument).Execute(0xc0003617c0, 0xc0003a9920, 0x72da60, 
0xc0003a9890, 0x0)
        /home/mlaventure/go/pkg/mod/github.com/flosch/pongo2@v0.0.0-20200529170236-5aba
cdfa4915/nodes.go:10 +0x83
github.com/flosch/pongo2.(*Template).execute(0xc00012c3f0, 0xc0003a8c90, 0x72da60, 0xc0
003a9890, 0x8f8760, 0x7f30808f7108)
        /home/mlaventure/go/pkg/mod/github.com/flosch/pongo2@v0.0.0-20200529170236-5aba
cdfa4915/template.go:173 +0x7b
github.com/flosch/pongo2.(*Template).newBufferAndExecute(0xc00012c3f0, 0xc0003a8c90, 0x
2, 0x6902a0, 0x1)
        /home/mlaventure/go/pkg/mod/github.com/flosch/pongo2@v0.0.0-20200529170236-5aba
cdfa4915/template.go:188 +0xef
github.com/flosch/pongo2.(*Template).ExecuteWriter(0xc00012c3f0, 0xc0003a8c90, 0x72c5c0
, 0xc0003a8e10, 0x0, 0x0)
        /home/mlaventure/go/pkg/mod/github.com/flosch/pongo2@v0.0.0-20200529170236-5aba
cdfa4915/template.go:198 +0x39
flosch commented 4 years ago

Can you provide a minimal template which panics? This helps me debugging. Thanks!

mlaventure commented 4 years ago

Can you provide a minimal template which panics? This helps me debugging. Thanks!

I figured, I've been trying to find one before opening a PR, I couldn't so I opened this issue instead. We'll keep trying, I may have had an idea of the difference between my current template and the one in the tests

mlaventure commented 4 years ago

Got it: It's when within a double loop:

{% for comment in complex.comments2 %}
    {% for comment2 in complex.comments2 %}
        {% ifchanged comment2.author.validated %}
              // This will panic
        {% endifchanged %}
    {% endfor %}
{% endfor %}
flosch commented 4 years ago

Thanks! Will have a look soon.