Skip to content

Commit fd14edc

Browse files
Fix panic in table parser
1 parent 15ade8a commit fd14edc

File tree

2 files changed

+39
-2
lines changed

2 files changed

+39
-2
lines changed

extension/table.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -184,11 +184,11 @@ func (b *tableParagraphTransformer) Transform(node *gast.Paragraph, reader text.
184184
func (b *tableParagraphTransformer) parseRow(segment text.Segment,
185185
alignments []ast.Alignment, isHeader bool, reader text.Reader, pc parser.Context) *ast.TableRow {
186186
source := reader.Source()
187+
segment = segment.TrimLeftSpace(source)
188+
segment = segment.TrimRightSpace(source)
187189
line := segment.Value(source)
188190
pos := 0
189-
pos += util.TrimLeftSpaceLength(line)
190191
limit := len(line)
191-
limit -= util.TrimRightSpaceLength(line)
192192
row := ast.NewTableRow(alignments)
193193
if len(line) > 0 && line[pos] == '|' {
194194
pos++

extension/table_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -355,3 +355,40 @@ bar | baz
355355
t,
356356
)
357357
}
358+
359+
func TestTableFuzzedPanics(t *testing.T) {
360+
markdown := goldmark.New(
361+
goldmark.WithRendererOptions(
362+
html.WithXHTML(),
363+
html.WithUnsafe(),
364+
),
365+
goldmark.WithExtensions(
366+
NewTable(),
367+
),
368+
)
369+
testutil.DoTestCase(
370+
markdown,
371+
testutil.MarkdownTestCase{
372+
No: 1,
373+
Description: "This should not panic",
374+
Markdown: "* 0\n-|\n\t0",
375+
Expected: `<ul>
376+
<li>
377+
<table>
378+
<thead>
379+
<tr>
380+
<th>0</th>
381+
</tr>
382+
</thead>
383+
<tbody>
384+
<tr>
385+
<td>0</td>
386+
</tr>
387+
</tbody>
388+
</table>
389+
</li>
390+
</ul>`,
391+
},
392+
t,
393+
)
394+
}

0 commit comments

Comments
 (0)