Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix StackOverflowError in color definitions #3915

Merged
merged 3 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
* Default to 0.15.0 behaviour if inputs field is not found in Tectonic.toml

### Fixed
* Fix StackOverflowError when color definitions reference each other
* Fix #3906 invalid element exception

## [0.10.1] - 2025-02-08
Expand Down
80 changes: 41 additions & 39 deletions src/nl/hannahsten/texifyidea/gutter/LatexElementColorProvider.kt
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class LatexElementColorProvider : ElementColorProvider {
in ColorMagic.takeColorCommands -> {
command?.getRequiredArgumentValueByName("color")
}

else -> null
} ?: return null
// Find the color to show.
Expand All @@ -87,57 +88,58 @@ class LatexElementColorProvider : ElementColorProvider {
return null
}

fun findColor(colorName: String, file: PsiFile): Color? {
fun findColor(colorName: String, file: PsiFile, recursionDepth: Int = 0): Color? {
val defaultHex = ColorMagic.defaultXcolors[colorName]

return if (defaultHex != null) Color(defaultHex)
else {
val colorDefiningCommands = LatexCommandsIndex.Util.getCommandsByNames(
file,
*ColorMagic.colorDefinitions.map { "\\${it.command}" }
.toTypedArray()
)
// If this color is a single color (not a mix, and thus does not contain a !)
// and we did not find it in the default colors (above), it should be in the
// first parameter of a color definition command. If not, we can not find the
// color (and return null in the end).
if (
colorName.contains('!') ||
colorDefiningCommands.map { it.getRequiredArgumentValueByName("name") }.contains(colorName)
) {
val colorDefinitionCommand =
colorDefiningCommands.find { it.getRequiredArgumentValueByName("name") == colorName }
when (colorDefinitionCommand?.name?.substring(1)) {
LatexColorDefinitionCommand.COLORLET.command -> {
getColorFromColorParameter(file, colorDefinitionCommand.getRequiredArgumentValueByName("color"))
}
LatexColorDefinitionCommand.DEFINECOLOR.command, LatexColorDefinitionCommand.PROVIDECOLOR.command -> {
getColorFromDefineColor(
colorDefinitionCommand.getRequiredArgumentValueByName("model-list"),
colorDefinitionCommand.getRequiredArgumentValueByName("spec-list")
)
}
LatexColorDefinitionCommand.DEFINECOLORSERIES.command -> {
getColorFromDefineColor(
colorDefinitionCommand.getOptionalArgumentValueByName("b-model") ?: colorDefinitionCommand.getRequiredArgumentValueByName("core model"),
colorDefinitionCommand.getRequiredArgumentValueByName("b-spec")
)
}
else -> getColorFromColorParameter(file, colorName)
}
@Suppress("UseJBColor") // Should show actual color also in dark mode
if (defaultHex != null) return Color(defaultHex)

val colorDefiningCommands = LatexCommandsIndex.Util.getCommandsByNames(
file,
*ColorMagic.colorDefinitions.map { "\\${it.command}" }
.toTypedArray()
)
// If this color is a single color (not a mix, and thus does not contain a !)
// and we did not find it in the default colors (above), it should be in the
// first parameter of a color definition command. If not, we can not find the
// color (and return null in the end).
if (!colorName.contains('!') && !colorDefiningCommands.map { it.getRequiredArgumentValueByName("name") }.contains(colorName)) return null

val colorDefinitionCommand = colorDefiningCommands.find { it.getRequiredArgumentValueByName("name") == colorName }
return when (colorDefinitionCommand?.name?.substring(1)) {
LatexColorDefinitionCommand.COLORLET.command -> {
getColorFromColorParameter(file, colorDefinitionCommand.getRequiredArgumentValueByName("color"), recursionDepth)
}
else return null

LatexColorDefinitionCommand.DEFINECOLOR.command, LatexColorDefinitionCommand.PROVIDECOLOR.command -> {
getColorFromDefineColor(
colorDefinitionCommand.getRequiredArgumentValueByName("model-list"),
colorDefinitionCommand.getRequiredArgumentValueByName("spec-list")
)
}

LatexColorDefinitionCommand.DEFINECOLORSERIES.command -> {
getColorFromDefineColor(
colorDefinitionCommand.getOptionalArgumentValueByName("b-model") ?: colorDefinitionCommand.getRequiredArgumentValueByName("core model"),
colorDefinitionCommand.getRequiredArgumentValueByName("b-spec")
)
}

else -> getColorFromColorParameter(file, colorName, recursionDepth)
}
}

/**
* Given the color parameter [definitionText] of a command, compute the defined color.
*/
private fun getColorFromColorParameter(file: PsiFile, definitionText: String?): Color? {
private fun getColorFromColorParameter(file: PsiFile, definitionText: String?, recursionDepth: Int): Color? {
// Infinite loops can occur with invalid color definitions.
if (recursionDepth > 42) return null

definitionText ?: return null
val colorParts = definitionText.split("!").filter { it.isNotBlank() }
val colors = colorParts.filter { it.all { c -> c.isLetter() } }
.map { findColor(it, file) ?: return null }
.map { findColor(it, file, recursionDepth + 1) ?: return null }
if (colors.isEmpty()) return null
val numbers = colorParts.filter { it.all { c -> c.isDigit() } }
.map { it.toInt() }
Expand Down
13 changes: 13 additions & 0 deletions test/nl/hannahsten/texifyidea/gutter/LatexGutterTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import com.intellij.testFramework.fixtures.BasePlatformTestCase
import io.mockk.every
import io.mockk.mockkStatic
import nl.hannahsten.texifyidea.TexifyIcons
import nl.hannahsten.texifyidea.file.LatexFileType
import nl.hannahsten.texifyidea.util.runCommandWithExitCode

class LatexGutterTest : BasePlatformTestCase() {
Expand Down Expand Up @@ -58,6 +59,18 @@ class LatexGutterTest : BasePlatformTestCase() {
assertTrue(gutters.all { g -> g.tooltipText == "Go to referenced file" })
}

fun testInfiniteColorLoop() {
myFixture.configureByText(
LatexFileType,
"""
\usepackage{xcolor}
\colorlet{kameel}{oliefant}
\colorlet{oliefant}{kameel}
""".trimIndent()
)
myFixture.findAllGutters()
}

private fun getLineMarkerLine(marker: LineMarkerInfo<*>): Int {
return myFixture.editor.document.getLineNumber((marker.element as LeafPsiElement).textRange.startOffset)
}
Expand Down
Loading