From a19303e121e18d8ef54e2f326245dba8d06be82e Mon Sep 17 00:00:00 2001 From: Ilya Chernikov Date: Thu, 6 Apr 2017 18:49:03 +0200 Subject: [PATCH] Fix environment arg parsing regex - avoid SO on the long strings --- .../cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt | 2 +- .../tests/org/jetbrains/kotlin/daemon/CompilerApiTest.kt | 7 +++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt index a0fd4e7cf86..d7f08680199 100644 --- a/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt +++ b/compiler/cli/src/org/jetbrains/kotlin/cli/jvm/K2JVMCompiler.kt @@ -423,7 +423,7 @@ class K2JVMCompiler : CLICompiler() { // - is a single word (\w+ pattern) // - optionally quoted string with allowed escaped chars (only double-quote and backslash chars are supported) // TODO: implement generic unescaping - val envParseRe = """(\w+)=(?:"((?:\\.|[^"])*)"|([^\s]*))""".toRegex() + val envParseRe = """(\w+)=(?:"([^"\\]*(\\.[^"\\]*)*)"|([^\s]*))""".toRegex() val unescapeRe = """\\(["\\])""".toRegex() if (arguments.scriptResolverEnvironment != null) { for (envParam in arguments.scriptResolverEnvironment) { diff --git a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerApiTest.kt b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerApiTest.kt index 22c647a8bdc..f3cb192cd97 100644 --- a/compiler/tests/org/jetbrains/kotlin/daemon/CompilerApiTest.kt +++ b/compiler/tests/org/jetbrains/kotlin/daemon/CompilerApiTest.kt @@ -29,6 +29,7 @@ import org.jetbrains.kotlin.daemon.common.* import org.jetbrains.kotlin.integration.KotlinIntegrationTestBase import org.jetbrains.kotlin.scripts.captureOut import org.jetbrains.kotlin.test.KotlinTestUtils +import org.jetbrains.kotlin.utils.keysToMap import org.junit.Assert import java.io.File import java.net.URLClassLoader @@ -97,10 +98,12 @@ class CompilerApiTest : KotlinIntegrationTestBase() { fun args(body: K2JVMCompilerArguments.() -> Unit): K2JVMCompilerArguments = K2JVMCompilerArguments().apply(body) + val longStr = (1..100).joinToString { """\" $it aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \\""" } + val unescapeRe = """\\(["\\])""".toRegex() val messageCollector = TestMessageCollector() - Assert.assertEquals(hashMapOf("abc" to "def", "11" to "ab cd \\ \""), + Assert.assertEquals(hashMapOf("abc" to "def", "11" to "ab cd \\ \"", "long" to unescapeRe.replace(longStr, "\$1")), K2JVMCompiler.createScriptResolverEnvironment( - args { scriptResolverEnvironment = arrayOf("abc=def", """11="ab cd \\ \""""") }, + args { scriptResolverEnvironment = arrayOf("abc=def", """11="ab cd \\ \""""", "long=\"$longStr\"") }, messageCollector)) }