From 4e5ef34046d56d7e091de2fbc950203e00ddce29 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 2 Jul 2025 15:22:54 +0200 Subject: [PATCH 1/3] Fix performance and correctness of HTML comment parser. RegExp had catastrophic backtracking. Also didn't match the spec if linked to. (Which is still CM 30, not 31.2, they differ.) Fixed a few other incorrect parsings. - The content of ``, `` and `` can contain newlines. Changed `.` to `[^]`. - The `]+-[^-<>]+)+|[^-<>]+)-->' + r'` is needed. + // The number of lines increase time exponentially. + // The length of lines affect the base of the exponentiation. + // Locally, three "Lorem-ipsum" lines ran in ~6 seconds, two in < 200 ms. + // Adding a fourth line should ensure it cannot possibly finish in ten + // seconds if the bug isn't fixed. + const input = ''' +a +'''; + + final time = Stopwatch()..start(); + final html = markdownToHtml(input); // Should not hang. + final elapsed = time.elapsedMilliseconds; + expect(elapsed, lessThan(10000)); + }); + + test('HTML comment with lt/gt', () { + // Incorrect parsing found as part of fixing #2119. + // Now matches `` where text + // does not start with `>` or `->`, does not end with `-`, + // and does not contain `--`. + const input = 'a '; + final html = markdownToHtml(input); + expect(html, '

$input

\n'); + }); +} From 0351e3c049b5f2398c24ef308725213c70d220ad Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 2 Jul 2025 15:29:00 +0200 Subject: [PATCH 2/3] Add CHANGELOG entry. --- pkgs/markdown/CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/pkgs/markdown/CHANGELOG.md b/pkgs/markdown/CHANGELOG.md index b67e9751a2..60a2508877 100644 --- a/pkgs/markdown/CHANGELOG.md +++ b/pkgs/markdown/CHANGELOG.md @@ -3,6 +3,7 @@ * Update the README link to the markdown playground (https://dart-lang.github.io/tools). * Update `package:web` API references in the example. +* Fix performance and correctness of HTML comment parser. * Require Dart `^3.4.0`. ## 7.3.0 From f7ca1fdfc20ac1543a304372dd175222a9842728 Mon Sep 17 00:00:00 2001 From: "Lasse R.H. Nielsen" Date: Wed, 2 Jul 2025 15:32:39 +0200 Subject: [PATCH 3/3] Fix unused variable warning in test. Break long lines. --- pkgs/markdown/test/regression_test.dart | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/pkgs/markdown/test/regression_test.dart b/pkgs/markdown/test/regression_test.dart index 69a18c281f..c9d26213ba 100644 --- a/pkgs/markdown/test/regression_test.dart +++ b/pkgs/markdown/test/regression_test.dart @@ -18,15 +18,28 @@ void main() { // seconds if the bug isn't fixed. const input = ''' a '''; final time = Stopwatch()..start(); final html = markdownToHtml(input); // Should not hang. + expect(html, isNotNull); // To use the output. final elapsed = time.elapsedMilliseconds; expect(elapsed, lessThan(10000)); });