yuin / goldmark

:trophy: A markdown parser written in Go. Easy to extend, standard(CommonMark) compliant, well structured.
MIT License
3.68k stars 255 forks source link

crash during renderFencedCodeBlock #229

Closed rsc closed 3 years ago

rsc commented 3 years ago

Please answer the following before submitting your issue:

  1. What version of goldmark are you using? : v1.3.8

  2. What version of Go are you using? : Go 1.17 devel branch

  3. What operating system and processor architecture are you using? : macOS x86

  4. What did you do? : Render the following input:

    - List
    
    A
        B
    C
    ```
    
    
    The entire fenced code block is indented with four leading spaces at the start of each line.
    The B line has a tab immediately before the B. That is, that line is "space space space space tab B newline".
  5. What did you expect to see? : Rendered HTML.

  6. What did you see instead? : A crash.

    panic: runtime error: slice bounds out of range [32:29]
    
    github.com/yuin/goldmark/text.(*Segment).Value(0xc00030d0c8, {0xc000348210, 0xc00052f200, 0x2d})
        /Users/rsc/pkg/mod/github.com/yuin/goldmark@v1.3.8/text/segment.go:48 +0x29d
    github.com/yuin/goldmark/renderer/html.(*Renderer).writeLines(0xc00052c9d8, {0x1983b90, 0xc00052f200}, {0xc000348210, 0x2c, 0x2d}, {0x19921f0, 0xc000490500})
        /Users/rsc/pkg/mod/github.com/yuin/goldmark@v1.3.8/renderer/html/html.go:193 +0xbb
    github.com/yuin/goldmark/renderer/html.(*Renderer).renderFencedCodeBlock(0xc00052c9d8, {0x1983b90, 0xc00052f200}, {0xc000348210, 0x2c, 0x2d}, {0x19921f0, 0xc000490500}, 0x90)
        /Users/rsc/pkg/mod/github.com/yuin/goldmark@v1.3.8/renderer/html/html.go:285 +0x18b
    github.com/yuin/goldmark/renderer.(*renderer).Render.func2({0x19921f0, 0xc000490500}, 0x1)
        /Users/rsc/pkg/mod/github.com/yuin/goldmark@v1.3.8/renderer/renderer.go:166 +0xd1
    github.com/yuin/goldmark/ast.walkHelper({0x19921f0, 0xc000490500}, 0xc00030d370)
        /Users/rsc/pkg/mod/github.com/yuin/goldmark@v1.3.8/ast/ast.go:492 +0x34
    github.com/yuin/goldmark/ast.walkHelper({0x19928b0, 0xc00056c750}, 0xc00030d370)
        /Users/rsc/pkg/mod/github.com/yuin/goldmark@v1.3.8/ast/ast.go:498 +0x8e
    github.com/yuin/goldmark/ast.walkHelper({0x1992790, 0xc00056c6c0}, 0xc00030d370)
        /Users/rsc/pkg/mod/github.com/yuin/goldmark@v1.3.8/ast/ast.go:498 +0x8e
    github.com/yuin/goldmark/ast.walkHelper({0x1991fb0, 0xc00056c630}, 0xc00030d370)
        /Users/rsc/pkg/mod/github.com/yuin/goldmark@v1.3.8/ast/ast.go:498 +0x8e
    github.com/yuin/goldmark/ast.Walk(...)
        /Users/rsc/pkg/mod/github.com/yuin/goldmark@v1.3.8/ast/ast.go:487
    github.com/yuin/goldmark/renderer.(*renderer).Render(0xc00049a320, {0x19637a0, 0xc0008eb770}, {0xc000348210, 0x2c, 0x2d}, {0x1991fb0, 0xc00056c630})
        /Users/rsc/pkg/mod/github.com/yuin/goldmark@v1.3.8/renderer/renderer.go:161 +0x22d
    github.com/yuin/goldmark.(*markdown).Convert(0xc00052ee40, {0xc000348210, 0x2c, 0x2d}, {0x19637a0, 0xc0008eb770}, {0x0, 0x0, 0x0})
        /Users/rsc/pkg/mod/github.com/yuin/goldmark@v1.3.8/markdown.go:117 +0x10b
  7. Did you confirm your output is different from CommonMark online demo or other official renderer correspond with an extension? : No, but it would be truly astonishing if the official renderer did this too.

rsc commented 3 years ago

Thanks for the quick fix!