gomarkdown / markdown

markdown parser and HTML renderer for Go
Other
1.36k stars 171 forks source link

Slow performance decoding binary bytes #265

Closed wader closed 1 year ago

wader commented 1 year ago

Hi, I noticed while fuzzing fq that it sometimes times out and it turned out to be gomarkdown being quite slow when fed random binary data. Did a quick CPU profile and most of the time is spent in parser.codeSpan: https://github.com/gomarkdown/markdown/blob/90efaac57fb4d6c93a2f5dff442a6ae0774de269/parser/inline.go#L151-L161

A bit weird "use case" i admit but maybe it could be used for denial-of-service attacks etc? maybe the parser should fail somehow instead if hard to make faster?

Attaching file produced by go fuzzer. File takes around 15s to parser on my laptop.

test2.md

kjk commented 1 year ago

It does look like we could break out of the loop when we set hasCharsAfterDelimiter = true instead of looping till the end.

kjk commented 1 year ago

Doing the break speeds things up by 5000+x:

before:
BenchmarkIssue265-16                   1        18239097588 ns/op         787176 B/op        162 allocs/op

after:
BenchmarkIssue265-16                 448           3093183 ns/op          787170 B/op        161 allocs/op
wader commented 1 year ago

Pretty good :)

kjk commented 1 year ago

Thanks for the test case!

wader commented 1 year ago

No problem, thanks for quick fix and thanks for gomarkdown, works great! I use it in fq for being able to use markdown both for web and cli documentation. Is implemented as "format" you can decode, was the reason it got fuzzed.