Fix crash for closing shortcode with no .Inner set
authorBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 14 Jun 2020 16:16:45 +0000 (18:16 +0200)
committerBjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com>
Sun, 14 Jun 2020 18:19:08 +0000 (20:19 +0200)
Fixes #6857
Closes #7330

hugolib/shortcode.go
hugolib/shortcode_test.go

index 0d3bfff18e83021a17d333d88d24a764779328da..f5413a9322b547582fe9a8c02d0d83a61ce82d4f 100644 (file)
@@ -496,19 +496,26 @@ Loop:
                case currItem.IsRightShortcodeDelim():
                        // we trust the template on this:
                        // if there's no inner, we're done
-                       if !sc.isInline && !sc.info.ParseInfo().IsInner {
-                               return sc, nil
+                       if !sc.isInline {
+                               if sc.info == nil {
+                                       // This should not happen.
+                                       return sc, fail(errors.New("BUG: template info not set"), currItem)
+                               }
+                               if !sc.info.ParseInfo().IsInner {
+                                       return sc, nil
+                               }
                        }
 
                case currItem.IsShortcodeClose():
                        next := pt.Peek()
-                       if !sc.isInline && !sc.info.ParseInfo().IsInner {
-                               if next.IsError() {
-                                       // return that error, more specific
-                                       continue
+                       if !sc.isInline {
+                               if sc.info == nil || !sc.info.ParseInfo().IsInner {
+                                       if next.IsError() {
+                                               // return that error, more specific
+                                               continue
+                                       }
+                                       return sc, fail(_errors.Errorf("shortcode %q has no .Inner, yet a closing tag was provided", next.Val), next)
                                }
-
-                               return sc, fail(_errors.Errorf("shortcode %q has no .Inner, yet a closing tag was provided", next.Val), next)
                        }
                        if next.IsRightShortcodeDelim() {
                                // self-closing
index d60bdd48bfa870d817bd148f74767c65ed3d1e38..8bb46846512640020b3f00405661a2871d8ff259 100644 (file)
@@ -1316,3 +1316,23 @@ title: "Hugo Rocks!"
        }
 
 }
+
+// https://github.com/gohugoio/hugo/issues/6857
+func TestShortcodeNoInner(t *testing.T) {
+       t.Parallel()
+
+       b := newTestSitesBuilder(t)
+
+       b.WithContent("page.md", `---
+title: "No Inner!"
+---
+{{< noinner >}}{{< /noinner >}}
+
+
+`).WithTemplatesAdded(
+               "layouts/shortcodes/noinner.html", `No inner here.`)
+
+       err := b.BuildE(BuildCfg{})
+       b.Assert(err.Error(), qt.Contains, `failed to extract shortcode: shortcode "noinner" has no .Inner, yet a closing tag was provided`)
+
+}