Out of range error when rendering list items #30

Open
opened 2026-01-31 22:22:13 +00:00 by ryancahildebrandt · 0 comments
ryancahildebrandt commented 2026-01-31 22:22:13 +00:00 (Migrated from github.com)

When attempting to render ListItem nodes, the renderer panics

panic: runtime error: index out of range [-1]

goroutine 1 [running]:
github.com/teekennedy/goldmark-markdown.(*Renderer).renderListItem(0xc0001a6000?, {0x57b540?, 0xc0000a5860?}, 0x74?)
        /home/ryan/go/pkg/mod/github.com/teekennedy/[email protected]/renderer.go:294 +0x4dd
github.com/teekennedy/goldmark-markdown.(*Renderer).Render.func1.(*Renderer).chainRenderers.7({0x57b540, 0xc0000a5860}, 0x1)
        /home/ryan/go/pkg/mod/github.com/teekennedy/[email protected]/renderer.go:128 +0x47
github.com/teekennedy/goldmark-markdown.(*Renderer).Render.func2({0x57b540, 0xc0000a5860}, 0x1)
        /home/ryan/go/pkg/mod/github.com/teekennedy/[email protected]/renderer.go:105 +0x58
github.com/yuin/goldmark/ast.walkHelper({0x57b540, 0xc0000a5860}, 0xc00019fd10)
        /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:505 +0x2e
github.com/yuin/goldmark/ast.Walk(...)
        /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:500
github.com/teekennedy/goldmark-markdown.(*Renderer).Render(0xc0001a6000, {0x578a38, 0xc00009a020}, {0xc0001a8000, 0x2b, 0x200}, {0x57
        /home/ryan/go/pkg/mod/github.com/teekennedy/[email protected]/renderer.go:104 +0x1fb
main.main.func1({0x57b540, 0xc0000a5860}, 0x10?)
        /home/ryan/github/chunk/main.go:45 +0xf4
github.com/yuin/goldmark/ast.walkHelper({0x57b540, 0xc0000a5860}, 0xc00019fed8)
        /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:505 +0x2e
github.com/yuin/goldmark/ast.walkHelper({0x57ad60, 0xc0000a57c0}, 0xc00019fed8)
        /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:511 +0x85
github.com/yuin/goldmark/ast.walkHelper({0x57bae0, 0xc0000a5720}, 0xc00019fed8)
        /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:511 +0x85
github.com/yuin/goldmark/ast.Walk(...)
        /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:500
main.main()
        /home/ryan/github/chunk/main.go:31 +0x1e9
exit status 2

This error happens even on markdown text pulled from a passing test ("- A1\n- B1\n - C2\n - D3\n - E1"):

func main() {
	var err error
	var f []byte
	var md = goldmark.New(goldmark.WithRenderer(markdown.NewRenderer(markdown.WithHeadingStyle(markdown.HeadingStyleATX))))

	b := []byte("- A1\n- B1\n    - C2\n        - D3\n        - E1")
	reader := text.NewReader(b)
	par := md.Parser().Parse(reader)
	ast.Walk(par, func(n ast.Node, entering bool) (ast.WalkStatus, error) {
		if !entering {
			return ast.WalkContinue, nil
		}
		if n.Kind() == ast.KindListItem {
			md.Renderer().Render(os.Stdout, reader.Source(), n)
		}
		return ast.WalkContinue, nil
	})
}

Checking whether the function has reached the end of the list items in Renderer.renderListItem fixes the error and tests still pass

func (r *Renderer) renderListItem(node ast.Node, entering bool) ast.WalkStatus {
        if entering {
                if len(r.rc.lists) == 0{ return ast.WalkStop }
                var itemPrefix []byte
                l := r.rc.lists[len(r.rc.lists)-1]

                if l.list.IsOrdered() {
                        itemPrefix = append(itemPrefix, []byte(fmt.Sprint(l.num))...)
                        r.rc.lists[len(r.rc.lists)-1].num += 1
                }
                itemPrefix = append(itemPrefix, l.list.Marker, ' ')
                // Prefix the current line with the item prefix
                r.rc.writer.PushPrefix(itemPrefix, 0, 0)
                // Prefix subsequent lines with padding the same length as the item prefix
                indentLen := int(max(r.config.NestedListLength, NestedListLengthMinimum))
                indent := bytes.Repeat([]byte{' '}, indentLen)
                r.rc.writer.PushPrefix(bytes.Repeat(indent, len(itemPrefix)), 1)
        } else {
                r.rc.writer.PopPrefix()
                r.rc.writer.PopPrefix()
        }
        return ast.WalkContinue
}
When attempting to render ListItem nodes, the renderer panics ``` panic: runtime error: index out of range [-1] goroutine 1 [running]: github.com/teekennedy/goldmark-markdown.(*Renderer).renderListItem(0xc0001a6000?, {0x57b540?, 0xc0000a5860?}, 0x74?) /home/ryan/go/pkg/mod/github.com/teekennedy/[email protected]/renderer.go:294 +0x4dd github.com/teekennedy/goldmark-markdown.(*Renderer).Render.func1.(*Renderer).chainRenderers.7({0x57b540, 0xc0000a5860}, 0x1) /home/ryan/go/pkg/mod/github.com/teekennedy/[email protected]/renderer.go:128 +0x47 github.com/teekennedy/goldmark-markdown.(*Renderer).Render.func2({0x57b540, 0xc0000a5860}, 0x1) /home/ryan/go/pkg/mod/github.com/teekennedy/[email protected]/renderer.go:105 +0x58 github.com/yuin/goldmark/ast.walkHelper({0x57b540, 0xc0000a5860}, 0xc00019fd10) /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:505 +0x2e github.com/yuin/goldmark/ast.Walk(...) /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:500 github.com/teekennedy/goldmark-markdown.(*Renderer).Render(0xc0001a6000, {0x578a38, 0xc00009a020}, {0xc0001a8000, 0x2b, 0x200}, {0x57 /home/ryan/go/pkg/mod/github.com/teekennedy/[email protected]/renderer.go:104 +0x1fb main.main.func1({0x57b540, 0xc0000a5860}, 0x10?) /home/ryan/github/chunk/main.go:45 +0xf4 github.com/yuin/goldmark/ast.walkHelper({0x57b540, 0xc0000a5860}, 0xc00019fed8) /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:505 +0x2e github.com/yuin/goldmark/ast.walkHelper({0x57ad60, 0xc0000a57c0}, 0xc00019fed8) /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:511 +0x85 github.com/yuin/goldmark/ast.walkHelper({0x57bae0, 0xc0000a5720}, 0xc00019fed8) /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:511 +0x85 github.com/yuin/goldmark/ast.Walk(...) /home/ryan/go/pkg/mod/github.com/yuin/[email protected]/ast/ast.go:500 main.main() /home/ryan/github/chunk/main.go:31 +0x1e9 exit status 2 ``` This error happens even on markdown text pulled from a passing test ("- A1\n- B1\n - C2\n - D3\n - E1"): ```go func main() { var err error var f []byte var md = goldmark.New(goldmark.WithRenderer(markdown.NewRenderer(markdown.WithHeadingStyle(markdown.HeadingStyleATX)))) b := []byte("- A1\n- B1\n - C2\n - D3\n - E1") reader := text.NewReader(b) par := md.Parser().Parse(reader) ast.Walk(par, func(n ast.Node, entering bool) (ast.WalkStatus, error) { if !entering { return ast.WalkContinue, nil } if n.Kind() == ast.KindListItem { md.Renderer().Render(os.Stdout, reader.Source(), n) } return ast.WalkContinue, nil }) } ``` Checking whether the function has reached the end of the list items in Renderer.renderListItem fixes the error and tests still pass ```go func (r *Renderer) renderListItem(node ast.Node, entering bool) ast.WalkStatus { if entering { if len(r.rc.lists) == 0{ return ast.WalkStop } var itemPrefix []byte l := r.rc.lists[len(r.rc.lists)-1] if l.list.IsOrdered() { itemPrefix = append(itemPrefix, []byte(fmt.Sprint(l.num))...) r.rc.lists[len(r.rc.lists)-1].num += 1 } itemPrefix = append(itemPrefix, l.list.Marker, ' ') // Prefix the current line with the item prefix r.rc.writer.PushPrefix(itemPrefix, 0, 0) // Prefix subsequent lines with padding the same length as the item prefix indentLen := int(max(r.config.NestedListLength, NestedListLengthMinimum)) indent := bytes.Repeat([]byte{' '}, indentLen) r.rc.writer.PushPrefix(bytes.Repeat(indent, len(itemPrefix)), 1) } else { r.rc.writer.PopPrefix() r.rc.writer.PopPrefix() } return ast.WalkContinue } ```
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
golibs/goldmark-markdown#30
No description provided.