Fix environment arg parsing regex - avoid SO on the long strings

This commit is contained in:
Ilya Chernikov
2017-04-06 18:49:03 +02:00
parent 3552b94cdc
commit a19303e121
2 changed files with 6 additions and 3 deletions
@@ -423,7 +423,7 @@ class K2JVMCompiler : CLICompiler<K2JVMCompilerArguments>() {
// <key> - is a single word (\w+ pattern)
// <value> - 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) {
@@ -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))
}