Skip to content

Commit 7042d99

Browse files
vjymisal0jgm
authored andcommitted
Fix reference link title incorrectly kept when followed by garbage
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 #468) to test/regression.txt covering this exact case. Fixes #468
1 parent 9562204 commit 7042d99

2 files changed

Lines changed: 14 additions & 0 deletions

File tree

‎src/inlines.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1495,6 +1495,10 @@ bufsize_t cmark_parse_reference_inline(cmark_mem *mem, cmark_chunk *input,
14951495
if (!skip_line_end(&subj)) {
14961496
return 0;
14971497
}
1498+
// the previously scanned title turned out to be invalid (it was
1499+
// followed by trailing garbage instead of the end of the line), so
1500+
// the reference has no title after all.
1501+
title = cmark_chunk_literal("");
14981502
} else {
14991503
return 0;
15001504
}

‎test/regression.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,3 +321,13 @@ Issue #611:
321321
<p><foo>text</foo></p>
322322
````````````````````````````````
323323

324+
Issue #468: Link reference definition title followed by trailing
325+
garbage on the same line must not be treated as a title.
326+
```````````````````````````````` example
327+
[bla]: /bla
328+
"The title" garbage
329+
[bla]
330+
.
331+
<p>&quot;The title&quot; garbage
332+
<a href="/bla">bla</a></p>
333+
````````````````````````````````

0 commit comments

Comments
 (0)