gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.73k stars 7.53k forks source link

Crash when constant shortcode #7379

Closed jernst closed 4 years ago

jernst commented 4 years ago

What version of Hugo are you using (hugo version)?

$ hugo version
Hugo Static Site Generator v0.72.0/extended darwin/amd64 BuildDate: unknown

From brew.

Does this issue reproduce with the latest release?

Yes.

Steps to reproduce

Workaround

Stack trace

Building sites … panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x20 pc=0x4bed0a2]

goroutine 207 [running]:
github.com/gohugoio/hugo/hugolib.(*shortcodeHandler).extractShortcode(0xc000af9240, 0x1, 0x0, 0xc0008c3e00, 0x0, 0x0, 0x559fd20)
    /private/tmp/hugo-20200531-14626-1z0dbx1/hugo-0.72.0/src/github.com/gohugoio/hugo/hugolib/shortcode.go:505 +0x882
github.com/gohugoio/hugo/hugolib.(*pageState).mapContent(0xc000c9f800, 0xc000ca1f80, 0xc0004cb680, 0xc000dc3a20, 0x5eb8de0)
    /private/tmp/hugo-20200531-14626-1z0dbx1/hugo-0.72.0/src/github.com/gohugoio/hugo/hugolib/page.go:792 +0xb80
github.com/gohugoio/hugo/hugolib.(*pageMap).newPageFromContentNode(0xc000a05c90, 0xc000528660, 0xc000ca1f80, 0x0, 0x0, 0x0, 0x0)
    /private/tmp/hugo-20200531-14626-1z0dbx1/hugo-0.72.0/src/github.com/gohugoio/hugo/hugolib/content_map_page.go:164 +0x625
github.com/gohugoio/hugo/hugolib.(*pageMap).assemblePages.func1(0xc000ad4a20, 0x20, 0x566d120, 0xc000528660, 0xc000c1fd00)
    /private/tmp/hugo-20200531-14626-1z0dbx1/hugo-0.72.0/src/github.com/gohugoio/hugo/hugolib/content_map_page.go:363 +0x150
github.com/armon/go-radix.recursiveWalk(0xc000528690, 0xc000c1fe00, 0x4821800)
    /Users/brew/Library/Caches/Homebrew/go_cache/pkg/mod/github.com/armon/go-radix@v1.0.0/radix.go:519 +0xf0
github.com/armon/go-radix.recursiveWalk(0xc000528420, 0xc000c1fe00, 0x4bc6000)
    /Users/brew/Library/Caches/Homebrew/go_cache/pkg/mod/github.com/armon/go-radix@v1.0.0/radix.go:525 +0x7e
github.com/armon/go-radix.recursiveWalk(0xc000528390, 0xc000c1fe00, 0x0)
    /Users/brew/Library/Caches/Homebrew/go_cache/pkg/mod/github.com/armon/go-radix@v1.0.0/radix.go:525 +0x7e
github.com/armon/go-radix.recursiveWalk(0xc000a0cae0, 0xc000c1fe00, 0x0)
    /Users/brew/Library/Caches/Homebrew/go_cache/pkg/mod/github.com/armon/go-radix@v1.0.0/radix.go:525 +0x7e
github.com/armon/go-radix.(*Tree).Walk(...)
    /Users/brew/Library/Caches/Homebrew/go_cache/pkg/mod/github.com/armon/go-radix@v1.0.0/radix.go:447
github.com/gohugoio/hugo/hugolib.(*pageMap).assemblePages(0xc000a05c90, 0x0, 0x0)
    /private/tmp/hugo-20200531-14626-1z0dbx1/hugo-0.72.0/src/github.com/gohugoio/hugo/hugolib/content_map_page.go:336 +0xec
github.com/gohugoio/hugo/hugolib.(*pageMaps).AssemblePages.func1(0xc000a05c90, 0x0, 0x0)
    /private/tmp/hugo-20200531-14626-1z0dbx1/hugo-0.72.0/src/github.com/gohugoio/hugo/hugolib/content_map_page.go:717 +0x68
github.com/gohugoio/hugo/hugolib.(*pageMaps).withMaps.func1(0x0, 0x0)
    /private/tmp/hugo-20200531-14626-1z0dbx1/hugo-0.72.0/src/github.com/gohugoio/hugo/hugolib/content_map_page.go:785 +0x2e
github.com/gohugoio/hugo/common/para.(*errGroupRunner).Run.func1(0xc000770790, 0x1)
    /private/tmp/hugo-20200531-14626-1z0dbx1/hugo-0.72.0/src/github.com/gohugoio/hugo/common/para/para.go:52 +0x2f
golang.org/x/sync/errgroup.(*Group).Go.func1(0xc000a93b30, 0xc000ac4840)
    /Users/brew/Library/Caches/Homebrew/go_cache/pkg/mod/golang.org/x/sync@v0.0.0-20190911185100-cd5d95a43a6e/errgroup/errgroup.go:57 +0x59
created by golang.org/x/sync/errgroup.(*Group).Go
    /Users/brew/Library/Caches/Homebrew/go_cache/pkg/mod/golang.org/x/sync@v0.0.0-20190911185100-cd5d95a43a6e/errgroup/errgroup.go:54 +0x66
bep commented 4 years ago

Wat exactly do you mean by "constant"?

jernst commented 4 years ago

"constant" as in: the result of the shortcode is constant regardless of its "environment". So the value does not depend on .Inner or the page context or the front matter or whatever.

use case: I have some text in my .md file (like a comment, or something that isn't entirely baked yet) that I don't want to show on the site, but keep in the .md file. In my example above, regardless of content, it would evaluate to "XXX".

jmooring commented 4 years ago

I suspect this is a duplicate of https://github.com/gohugoio/hugo/issues/6857.

Fail

layouts/shortcodes/foo.html

foo

content/post/bar.md

{{< foo >}}{{< /foo >}}

Pass

layouts/shortcodes/foo.html

foo

content/post/bar.md

{{< foo >}}

Pass

layouts/shortcodes/foo.html

{{ $noop := .Inner }}
foo

content/post/bar.md

{{< foo >}}{{< /foo >}}

Notes

To reliably reproduce you should run hugo after making changes. If you make changes while running hugo server the error may not occur.

bep commented 4 years ago

OK, but note that the above is supposed to fail (but not crash).

jernst commented 4 years ago

Whis is "the above supposed to fail"? I'd like to be able to use this construct to "comment out" some text, among other use cases. Or am I misunderstanding?

bep commented 4 years ago

Any closing shortcode needs a .Inner reference (which is partly how we identify it as such).

You can do what you want if you just add $foo := .Inner or something in the shortcode template.

github-actions[bot] commented 2 years ago

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.