You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Incorrect parser->offset advancement for closing fenced code blocks in parse_code_block_prefix #590
In the parse_code_block_prefix function within blocks.c, when a closing fenced code block is successfully matched, the parser->offset is advanced by the length of the fence sequence (matched) using S_advance_offset. However, S_advance_offset advances parser->offsetfrom its current value, which is not necessarily aligned with the start of the fence.
S_advance_offset is called with matched as the count. If parser->offset is, for example, 0 (start of the line) and the fence starts at parser->first_nonspace (e.g., 4 due to indentation), then S_advance_offset(parser, input, matched, false) would advance parser->offset from 0 by matched bytes, instead of advancing it from parser->first_nonspace by matched bytes. This leaves parser->offset at an incorrect position relative to the end of the consumed fence.
Environment
cmark version: 0.31.1
Platform: Windows
Compiler: Microsoft (R) C/C++ Optimizing Compiler Version 19.29.30154 for x64
changed the title [-]Title: Incorrect `parser->offset` advancement for closing fenced code blocks in `parse_code_block_prefix`[/-][+]Incorrect `parser->offset` advancement for closing fenced code blocks in `parse_code_block_prefix`[/+]on Oct 31, 2025
I looked into this and can confirm the arithmetic, with one correction to the example and one finding
that probably changes what you want to do about it.
The arithmetic is wrong.parse_code_block_prefix calls S_advance_offset(parser, input, matched, false), which advances from parser->offset rather than from the fence start. The three sibling
scanners in the same file all pass parser->first_nonspace + matched - parser->offset instead
(blocks.c lines 1017, 1044, 1089), so the closing-fence path is the odd one out.
Instrumenting the close path on 6566d91 with a 3-space-indented close:
Advancing from first_nonspace gives offset=6, which is where the fence actually ends. Same
pattern nested in a blockquote or list.
The example in the issue is not a closing fence. A close is only scanned when parser->indent <= 3, so the 4-space case is code content by the spec ("This is not a closing fence, because it is
indented 4 spaces"). The cases that do reach this line are indent 1 to 3, or leftover indent after a
quote or list prefix.
I could not find a user-visible effect. Immediately after the call, *should_continue = false
makes check_open_blocks return NULL and S_process_line jump to finished:, which reads input.len rather than parser->offset. finalize takes end_column from parser->curline.size.
The next line resets the offset. So the corrected value is never read.
I rendered 14 documents through 6 renderers (HTML, HTML --sourcepos, XML --sourcepos, commonmark,
latex, man) with and without the change: byte-identical, zero diffs. I could not construct a case
where it matters, and I could not write a regression.txt entry that fails without it, since
nothing observable changes.
That leaves three options and I do not think an outside contributor should pick: correct the
arithmetic for consistency with its siblings, drop the call as dead, or leave it. Happy to open a PR
for whichever you prefer. The one-line change is ready either way, but I did not want to send you a
"Fixes #590" with no test behind it.
While the comment above is hard-to-parse LLM output, the observation that the arithmetic is wrong but not user-visible is correct. The end_column of blocks doesn't depend on parser->offset but on parser->curline.size or parser->last_line_length (see function finalize). I'd suggest to remove the S_advance_offset call.
@cpruijsen Please don't post raw LLM-generated content, but a human-written, concise and double-checked summary instead.
Activity
[-]Title: Incorrect `parser->offset` advancement for closing fenced code blocks in `parse_code_block_prefix`[/-][+]Incorrect `parser->offset` advancement for closing fenced code blocks in `parse_code_block_prefix`[/+]jgm commentedon Nov 6, 2025
Thank you for the diagnosis. Do you want to suggest a fix (make a PR)?
blocks: Fix parser->offset advance for closing code fences
cpruijsen commentedon Sep 13, 2026
I looked into this and can confirm the arithmetic, with one correction to the example and one finding
that probably changes what you want to do about it.
The arithmetic is wrong.
parse_code_block_prefixcallsS_advance_offset(parser, input, matched, false), which advances fromparser->offsetrather than from the fence start. The three siblingscanners in the same file all pass
parser->first_nonspace + matched - parser->offsetinstead(blocks.c lines 1017, 1044, 1089), so the closing-fence path is the odd one out.
Instrumenting the close path on 6566d91 with a 3-space-indented close:
Advancing from
first_nonspacegivesoffset=6, which is where the fence actually ends. Samepattern nested in a blockquote or list.
The example in the issue is not a closing fence. A close is only scanned when
parser->indent <= 3, so the 4-space case is code content by the spec ("This is not a closing fence, because it isindented 4 spaces"). The cases that do reach this line are indent 1 to 3, or leftover indent after a
quote or list prefix.
I could not find a user-visible effect. Immediately after the call,
*should_continue = falsemakes
check_open_blocksreturn NULL andS_process_linejump tofinished:, which readsinput.lenrather thanparser->offset.finalizetakesend_columnfromparser->curline.size.The next line resets the offset. So the corrected value is never read.
I rendered 14 documents through 6 renderers (HTML, HTML --sourcepos, XML --sourcepos, commonmark,
latex, man) with and without the change: byte-identical, zero diffs. I could not construct a case
where it matters, and I could not write a
regression.txtentry that fails without it, sincenothing observable changes.
That leaves three options and I do not think an outside contributor should pick: correct the
arithmetic for consistency with its siblings, drop the call as dead, or leave it. Happy to open a PR
for whichever you prefer. The one-line change is ready either way, but I did not want to send you a
"Fixes #590" with no test behind it.
nwellnhof commentedon Sep 14, 2026
While the comment above is hard-to-parse LLM output, the observation that the arithmetic is wrong but not user-visible is correct. The
end_columnof blocks doesn't depend onparser->offsetbut onparser->curline.sizeorparser->last_line_length(see functionfinalize). I'd suggest to remove theS_advance_offsetcall.@cpruijsen Please don't post raw LLM-generated content, but a human-written, concise and double-checked summary instead.