Skip to content

Commit

Permalink
Prevent stack overflow exception when code provided vis stdin can not…
Browse files Browse the repository at this point in the history
… be parsed as Kotlin, nor Kotlin script (#2380)

Closes #2379
  • Loading branch information
paul-dingemans authored Nov 26, 2023
1 parent 421cdbe commit 352c0c4
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -538,18 +538,32 @@ internal class KtlintCommandLine {
}
} catch (e: Exception) {
if (code.isStdIn && e is KtLintParseException) {
logger.warn {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
Now, trying to read the input as Kotlin Script.
""".trimIndent()
if (code.script) {
// When reading from stdin, code is only parsed as Kotlint script, if it could not be parsed as pure Kotlin. Now parsing
// of the code has failed for both, the file has to be ignored.
logger.error {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
""".trimIndent()
}
ktlintCliErrors.add(e.toKtlintCliError(code))
} else {
// When reading from stdin, it is first assumed that the provided code is pure Kotlin instead of Kotlin script. If
// parsing fails, retry parsing at Kotlin script.
logger.warn {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
Now, trying to read the input as Kotlin Script.
""".trimIndent()
}
return format(
ktLintRuleEngine = ktLintRuleEngine,
code = Code.fromSnippet(code.content, script = true),
baselineLintErrors = baselineLintErrors,
)
}
return format(
ktLintRuleEngine = ktLintRuleEngine,
code = Code.fromSnippet(code.content, script = true),
baselineLintErrors = baselineLintErrors,
)
} else {
ktlintCliErrors.add(e.toKtlintCliError(code))
tripped.set(true)
Expand Down Expand Up @@ -587,18 +601,32 @@ internal class KtlintCommandLine {
}
} catch (e: Exception) {
if (code.isStdIn && e is KtLintParseException) {
logger.warn {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
Now, trying to read the input as Kotlin Script.
""".trimIndent()
if (code.script) {
// When reading from stdin, code is only parsed as Kotlint script, if it could not be parsed as pure Kotlin. Now parsing
// of the code has failed for both, the file has to be ignored.
logger.error {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
""".trimIndent()
}
ktlintCliErrors.add(e.toKtlintCliError(code))
} else {
// When reading from stdin, it is first assumed that the provided code is pure Kotlin instead of Kotlin script. If
// parsing fails, retry parsing at Kotlin script.
logger.warn {
"""
Can not parse input from <stdin> as Kotlin, due to error below:
${e.toKtlintCliError(code).detail}
Now, trying to read the input as Kotlin Script.
""".trimIndent()
}
return lint(
ktLintRuleEngine = ktLintRuleEngine,
code = Code.fromSnippet(code.content, script = true),
baselineLintErrors = baselineLintErrors,
)
}
return lint(
ktLintRuleEngine = ktLintRuleEngine,
code = Code.fromSnippet(code.content, script = true),
baselineLintErrors = baselineLintErrors,
)
} else {
ktlintCliErrors.add(e.toKtlintCliError(code))
tripped.set(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,28 @@ class SimpleCLITest {
}
}

@Test
fun `Issue 2379 - Given stdin input resulting in a KtLintParseException when formatted as Kotlin Script code`(
@TempDir
tempDir: Path,
) {
CommandLineTestRunner(tempDir)
.run(
testProjectName = "too-many-empty-lines",
arguments = listOf("--stdin", "--format"),
stdin =
ByteArrayInputStream(
"""
fun foo() =
""".trimIndent().toByteArray(),
),
) {
assertThat(errorOutput)
.containsLineMatching(Regex(".*Not a valid Kotlin file.*"))
.doesNotContainLineMatching(Regex(".*Now, trying to read the input as Kotlin Script.*"))
}
}

@Test
fun `Enable android code style via parameter --code-style=android_studio`(
@TempDir
Expand Down

0 comments on commit 352c0c4

Please sign in to comment.