Skip to content

Commit cd5a278

Browse files
Fix RangeError hazards in links (dart-archive/markdown#624)
1 parent 856d0d3 commit cd5a278

File tree

3 files changed

+43
-0
lines changed

3 files changed

+43
-0
lines changed

pkgs/markdown/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
* Fix an issue with checkbox list items separated with blank lines (#602).
44
* Require package `web: '>=0.4.2 <2.0.0'`.
5+
* Fix several `RangeError` hazards in links (#623).
56

67
## 7.2.2
78

pkgs/markdown/lib/src/inline_syntaxes/link_syntax.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ class LinkSyntax extends DelimiterSyntax {
216216
final char = parser.charAt(parser.pos);
217217
if (char == $backslash) {
218218
parser.advanceBy(1);
219+
if (parser.isDone) return null;
219220
final next = parser.charAt(parser.pos);
220221
if (next != $backslash && next != $rbracket) {
221222
buffer.writeCharCode(char);
@@ -273,12 +274,14 @@ class LinkSyntax extends DelimiterSyntax {
273274
/// Returns the link if it was successfully created, `null` otherwise.
274275
InlineLink? _parseInlineBracketedLink(InlineParser parser) {
275276
parser.advanceBy(1);
277+
if (parser.isDone) return null;
276278

277279
final buffer = StringBuffer();
278280
while (true) {
279281
final char = parser.charAt(parser.pos);
280282
if (char == $backslash) {
281283
parser.advanceBy(1);
284+
if (parser.isDone) return null;
282285
final next = parser.charAt(parser.pos);
283286
// TODO: Follow the backslash spec better here.
284287
// https://spec.commonmark.org/0.30/#backslash-escapes
@@ -302,6 +305,7 @@ class LinkSyntax extends DelimiterSyntax {
302305
final destination = buffer.toString();
303306

304307
parser.advanceBy(1);
308+
if (parser.isDone) return null;
305309
final char = parser.charAt(parser.pos);
306310
if (char == $space || char == $lf || char == $cr || char == $ff) {
307311
final title = _parseTitle(parser);
@@ -433,13 +437,15 @@ class LinkSyntax extends DelimiterSyntax {
433437

434438
final closeDelimiter = delimiter == $lparen ? $rparen : delimiter;
435439
parser.advanceBy(1);
440+
if (parser.isDone) return null;
436441

437442
// Now we look for an un-escaped closing delimiter.
438443
final buffer = StringBuffer();
439444
while (true) {
440445
final char = parser.charAt(parser.pos);
441446
if (char == $backslash) {
442447
parser.advanceBy(1);
448+
if (parser.isDone) return null;
443449
final next = parser.charAt(parser.pos);
444450
if (next != $backslash && next != closeDelimiter) {
445451
buffer.writeCharCode(char);

pkgs/markdown/test/markdown_test.dart

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,42 @@ void main() async {
7878
5 Ethernet ([Music](
7979
''', '''
8080
<p>5 Ethernet ([Music](</p>
81+
''');
82+
83+
validateCore('Incorrect Links - Issue #623 - 1 - Bracketed link 1', '''
84+
[](<
85+
''', '''
86+
<p>[](&lt;</p>
87+
''');
88+
89+
validateCore('Incorrect Links - Issue #623 - 2 - Bracketed link 2', '''
90+
[](<>
91+
''', '''
92+
<p>[](&lt;&gt;</p>
93+
''');
94+
95+
validateCore('Incorrect Links - Issue #623 - 3 - Bracketed link 3', r'''
96+
[](<\
97+
''', r'''
98+
<p>[](&lt;\</p>
99+
''');
100+
101+
validateCore('Incorrect Links - Issue #623 - 4 - Link title 1', '''
102+
[](www.example.com "
103+
''', '''
104+
<p>[](www.example.com &quot;</p>
105+
''');
106+
107+
validateCore('Incorrect Links - Issue #623 - 5 - Link title 2', r'''
108+
[](www.example.com "\
109+
''', r'''
110+
<p>[](www.example.com &quot;\</p>
111+
''');
112+
113+
validateCore('Incorrect Links - Issue #623 - 6 - Reference link label', r'''
114+
[][\
115+
''', r'''
116+
<p>[][\</p>
81117
''');
82118

83119
validateCore('Escaping code block language', '''

0 commit comments

Comments
 (0)