Skip to content

Fix reference link title incorrectly kept when followed by trailing garbage - #627

Open
vjymisal0 wants to merge 1 commit into
commonmark:masterfrom
vjymisal0:fix/reference-title-trailing-garbage
Open

Fix reference link title incorrectly kept when followed by trailing garbage#627
vjymisal0 wants to merge 1 commit into
commonmark:masterfrom
vjymisal0:fix/reference-title-trailing-garbage

Conversation

@vjymisal0

Copy link
Copy Markdown

What

Fixes #468.

cmark_parse_reference_inline() in src/inlines.c speculatively parses a link title following a reference definition's URL. When the text after the parsed title isn't the end of the line (i.e. there's trailing garbage after the closing quote), the function correctly rewinds and re-checks whether the definition can still be valid by treating the URL's line as ending right after the URL — leaving the "title" text to be parsed as ordinary paragraph content on the next line.

The bug: on that rewind path, the title chunk that had already been speculatively parsed was never reset. So even though the code determined the title was invalid (it decided the reference has no title, and left "The title" garbage as plain text of the next paragraph), it still passed the stale, invalid title chunk into cmark_reference_create().

Example:

[bla]: /bla
"The title" garbage
[bla]

Before this fix:

<p>&quot;The title&quot; garbage
<a href="/bla" title="The title">bla</a></p>

The CommonMark reference dingus (and this same cmark binary via cmark --version's own paragraph output for the discarded title text) shows there should be no title attribute here, since "The title" garbage is not a valid title specification — only "The title" on its own would be.

After this fix:

<p>&quot;The title&quot; garbage
<a href="/bla">bla</a></p>

Fix

Reset title to an empty chunk (cmark_chunk_literal("")) on the rewind branch, mirroring what the "title didn't match at all" branch already does a few lines below.

Testing

  • Reproduced the bug against current master with the CLI (cmark.exe built via CMake/MinGW), confirmed the fix resolves it, and confirmed normal (non-garbage-suffixed) titles — both single-line [bla]: /bla "The title" and title-on-next-line — still parse correctly.
  • Added a regression test to test/regression.txt (Issue #468) covering this exact case.
  • Ran the full CommonMark spec test suite (ctest -R spectest_executable) — all pass.
  • Ran ctest -R regressiontest_executable — all 27 regression cases pass, including the new one.
  • ctest -R html_normalization and ctest -R smartpuncttest_executable also pass.
  • api_test fails in my local Windows/MinGW build with a DLL-load exception (0xc0000135) both before and after this change (verified by stashing the fix and rebuilding), so it's a pre-existing local toolchain issue unrelated to this diff, not a regression.

cmark_parse_reference_inline() speculatively parses a link title after
a reference definition's URL. If what follows the title is not the end
of the line (i.e. there is trailing garbage), the function rewinds and
retries treating the URL's line as ending right after the URL, so that
the "title" text is left as ordinary paragraph content instead of
being consumed as part of the reference definition.

However, on this rewind path the previously scanned `title` chunk was
never reset, so the reference was still registered with that title
even though it was determined to be invalid. This let garbage-suffixed
"titles" leak into the parsed link, e.g.:

    [bla]: /bla
    "The title" garbage
    [bla]

incorrectly produced `<a href="/bla" title="The title">`, even though
the CommonMark reference dingus (and any spec-conformant parser)
produces no title here, since `"The title" garbage` is not a valid
title specification.

Fix: reset `title` to an empty chunk on the rewind path, matching the
behavior of the "no title matched" branch just below it.

Added a regression test (Issue commonmark#468) to test/regression.txt covering
this exact case.

Fixes commonmark#468
Copilot AI lite review requested due to automatic review settings August 10, 2026 16:04

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Link reference definition title that should not be

2 participants