Fix nilpointer on ToC heading

Fixes #11843

Co-authored-by: Joe Mooring <joe.mooring@veriphor.com>
This commit is contained in:
Bjørn Erik Pedersen 2025-09-04 11:21:29 +02:00
parent b8eb45c9df
commit 4f2d2b2cc4
3 changed files with 33 additions and 0 deletions

View File

@ -101,6 +101,10 @@ func IsTruthfulValue(val reflect.Value) (truth bool) {
return
}
if val.Kind() == reflect.Pointer && val.IsNil() {
return
}
if val.Type().Implements(zeroType) {
return !val.Interface().(types.Zeroer).IsZero()
}

View File

@ -22,13 +22,29 @@ import (
qt "github.com/frankban/quicktest"
)
type zeroStruct struct {
zero bool
}
func (z zeroStruct) IsZero() bool {
return z.zero
}
func TestIsTruthful(t *testing.T) {
c := qt.New(t)
var nilpointerZero *zeroStruct
c.Assert(IsTruthful(true), qt.Equals, true)
c.Assert(IsTruthful(false), qt.Equals, false)
c.Assert(IsTruthful(time.Now()), qt.Equals, true)
c.Assert(IsTruthful(time.Time{}), qt.Equals, false)
c.Assert(IsTruthful(&zeroStruct{zero: false}), qt.Equals, true)
c.Assert(IsTruthful(&zeroStruct{zero: true}), qt.Equals, false)
c.Assert(IsTruthful(zeroStruct{zero: false}), qt.Equals, true)
c.Assert(IsTruthful(zeroStruct{zero: true}), qt.Equals, false)
c.Assert(IsTruthful(nil), qt.Equals, false)
c.Assert(IsTruthful(nilpointerZero), qt.Equals, false)
}
func TestGetMethodByName(t *testing.T) {

View File

@ -121,3 +121,16 @@ CONTENT
b, _ = hugolib.TestE(t, files)
b.AssertLogMatches(`error calling ToHTML: startLevel: unable to cast "x" of type string`)
}
func TestHeadingsNilpointerIssue11843(t *testing.T) {
t.Parallel()
files := `
-- hugo.toml --
-- layouts/home.html --
{{ $h := index .Fragments.HeadingsMap "bad_id" }}
{{ if not $h }}OK{{ end }}
`
b := hugolib.Test(t, files)
b.AssertFileContent("public/index.html", "OK")
}