Pārlūkot izejas kodu

Adding more infinite loop detection

main
Rocketsoup 1 gadu atpakaļ
vecāks
revīzija
a26fd86115
3 mainītis faili ar 19 papildinājumiem un 1 dzēšanām
  1. 17
    0
      js/markdown.js
  2. 1
    1
      js/markdown.min.js
  3. 1
    0
      php/markdown.php

+ 17
- 0
js/markdown.js Parādīt failu

@@ -856,6 +856,7 @@ class MDState {
856 856
 		// nodes, until no more substitutions can be made.
857 857
 		var anyChanges = false;
858 858
 		do {
859
+			this.checkExecutionTime();
859 860
 			anyChanges = false;
860 861
 			for (const readerTuple of this.root.readersBySubstitutePriority) {
861 862
 				/** @type {number} */
@@ -896,6 +897,21 @@ class MDState {
896 897
 		return nodes;
897 898
 	}
898 899
 
900
+	#startTime = performance.now();
901
+
902
+	/**
903
+	 * Checks if parsing has taken an excessive length of time. Because I'm not
904
+	 * fully confident in my loops yet. :)
905
+	 * 
906
+	 * @param {number} maxSeconds
907
+	 */
908
+	checkExecutionTime(maxSeconds=1.0) {
909
+		const elapsed = (performance.now() - this.root.#startTime) / 1000.0;
910
+		if (elapsed > maxSeconds) {
911
+			throw new Error("Markdown parsing taking too long. Infinite loop?");
912
+		}
913
+	}
914
+
899 915
 	/**
900 916
 	 * Mapping of reference symbols to URLs. Used by `MDReferencedLinkReader`
901 917
 	 * and `MDReferencedImageReader`.
@@ -2785,6 +2801,7 @@ class MDSimplePairInlineReader extends MDReader {
2785 2801
 			return true;
2786 2802
 		});
2787 2803
 		if (match === null) return false;
2804
+		state.checkExecutionTime();
2788 2805
 		let content = (plaintext)
2789 2806
 			? match.contentTokens.map((token) => token.original).join('')
2790 2807
 			: state.tokensToNodes(match.contentTokens);

+ 1
- 1
js/markdown.min.js
Failā izmaiņas netiks attēlotas, jo tās ir par lielu
Parādīt failu


+ 1
- 0
php/markdown.php Parādīt failu

@@ -724,6 +724,7 @@ class MDState {
724 724
 		// nodes, until no more substitutions can be made.
725 725
 		$anyChanges = false;
726 726
 		do {
727
+			$this->checkExecutionTime();
727 728
 			$anyChanges = false;
728 729
 			foreach ($this->root()->readersBySubstitutePriority as $readerTuple) {
729 730
 				/** @var int */

Notiek ielāde…
Atcelt
Saglabāt