Преглед изворни кода

Adding more infinite loop detection

main
Rocketsoup пре 1 година
родитељ
комит
a26fd86115
3 измењених фајлова са 19 додато и 1 уклоњено
  1. 17
    0
      js/markdown.js
  2. 1
    1
      js/markdown.min.js
  3. 1
    0
      php/markdown.php

+ 17
- 0
js/markdown.js Прегледај датотеку

856
 		// nodes, until no more substitutions can be made.
856
 		// nodes, until no more substitutions can be made.
857
 		var anyChanges = false;
857
 		var anyChanges = false;
858
 		do {
858
 		do {
859
+			this.checkExecutionTime();
859
 			anyChanges = false;
860
 			anyChanges = false;
860
 			for (const readerTuple of this.root.readersBySubstitutePriority) {
861
 			for (const readerTuple of this.root.readersBySubstitutePriority) {
861
 				/** @type {number} */
862
 				/** @type {number} */
896
 		return nodes;
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
 	 * Mapping of reference symbols to URLs. Used by `MDReferencedLinkReader`
916
 	 * Mapping of reference symbols to URLs. Used by `MDReferencedLinkReader`
901
 	 * and `MDReferencedImageReader`.
917
 	 * and `MDReferencedImageReader`.
2785
 			return true;
2801
 			return true;
2786
 		});
2802
 		});
2787
 		if (match === null) return false;
2803
 		if (match === null) return false;
2804
+		state.checkExecutionTime();
2788
 		let content = (plaintext)
2805
 		let content = (plaintext)
2789
 			? match.contentTokens.map((token) => token.original).join('')
2806
 			? match.contentTokens.map((token) => token.original).join('')
2790
 			: state.tokensToNodes(match.contentTokens);
2807
 			: state.tokensToNodes(match.contentTokens);

+ 1
- 1
js/markdown.min.js
Разлика између датотеке није приказан због своје велике величине
Прегледај датотеку


+ 1
- 0
php/markdown.php Прегледај датотеку

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

Loading…
Откажи
Сачувај