New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Check eq vs nil #5043

Closed
hach-que opened this Issue Aug 7, 2018 · 7 comments

Comments

Projects
None yet
3 participants
@hach-que

hach-que commented Aug 7, 2018

I have a shortcode like this:

{{- $page := .Site.GetPage (.Get 0) -}}
{{- if eq $page nil -}}
{{- errorf "ERROR: page is nil %s" (.Get 0) -}}
{{- else -}}
[{{- $page.Title -}}]({{- ref . (.Get 0) -}})
{{- end -}}

which is used like this:

{{< linkref "./authenticating-users" >}}

If the target file doesn't exist, Hugo crashes with this stack trace:

Building sites … panic: runtime error: invalid memory address or nil pointer dereference [recovered]
        panic: runtime error: invalid memory address or nil pointer dereference
[signal 0xc0000005 code=0x0 addr=0x308 pc=0xc694c5]

goroutine 1085 [running]:
text/template.errRecover(0xc0499737f0)
        /usr/local/go/src/text/template/exec.go:137 +0x1db
panic(0xddca80, 0x15af010)
        /usr/local/go/src/runtime/panic.go:502 +0x237
github.com/gohugoio/hugo/hugolib.(*Page).Title(0x0, 0x0, 0x0)
        /go/src/github.com/gohugoio/hugo/hugolib/page.go:1183 +0x5
reflect.Value.call(0xec0d00, 0xc0451746e0, 0x12e93, 0xec6a2b, 0x4, 0x15f07e0, 0x0, 0x0, 0xebb520, 0x1, ...)
        /usr/local/go/src/reflect/value.go:447 +0x970
reflect.Value.Call(0xec0d00, 0xc0451746e0, 0x12e93, 0x15f07e0, 0x0, 0x0, 0xc04712f730, 0x13, 0x4b)
        /usr/local/go/src/reflect/value.go:308 +0xab
text/template.(*state).evalCall(0xc049973770, 0xe17d40, 0xc045587950, 0x16, 0xec0d00, 0xc0451746e0, 0x12e93, 0x10a3d00, 0xc04286a870, 0xc0424ab1a6, ...)
        /usr/local/go/src/text/template/exec.go:667 +0x4f8
text/template.(*state).evalField(0xc049973770, 0xe17d40, 0xc045587950, 0x16, 0xc0424ab1a6, 0x5, 0x10a3d00, 0xc04286a870, 0xc0428646e0, 0x1, ...)
        /usr/local/go/src/text/template/exec.go:557 +0xcb4
text/template.(*state).evalFieldChain(0xc049973770, 0xe17d40, 0xc045587950, 0x16, 0xec0d00, 0xc0451746e0, 0x96, 0x10a3d00, 0xc04286a870, 0xc042857610, ...)
        /usr/local/go/src/text/template/exec.go:525 +0x227
text/template.(*state).evalVariableNode(0xc049973770, 0xe17d40, 0xc045587950, 0x16, 0xc04286a870, 0xc0428646e0, 0x1, 0x1, 0x0, 0x0, ...)
        /usr/local/go/src/text/template/exec.go:513 +0x199
text/template.(*state).evalCommand(0xc049973770, 0xe17d40, 0xc045587950, 0x16, 0xc04268b620, 0x0, 0x0, 0x0, 0xc049973470, 0x58d57d, ...)
        /usr/local/go/src/text/template/exec.go:437 +0x718
text/template.(*state).evalPipeline(0xc049973770, 0xe17d40, 0xc045587950, 0x16, 0xc0425ddea0, 0x0, 0x0, 0xc04286a750)
        /usr/local/go/src/text/template/exec.go:405 +0x116
text/template.(*state).walk(0xc049973770, 0xe17d40, 0xc045587950, 0x16, 0x10a38c0, 0xc04286a8a0)
        /usr/local/go/src/text/template/exec.go:231 +0x4ac
text/template.(*state).walk(0xc049973770, 0xe17d40, 0xc045587950, 0x16, 0x10a3b00, 0xc04268b5c0)
        /usr/local/go/src/text/template/exec.go:239 +0x124
text/template.(*state).walkIfOrWith(0xc049973770, 0xa, 0xe17d40, 0xc045587950, 0x16, 0xc0428665f0, 0xc04286a7e0, 0xc04268b5c0)
        /usr/local/go/src/text/template/exec.go:272 +0x223
text/template.(*state).walk(0xc049973770, 0xe17d40, 0xc045587950, 0x16, 0x10a3ac0, 0xc0428ee600)
        /usr/local/go/src/text/template/exec.go:236 +0x315
text/template.(*state).walk(0xc049973770, 0xe17d40, 0xc045587950, 0x16, 0x10a3b00, 0xc0428ed860)
        /usr/local/go/src/text/template/exec.go:239 +0x124
text/template.(*Template).execute(0xc0428ee540, 0x10966e0, 0xc04b06d180, 0xe17d40, 0xc045587950, 0x0, 0x0)
        /usr/local/go/src/text/template/exec.go:194 +0x1ed
text/template.(*Template).Execute(0xc0428ee540, 0x10966e0, 0xc04b06d180, 0xe17d40, 0xc045587950, 0xc0450640c0, 0x460247)
        /usr/local/go/src/text/template/exec.go:177 +0x5a
html/template.(*Template).Execute(0xc0428ed800, 0x10966e0, 0xc04b06d180, 0xe17d40, 0xc045587950, 0x42b050, 0x8)
        /usr/local/go/src/html/template/template.go:122 +0x93
github.com/gohugoio/hugo/tpl.(*TemplateAdapter).Execute(0xc046153c40, 0x10966e0, 0xc04b06d180, 0xe17d40, 0xc045587950, 0x0, 0x0)
        /go/src/github.com/gohugoio/hugo/tpl/template.go:94 +0x7d
github.com/gohugoio/hugo/hugolib.renderShortcodeWithPage(0x109a080, 0xc046153c40, 0xc045587950, 0x0, 0x0)
        /go/src/github.com/gohugoio/hugo/hugolib/shortcode.go:759 +0xcd
github.com/gohugoio/hugo/hugolib.renderShortcode(0xec5204, 0x2, 0xec6ba7, 0x4, 0xec778f, 0x4, 0xc044a85e60, 0x25, 0xc044b552c0, 0x0, ...)
        /go/src/github.com/gohugoio/hugo/hugolib/shortcode.go:361 +0x549
github.com/gohugoio/hugo/hugolib.prepareShortcodeForPage.func1(0xc044029040, 0xe42180, 0xc0440291c0, 0xc044191bc0)
        /go/src/github.com/gohugoio/hugo/hugolib/shortcode.go:281 +0x5e
github.com/gohugoio/hugo/hugolib.(*shortcodeHandler).executeShortcodesForDelta(0xc0432cd9a0, 0xc04299ae30, 0x1b, 0xc042743d98)
        /go/src/github.com/gohugoio/hugo/hugolib/shortcode.go:442 +0xc8
github.com/gohugoio/hugo/hugolib.handleShortcodes(0xc04299ae30, 0xc0430d6b00, 0x2a3, 0x2a3, 0x2a3, 0x2a3, 0xc042743f50, 0xc043ffd080, 0xc0480d9a00)
        /go/src/github.com/gohugoio/hugo/hugolib/hugo_sites.go:610 +0x210
github.com/gohugoio/hugo/hugolib.(*Page).prepareForRender(0xc044e30500, 0xf7b530, 0xc044359ef0)
        /go/src/github.com/gohugoio/hugo/hugolib/page.go:1268 +0x131
github.com/gohugoio/hugo/hugolib.(*Page).initContent.func1.1(0xc044e30500, 0xc0492c4a80)
        /go/src/github.com/gohugoio/hugo/hugolib/page.go:303 +0x91
created by github.com/gohugoio/hugo/hugolib.(*Page).initContent.func1
        /go/src/github.com/gohugoio/hugo/hugolib/page.go:298 +0xf4

Hugo version: Hugo Static Site Generator v0.45 windows/amd64 BuildDate: 2018-07-22T12:10:05Z

@bep

This comment has been minimized.

Member

bep commented Aug 7, 2018

Please use https://discourse.gohugo.io/ for questions/troubleshooting. Also see Hugo Documentation.

@bep bep closed this Aug 7, 2018

@hach-que

This comment has been minimized.

hach-que commented Aug 7, 2018

Huh? This is a bug report, not a question.

@hach-que

This comment has been minimized.

hach-que commented Aug 7, 2018

From the Contributing guide:

If you believe you have found a defect in Hugo or its documentation, use the GitHub issue tracker to report the problem to the Hugo maintainers.

@bep

This comment has been minimized.

Member

bep commented Aug 7, 2018

Sure, if it is a bug, we will reopen this issue. Open a thread on the forum and we can trouble shoot it there.

@bep

This comment has been minimized.

Member

bep commented Aug 7, 2018

I'll reopen to check if there is something fishy with eq and nil checking, but this should work:

{{- $page := .Site.GetPage (.Get 0) -}}
{{- if not $page -}}
{{- errorf "ERROR: page is nil %s" (.Get 0) -}}
{{- else -}}
[{{- $page.Title -}}]({{- ref . (.Get 0) -}})
{{- end -}}

@bep bep reopened this Aug 7, 2018

@bep bep changed the title from Hugo crashes when referencing a non-existent page to Check eq vs nil Aug 7, 2018

@bep

This comment has been minimized.

Member

bep commented Aug 7, 2018

Of possibly:

{{- with  .Site.GetPage (.Get 0)  -}}
[{{- .Title -}}]({{ .RelPernalink}})
{{- else -}}
{{- errorf "ERROR: page is nil %s" (.Get 0) -}}
{{- end -}}
@hach-que

This comment has been minimized.

hach-que commented Aug 7, 2018

Both the with and not variants work (they don't crash and correctly render the error).

@moorereason moorereason added Bug and removed NeedsInvestigation labels Oct 3, 2018

moorereason added a commit to moorereason/hugo that referenced this issue Oct 3, 2018

hugolib: Allow nil to be unwrapped as *Page
Previously, calls to *Page.Eq(nil) would always return false because the
unwrapPage func didn't support the nil case.  Add support for unwrapping
nil to a *Page.

Fixes gohugoio#5043

@bep bep added this to the v0.50 milestone Oct 3, 2018

@bep bep closed this in #5280 Oct 3, 2018

bep added a commit that referenced this issue Oct 3, 2018

hugolib: Allow nil to be unwrapped as *Page
Previously, calls to *Page.Eq(nil) would always return false because the
unwrapPage func didn't support the nil case.  Add support for unwrapping
nil to a *Page.

Fixes #5043
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment