]> git.maquefel.me Git - brevno-suite/hugo/commitdiff
Fix nilpointer on ToC heading
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 4 Sep 2025 09:21:29 +0000 (11:21 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Thu, 4 Sep 2025 12:01:51 +0000 (14:01 +0200)
Fixes #11843

Co-authored-by: Joe Mooring <joe.mooring@veriphor.com>
common/hreflect/helpers.go
common/hreflect/helpers_test.go
markup/tableofcontents/tableofcontents_integration_test.go

index 5453713743f4f4e2edb747f854042692682bb467..abb9df6b15213f1d281658f3ff1d39ef21ba0584 100644 (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()
        }
index cbcad0f22e676fd161b30ef75fae3ce5a157f528..211172d3998868da6e93ce1296bb6141916f0bf8 100644 (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) {
index e6ae03ce2d3d64c3e12f1c2df6f36f2a878811fd..b47cbd245e3d5b5e18c70196d2809f280abfa0e2 100644 (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")
+}