From 31ad3e7ab049cf6a663566f2f809d8d1a90c996b Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sat, 2 May 2020 14:25:26 -0400 Subject: [PATCH 1/3] fix[parser] add edge case handle for illegal 0 width matches --- src/highlight.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/highlight.js b/src/highlight.js index d60330fba5..d1eb36e79e 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -365,6 +365,14 @@ const HLJS = function(hljs) { } } + // edge case for when illegal matches $ (end of line) which is technically + // a 0 width match but not a begin/end match so it's not caught by the + // first handler (when ignoreIllegals is true) + if (match.type === "illegal" && lexeme === "") { + // advance so we aren't stuck in an infinite loop + return 1; + } + /* Why might be find ourselves here? Only one occasion now. An end match that was triggered but could not be completed. When might this happen? When an `endSameasBegin` From a9a5adf9ae5844378a612b3fa02c64e13ee24ea8 Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sat, 2 May 2020 14:50:26 -0400 Subject: [PATCH 2/3] last ditch catch all --- src/highlight.js | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/src/highlight.js b/src/highlight.js index d1eb36e79e..07f61b6aec 100644 --- a/src/highlight.js +++ b/src/highlight.js @@ -373,6 +373,15 @@ const HLJS = function(hljs) { return 1; } + // infinite loops are BAD, this is a last ditch catch all. if we have a + // decent number of iterations yet our index (cursor position in our + // parsing) still 3x behind our index then something is very wrong + // so we bail + if (iterations > 100000 && iterations > match.index * 3) { + const err = new Error('potential infinite loop, way more iterations than matches'); + throw err; + } + /* Why might be find ourselves here? Only one occasion now. An end match that was triggered but could not be completed. When might this happen? When an `endSameasBegin` @@ -404,12 +413,14 @@ const HLJS = function(hljs) { var mode_buffer = ''; var relevance = 0; var index = 0; + var iterations = 0; var continueScanAtSamePosition = false; try { top.matcher.considerAll(); for (;;) { + iterations++; if (continueScanAtSamePosition) { continueScanAtSamePosition = false; // only regexes not matched previously will now be From 9edb030d6edd5190401594612b9e15f9268b949c Mon Sep 17 00:00:00 2001 From: Josh Goebel Date: Sun, 3 May 2020 14:00:10 -0400 Subject: [PATCH 3/3] Update CHANGES.md --- CHANGES.md | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index 8d9201c218..e5e293e3e2 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -37,10 +37,9 @@ Brower build: - [Issue](https://github.com/highlightjs/highlight.js/issues/2505) (bug) Fix: Version 10 fails to load as CommonJS module. (#2511) [Josh Goebel][] - [Issue](https://github.com/highlightjs/highlight.js/issues/2505) (removal) AMD module loading support has been removed. (#2511) [Josh Goebel][] - Parser Engine Changes: -- ... +- [Issue](https://github.com/highlightjs/highlight.js/issues/2522) fix(parser) Fix freez issue with illegal 0 width matches (#2524) [Josh Goebel][] [Josh Goebel]: https://github.com/yyyc514