Skip to content

Incorrect parser->offset advancement for closing fenced code blocks in parse_code_block_prefix #590

Open

Description

@joaobzrr

Description

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->offset from its current value, which is not necessarily aligned with the start of the fence.

      *should_continue = false;
      S_advance_offset(parser, input, matched, false); // <-- This line
      parser->current = finalize(parser, container);

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

Activity

  1. 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
  2. jgm commented on Nov 6, 2025

    @jgm
    Member

    Thank you for the diagnosis. Do you want to suggest a fix (make a PR)?

  3. added a commit that references this issue on Sep 13, 2026
    13a4aaf
  4. cpruijsen commented on Sep 13, 2026

    @cpruijsen

    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:

    ```\ncode\n   ```\n
    before:  offset=0 col=0 first_nonspace=3 matched=3
    after:   offset=3 col=3        <- advanced from 0
    

    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.

  5. nwellnhof commented on Sep 14, 2026

    @nwellnhof
    Contributor

    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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions