Skip to content

Commit

Permalink
markup/goldmark: Fix data race in the hugocontext wrapper
Browse files Browse the repository at this point in the history
The window for this to happen is very small, but it has been reported by Go's race detector (-race flag) in a tests once.
  • Loading branch information
bep committed Apr 22, 2024
1 parent 2d75f53 commit 509ab08
Show file tree
Hide file tree
Showing 3 changed files with 4 additions and 4 deletions.
2 changes: 1 addition & 1 deletion hugolib/page__per_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ func (pco *pageContentOutput) RenderShortcodes(ctx context.Context) (template.HT
// This content will be parsed and rendered by Goldmark.
// Wrap it in a special Hugo markup to assign the correct Page from
// the stack.
c = hugocontext.Wrap(c, pco.po.p.pid)
return template.HTML(hugocontext.Wrap(c, pco.po.p.pid)), nil
}

return helpers.BytesToHTML(c), nil
Expand Down
4 changes: 2 additions & 2 deletions markup/goldmark/hugocontext/hugocontext.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func New() goldmark.Extender {

// Wrap wraps the given byte slice in a Hugo context that used to determine the correct Page
// in .RenderShortcodes.
func Wrap(b []byte, pid uint64) []byte {
func Wrap(b []byte, pid uint64) string {
buf := bufferpool.GetBuffer()
defer bufferpool.PutBuffer(buf)
buf.Write(prefix)
Expand All @@ -45,7 +45,7 @@ func Wrap(b []byte, pid uint64) []byte {
buf.Write(b)
buf.Write(prefix)
buf.Write(closingDelimAndNewline)
return buf.Bytes()
return buf.String()
}

var kindHugoContext = ast.NewNodeKind("HugoContext")
Expand Down
2 changes: 1 addition & 1 deletion markup/goldmark/hugocontext/hugocontext_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ func TestWrap(t *testing.T) {

b := []byte("test")

c.Assert(string(Wrap(b, 42)), qt.Equals, "{{__hugo_ctx pid=42}}\ntest{{__hugo_ctx/}}\n")
c.Assert(Wrap(b, 42), qt.Equals, "{{__hugo_ctx pid=42}}\ntest{{__hugo_ctx/}}\n")
}

func BenchmarkWrap(b *testing.B) {
Expand Down

0 comments on commit 509ab08

Please sign in to comment.