Fix a corner case in the ATX header parser #53
Merged
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Hi @kivikakk.
Here's a fix for a panic in the parser. This is the patch I committed to Reddit's downstream fork, but I'm only just learning the codebase, and would love to have your feedback on the correct fix. The patch contains a massive comment indicative of my low understanding of the code in general, so I understand if you want that slimmed down.
Problem description from the commit follows.
Fix a corner case in the ATX header parser
The parser would previously crash in some cases of empty ATX headers with
trailing hashes. This case isn't captured by the CommonMark spec.
The bad cases look like this:
(note the leading space here is not part of the bug - it's just so
git commitdoesn't throw these lines away as comments)
In these cases the line parser advances
first_nonspaceall the way to thesecond set of hashes, while the
chop_trailing_spacesroutine for strippingthe trailing hashes also trims leading whitespace, leading to a buffer
overrun as
add_text_to_containerthinks the content of the lineis only the leading "###" but that the text begins at the start
of the start of "####".
This hasn't been detected previously because the spec only contains
a test case for
and standard CommonMark always eats exactly one space after the initial ATX
hashes, leaving the line parser's
offsetequal tofirst_nonspace, andcorrectly adding empty text to the header container.
The spec might want an additional test case containing multiple spaces in an
empty bookended ATX header.
cc @SSJohns