Repository navigation
Description
Summary
cmark reports an impossible source position for an empty ATX heading whose opening # run ends the line and has whitespace immediately before it: the end column comes before the start column on the same line. - # gives 1:3-1:1 and # gives 1:2-1:0. The heading's ancestors also end before the #, and for # the document node itself is 1:1-1:0. HTML output is correct; the defect is confined to source positions, as seen through cmark_node_get_end_column, --sourcepos XML and data-sourcepos. A consumer that builds a range from these positions gets an invalid one.
Runtime or environment
- cmark 0.31.2 and
masterat 522c37a, both built with CMake in Debug mode. - For comparison, the same build of
github/cmark-gfm0.29.0.gfm.13 and its default branch at 27d942c, andswiftlang/swift-cmark0.9.0 and itsgfmbranch at 4376116. - swift-markdown 0.9.0 with Apple Swift 6.4.2 for the downstream effect.
- Apple clang 21.0.0, macOS 27.2, arm64.
Steps to reproduce
printf -- '- #\n' | cmark --to xml --sourceposRepeat with the other inputs in the table under Actual behavior. A small C program using cmark_parse_document and the cmark_node_get_start_* and cmark_node_get_end_* getters gives the same numbers.
Expected behavior
CommonMark 0.31.2 says "ATX headings can be empty" (https://spec.commonmark.org/0.31.2/#atx-headings), so these inputs are valid. src/cmark.h documents cmark_node_get_start_column as "the column at which 'node' begins" and cmark_node_get_end_column as "the column at which 'node' ends". A node cannot end before it begins. cmark's own result for other empty headings shows the position it should reach: # gives 1:1-1:1 and ># gives 1:2-1:2, covering the marker. For - #, the heading should start at column 3 and end at column 3 or later.
Actual behavior
All six builds gave identical results for every row.
| Input | Start | End | Result |
|---|---|---|---|
- #\n |
1:3 | 1:1 | inverted |
> #\n |
1:3 | 1:1 | inverted |
#\n |
1:2 | 1:0 | inverted |
#\n |
1:4 | 1:0 | inverted |
1. #\n |
1:4 | 1:2 | inverted |
> > #\n |
1:5 | 1:3 | inverted |
- ##\n |
1:3 | 1:1 | inverted |
###\n |
1:2 | 1:0 | inverted |
- # (no line ending) |
1:3 | 1:1 | inverted |
- #\r\n |
1:3 | 1:1 | inverted |
#\n (control) |
1:1 | 1:1 | valid |
##\n (control) |
1:1 | 1:2 | valid |
>#\n (control) |
1:2 | 1:2 | valid |
- # \n (control) |
1:3 | 1:4 | valid |
- # #\n (control) |
1:3 | 1:3 | valid |
- # x\n (control) |
1:3 | 1:5 | valid |
Full output for - #\n, where the list, the item and the document also stop before the #:
<document sourcepos="1:1-1:1" xmlns="http://commonmark.org/xml/1.0">
<list sourcepos="1:1-1:1" type="bullet" tight="true">
<item sourcepos="1:1-1:1">
<heading sourcepos="1:3-1:1" level="1" />
</item>
</list>
</document>To check the trigger, I parsed all 1,948,716 strings of length 1 to 6 over the alphabet #, space, -, >, x, newline, 1, ., backtick, *, tab, and walked every node. In 7,450 inputs a heading had an end before its start, and in every one of them the heading's line ended in a # run with a space or tab immediately before it. The only other inverted node was the document, in 22 inputs: 21 are the # forms above, and the other is a lone \n, which gives 1:1-1:0. I do not report that one, since it describes a document with no content. The enumeration counts are the same on both cmark builds.
Evidence
- Expected source: CommonMark 0.31.2, ATX headings, and the getter documentation at src/cmark.h lines 410-420.
- Failure source: my runs of
cmark --to xml --sourceposand of a C program calling the getters, with the results above. - Evidence provenance: observed
- Local verification: reproduced
- Reproduction completeness: complete
I found no documentation or test that accepts an inverted heading position. The spec does not define source positions, so the applicable contract is the getter documentation together with cmark's results for the control inputs.
Restoration check
Failing result: - #\n reports a heading at 1:3-1:1, and #\n reports a heading at 1:2-1:0 inside a document at 1:1-1:0. Passing result: for every inverted row in the table, the heading's end is at or after its start and the heading's span covers the # run, and the ancestors of the heading do not end before it. Controls: the control rows stay valid, and the rendered HTML for all rows stays the same (<h1></h1> for - #\n inside its list).
Additional context
The behavior appears to come from last_line_length. add_text_to_container calls chop_trailing_hashtags on the heading's line, which treats the opening # run as a closing sequence when whitespace precedes it and shortens input. S_process_line then derives last_line_length from the shortened input.len, and finalize uses it as end_column. For - # \n the scanner consumes the space, the rest of the line is blank, the chop is skipped, and the position stays valid.
As a local experiment only, I derived last_line_length from parser->curline instead. Every inverted row then gave a valid position (- #\n became 1:3-1:3) and all five ctest suites passed. That experiment also moved # foo #\n from 1:1-1:5 to 1:1-1:7 and the - # #\n control from 1:3-1:3 to 1:3-1:5, so they include the closing sequence. I am not claiming the current # foo # result is a defect, and I am not proposing that change as the fix; it is only evidence for where the short end column comes from.
Downstream, swift-markdown drops the inverted range and returns a nil range for such a heading, and I reported a crash in the Markdown Preview app on - # that traces to that nil: pluk-inc/markdown-preview#626.
References
- Position tracking of headings is inconsistent #141 is closed history about heading positions; its fix made the start of an ATX heading point at the
#. I did not bisect when the inversion first appeared. - incorrect start_column & end_column #442 is an open report of inline positions outside the document on
- \na, with a different input and node type. - Various fixes for source-position information github/cmark-gfm#210 is an open pull request fixing inverted ranges for autolinks, HTML comments and multi-line links. It does not cover empty headings.
Triage assessment
- Impact level: P2
- Assessment status: supported
- Impact basis: Every valid input of this form yields a position that no consumer can use as a range, and it also shortens the positions of ancestor nodes. Rendering is unaffected. One downstream parser drops the range, and one downstream app crashes as a consequence. How often real documents contain this form is unknown, though indented lines that hold only
#inside a list are one way to reach it. - Workaround status: available
- Workaround basis: Available only to someone who can edit the document: adding a space after the
#run gives a valid position (- # \ngives1:3-1:4) and the same HTML, which I verified. A consumer of arbitrary input has no workaround without a code change: it can treat an end before a start as invalid, and it still has no correct position for the heading.
Activity
Fix incorrect source position for ATX heading with no content.
Fix incorrect source position for ATX heading with no content.
jgm commentedon Nov 8, 2026
Fix in #657