commonmark / commonmark/cmark

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

Open Beginner friendly
#590 3 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
C
Stars
2k
Forks
691
Avg merge
1d 16h
Merged PRs (30d)
1

Description

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

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in blocks.c at parse_code_block_prefix and inspect how S_advance_offset uses parser->offset relative to parser->first_nonspace. Reproduce the case with an indented closing fence and run the existing cmark test suite; done means the parser offset ends at the consumed fence without regressing other fenced-block cases.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
tooling
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Active
Clarity
Clearly specified
Newbie friendliness
76/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.