Skip to content

Commit 4f2d2b2

Browse files
bepjmooring
andcommitted
Fix nilpointer on ToC heading
Fixes #11843 Co-authored-by: Joe Mooring <joe.mooring@veriphor.com>
1 parent b8eb45c commit 4f2d2b2

File tree

3 files changed

+33
-0
lines changed

3 files changed

+33
-0
lines changed

‎common/hreflect/helpers.go‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ func IsTruthfulValue(val reflect.Value) (truth bool) {
101101
return
102102
}
103103

104+
if val.Kind() == reflect.Pointer && val.IsNil() {
105+
return
106+
}
107+
104108
if val.Type().Implements(zeroType) {
105109
return !val.Interface().(types.Zeroer).IsZero()
106110
}

‎common/hreflect/helpers_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,29 @@ import (
2222
qt "github.com/frankban/quicktest"
2323
)
2424

25+
type zeroStruct struct {
26+
zero bool
27+
}
28+
29+
func (z zeroStruct) IsZero() bool {
30+
return z.zero
31+
}
32+
2533
func TestIsTruthful(t *testing.T) {
2634
c := qt.New(t)
2735

36+
var nilpointerZero *zeroStruct
37+
2838
c.Assert(IsTruthful(true), qt.Equals, true)
2939
c.Assert(IsTruthful(false), qt.Equals, false)
3040
c.Assert(IsTruthful(time.Now()), qt.Equals, true)
3141
c.Assert(IsTruthful(time.Time{}), qt.Equals, false)
42+
c.Assert(IsTruthful(&zeroStruct{zero: false}), qt.Equals, true)
43+
c.Assert(IsTruthful(&zeroStruct{zero: true}), qt.Equals, false)
44+
c.Assert(IsTruthful(zeroStruct{zero: false}), qt.Equals, true)
45+
c.Assert(IsTruthful(zeroStruct{zero: true}), qt.Equals, false)
46+
c.Assert(IsTruthful(nil), qt.Equals, false)
47+
c.Assert(IsTruthful(nilpointerZero), qt.Equals, false)
3248
}
3349

3450
func TestGetMethodByName(t *testing.T) {

‎markup/tableofcontents/tableofcontents_integration_test.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,3 +121,16 @@ CONTENT
121121
b, _ = hugolib.TestE(t, files)
122122
b.AssertLogMatches(`error calling ToHTML: startLevel: unable to cast "x" of type string`)
123123
}
124+
125+
func TestHeadingsNilpointerIssue11843(t *testing.T) {
126+
t.Parallel()
127+
files := `
128+
-- hugo.toml --
129+
-- layouts/home.html --
130+
{{ $h := index .Fragments.HeadingsMap "bad_id" }}
131+
{{ if not $h }}OK{{ end }}
132+
`
133+
b := hugolib.Test(t, files)
134+
135+
b.AssertFileContent("public/index.html", "OK")
136+
}

0 commit comments

Comments
 (0)