From ddf3b72a7facc81ca9c6aa1928a37b65f3176356 Mon Sep 17 00:00:00 2001 From: Neil Fraser Date: Mon, 31 Oct 2016 11:32:01 -0700 Subject: [PATCH] Prevent custom indents from getting into an infinite loop. --- core/generator.js | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/core/generator.js b/core/generator.js index 20fb77565..6d350cc03 100644 --- a/core/generator.js +++ b/core/generator.js @@ -360,11 +360,15 @@ Blockly.Generator.prototype.provideFunction_ = function(desiredName, code) { var codeText = code.join('\n').replace( this.FUNCTION_NAME_PLACEHOLDER_REGEXP_, functionName); // Change all ' ' indents into the desired indent. + // To avoid an infinite loop of replacements, change all indents to '\0' + // character first, then replace them all with the indent. + // We are assuming that no provided functions contain a literal null char. var oldCodeText; while (oldCodeText != codeText) { oldCodeText = codeText; - codeText = codeText.replace(/^(( )*) /gm, '$1' + this.INDENT); + codeText = codeText.replace(/^(( )*) /gm, '$1\0'); } + codeText = codeText.replace(/\0/g, this.INDENT); this.definitions_[desiredName] = codeText; } return this.functionNames_[desiredName];