Skip to content

Commit

Permalink
Fix RangeError hazards in links (#624)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrechalella authored Oct 16, 2024
1 parent d53feae commit 62153ee
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

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

## 7.2.2

Expand Down
6 changes: 6 additions & 0 deletions lib/src/inline_syntaxes/link_syntax.dart
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,7 @@ class LinkSyntax extends DelimiterSyntax {
final char = parser.charAt(parser.pos);
if (char == $backslash) {
parser.advanceBy(1);
if (parser.isDone) return null;
final next = parser.charAt(parser.pos);
if (next != $backslash && next != $rbracket) {
buffer.writeCharCode(char);
Expand Down Expand Up @@ -273,12 +274,14 @@ class LinkSyntax extends DelimiterSyntax {
/// Returns the link if it was successfully created, `null` otherwise.
InlineLink? _parseInlineBracketedLink(InlineParser parser) {
parser.advanceBy(1);
if (parser.isDone) return null;

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

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

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

// Now we look for an un-escaped closing delimiter.
final buffer = StringBuffer();
while (true) {
final char = parser.charAt(parser.pos);
if (char == $backslash) {
parser.advanceBy(1);
if (parser.isDone) return null;
final next = parser.charAt(parser.pos);
if (next != $backslash && next != closeDelimiter) {
buffer.writeCharCode(char);
Expand Down
36 changes: 36 additions & 0 deletions test/markdown_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,42 @@ void main() async {
5 Ethernet ([Music](
''', '''
<p>5 Ethernet ([Music](</p>
''');

validateCore('Incorrect Links - Issue #623 - 1 - Bracketed link 1', '''
[](<
''', '''
<p>[](&lt;</p>
''');

validateCore('Incorrect Links - Issue #623 - 2 - Bracketed link 2', '''
[](<>
''', '''
<p>[](&lt;&gt;</p>
''');

validateCore('Incorrect Links - Issue #623 - 3 - Bracketed link 3', r'''
[](<\
''', r'''
<p>[](&lt;\</p>
''');

validateCore('Incorrect Links - Issue #623 - 4 - Link title 1', '''
[](www.example.com "
''', '''
<p>[](www.example.com &quot;</p>
''');

validateCore('Incorrect Links - Issue #623 - 5 - Link title 2', r'''
[](www.example.com "\
''', r'''
<p>[](www.example.com &quot;\</p>
''');

validateCore('Incorrect Links - Issue #623 - 6 - Reference link label', r'''
[][\
''', r'''
<p>[][\</p>
''');

validateCore('Escaping code block language', '''
Expand Down

0 comments on commit 62153ee

Please sign in to comment.