Skip to content

Commit 583e2a6

Browse files
committed
Fix #13: Don't crash on invalid color values
1 parent adaf782 commit 583e2a6

File tree

8 files changed

+49
-29
lines changed

8 files changed

+49
-29
lines changed

src/main/kotlin/com/gmail/blueboxware/libgdxplugin/annotators/ColorAnnotator.kt

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,6 @@ class ColorAnnotator : Annotator {
4848
private val annotationsKey = key<MutableList<Pair<Int, Color>>>("annotations")
4949
private val cacheKey = key<ColorAnnotatorCache>("cache")
5050

51-
val colorRegex = Regex("#?(?:[0-9a-fA-F]{2}){3,4}")
52-
5351
}
5452

5553
override fun annotate(element: PsiElement, holder: AnnotationHolder) {
@@ -149,13 +147,13 @@ class ColorAnnotator : Annotator {
149147
if (initialValue is KtStringTemplateExpression) {
150148

151149
if (initialValue.text.startsWith("\"#")) {
152-
return stringToColor(initialValue.text)
150+
return color(initialValue.text)
153151
}
154152

155153
} else if (initialValue is PsiLiteralExpression) {
156154
if (initialValue.type.isStringType(element)) {
157155
if (initialValue.text.startsWith("\"#")) {
158-
return stringToColor(initialValue.text)
156+
return color(initialValue.text)
159157
}
160158
}
161159

@@ -203,7 +201,7 @@ class ColorAnnotator : Annotator {
203201
if (arg is KtStringTemplateExpression || arg is PsiLiteralExpression) {
204202
if (resolvedCall.second == "valueOf") {
205203
// Color.valueOf(string)
206-
return stringToColor(arg.text)
204+
return color(arg.text)
207205
} else if (resolvedCall.second == "getColor") {
208206
// Skin.getColor(string)
209207
val resourceName = StringUtil.unquoteString(arg.text)
@@ -255,7 +253,7 @@ class ColorAnnotator : Annotator {
255253
floats[i] = float
256254
}
257255
}
258-
return Color(floats[0], floats[1], floats[2], floats[3])
256+
return color(floats[0], floats[1], floats[2], floats[3])
259257
}
260258

261259
} else if (initialValue is PsiCall) {
@@ -280,7 +278,7 @@ class ColorAnnotator : Annotator {
280278
methodCallExpression.resolveCallToStrings()?.let { resolved ->
281279
if (resolved.second == "valueOf") {
282280
// Color.valueOf(String)
283-
return stringToColor(arg.text)
281+
return color(arg.text)
284282
} else if (resolved.second == "getColor") {
285283
// Skin.getColor(string)
286284
methodCallExpression.getAssetFiles().let { (skinFiles) ->
@@ -328,7 +326,7 @@ class ColorAnnotator : Annotator {
328326
}
329327
}
330328
}
331-
return Color(floats[0], floats[1], floats[2], floats[3])
329+
return color(floats[0], floats[1], floats[2], floats[3])
332330
}
333331

334332
}
@@ -648,12 +646,12 @@ class ColorAnnotator : Annotator {
648646

649647
}
650648

651-
private fun rgbaToColor(value: Long): Color {
649+
private fun rgbaToColor(value: Long): Color? {
652650
val r = (value and 0xff000000).ushr(24) / 255f
653651
val g = (value and 0x00ff0000).ushr(16) / 255f
654652
val b = (value and 0x0000ff00).ushr(8) / 255f
655653
val a = (value and 0x000000ff) / 255f
656-
return Color(r, g, b, a)
654+
return color(r, g, b, a)
657655
}
658656

659657
}

src/main/kotlin/com/gmail/blueboxware/libgdxplugin/filetypes/skin/psi/impl/mixins/SkinObjectMixin.kt

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package com.gmail.blueboxware.libgdxplugin.filetypes.skin.psi.impl.mixins
22

33
import com.gmail.blueboxware.libgdxplugin.filetypes.skin.psi.*
44
import com.gmail.blueboxware.libgdxplugin.filetypes.skin.psi.impl.SkinValueImpl
5+
import com.gmail.blueboxware.libgdxplugin.utils.color
56
import com.gmail.blueboxware.libgdxplugin.utils.findParentWhichIsChildOf
6-
import com.gmail.blueboxware.libgdxplugin.utils.stringToColor
77
import com.intellij.icons.AllIcons
88
import com.intellij.lang.ASTNode
99
import com.intellij.navigation.ItemPresentation
@@ -101,12 +101,12 @@ abstract class SkinObjectMixin(node: ASTNode) : SkinObject, SkinValueImpl(node)
101101

102102
override fun asColor(force: Boolean): Color? {
103103

104-
var color: Color? = null
104+
var thisColor: Color? = null
105105

106106
if (propertyList.size == 1 && propertyList.firstOrNull()?.name == "hex") {
107107

108108
(propertyList.firstOrNull()?.value as? SkinStringLiteral)?.value?.let { string ->
109-
color = stringToColor(string)
109+
thisColor = color(string)
110110
}
111111

112112
} else if (propertyList.size == 3 || propertyList.size == 4 || force) {
@@ -132,17 +132,13 @@ abstract class SkinObjectMixin(node: ASTNode) : SkinObject, SkinValueImpl(node)
132132

133133
if (force || (r != null && g != null && b != null)) {
134134

135-
try {
136-
color = Color(r ?: 1f, g ?: 1f, b ?: 1f, a)
137-
} catch (e: IllegalArgumentException) {
138-
// Do nothing
139-
}
135+
thisColor = color(r ?: 1f, g ?: 1f, b ?: 1f, a)
140136

141137
}
142138

143139
}
144140

145-
return color
141+
return thisColor
146142

147143
}
148144

src/main/kotlin/com/gmail/blueboxware/libgdxplugin/ui/LibGDXColorObjectRenderer.kt

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.gmail.blueboxware.libgdxplugin.ui
22

3+
import com.gmail.blueboxware.libgdxplugin.utils.color
34
import com.intellij.debugger.engine.evaluation.EvaluationContext
45
import com.intellij.debugger.settings.NodeRendererSettings
56
import com.intellij.debugger.ui.tree.ValueDescriptor
@@ -9,7 +10,6 @@ import com.intellij.util.ui.ColorIcon
910
import com.sun.jdi.ClassNotPreparedException
1011
import com.sun.jdi.FloatValue
1112
import com.sun.jdi.ObjectReference
12-
import java.awt.Color
1313
import java.lang.IllegalArgumentException
1414
import javax.swing.Icon
1515

@@ -49,9 +49,10 @@ class LibGDXColorObjectRenderer(rendererSettings: NodeRendererSettings): Compoun
4949
val b = getValue(value, "b")
5050
val a = getValue(value, "a")
5151

52-
val color = Color(r, g, b, a)
52+
return color(r, g, b, a)?.let {
53+
ColorIcon(16, 12, it, true)
54+
}
5355

54-
return ColorIcon(16, 12, color, true)
5556
}
5657

5758
private fun getValue(objectReference: ObjectReference, fieldName: String): Float {

src/main/kotlin/com/gmail/blueboxware/libgdxplugin/utils/ColorUtils.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.gmail.blueboxware.libgdxplugin.utils
22

3-
import com.gmail.blueboxware.libgdxplugin.annotators.ColorAnnotator
43
import com.intellij.openapi.editor.markup.GutterIconRenderer
54
import com.intellij.util.ui.ColorIcon
65
import com.intellij.util.ui.UIUtil
@@ -22,13 +21,21 @@ import javax.swing.Icon
2221
* See the License for the specific language governing permissions and
2322
* limitations under the License.
2423
*/
25-
internal fun stringToColor(string: String): Color? {
24+
private val COLOR_REGEX = Regex("#?(?:[0-9a-fA-F]{2}){3,4}")
25+
26+
internal fun color(r: Float, g: Float, b: Float, a: Float): Color? =
27+
if (r < 0 || r > 1 || g < 0 || g > 1 || b < 0 || b > 1 || a < 0 || a > 1)
28+
null
29+
else
30+
Color(r, g, b, a)
31+
32+
internal fun color(string: String): Color? {
2633

2734
if (string.length < 6) return null
2835

2936
var str = if (string[0] == '"') string.substring(1, string.length - 1) else string
3037

31-
if (!ColorAnnotator.colorRegex.matches(str)) return null
38+
if (!COLOR_REGEX.matches(str)) return null
3239

3340
if (str[0] == '#') {
3441
str = str.substring(1)
@@ -40,7 +47,7 @@ internal fun stringToColor(string: String): Color? {
4047
val b = Integer.valueOf(str.substring(4, 6), 16)
4148
val a = if (str.length == 8) Integer.valueOf(str.substring(6, 8), 16) else 255
4249

43-
Color(r / 255f, g / 255f, b / 255f, a / 255f)
50+
color(r / 255f, g / 255f, b / 255f, a / 255f)
4451
} catch (e: NumberFormatException) {
4552
null
4653
}

src/main/resources/META-INF/plugin.xml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
<idea-plugin>
1818
<id>com.gmail.blueboxware.libgdxplugin</id>
1919
<name>LibGDX Inspections</name>
20-
<version>1.16</version>
20+
<version>1.15.1</version>
2121
<vendor url="https://github.com/BlueBoxWare/LibGDXPlugin">Blue Box Ware</vendor>
2222

2323
<description><![CDATA[
@@ -30,8 +30,9 @@
3030
]]></description>
3131

3232
<change-notes><![CDATA[
33-
<b>1.16</b>
33+
<b>1.15.1</b>
3434
<ul>
35+
<li>Don't crash on 'invalid' color values (<a href="https://github.com/BlueBoxWare/LibGDXPlugin/issues/13">https://github.com/BlueBoxWare/LibGDXPlugin/issues/13</a>)</li>
3536
<li>Skins: don't treat parent property as special when using a LibGDX version below 1.9.9 </li>
3637
</ul>
3738

src/test/testdata/annotators/colorAnnotator/Java1.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,15 @@ static void f() {
9999
Color zz1 = <weak_warning descr="#ff0000ff">Color.valueOf(new C().c1)</weak_warning>;
100100
Color zz2 = Color.valueOf(<weak_warning descr="#00ff00ff">new C().c2</weak_warning>);
101101

102+
Color e1 = new Color(1.1f, 0f, 0f, 0f);
103+
Color e2 = new Color(-0.1f, 0f, 0f, 0f);
104+
Color e3 = new Color(0f, 2f, 0f, 0f);
105+
Color e4 = new Color(0f, -2f, 0f, 0f);
106+
Color e5 = new Color(0f, 0f, -0.01f, 0f);
107+
Color e6 = new Color(0f, 0f, 1.01f, 0f);
108+
Color e7 = new Color(0f, 0f, 0f, -0.001f);
109+
Color e8 = new Color(0f, 0f, 0f, 1.001f);
110+
102111
}
103112

104113
}

src/test/testdata/filetypes/skin/colorAnnotator/2.skin

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,14 @@
11
{
22
Color: {
33
color1: <weak_warning descr="#ff0000ff">{ r: 1, g: 0, b: 0, a: 1}</weak_warning>
4+
e1: { r: -1, g: 0, b: 0, a: 0 }
5+
e2: { r: 1.01, g: 0, b: 0, a: 0 }
6+
e3: { r: 0, g: -0.01, b: 0, a: 0 }
7+
e4: { r: 0, g: 1.1, b: 0, a: 0 }
8+
e5: { r: 0, g: 0, b: -5, a: 0 }
9+
e6: { r: 0, g: 0, b: 5, a: 0 }
10+
e7: { r: 0, g: 0, b: 0, a: -2 }
11+
e8: { r: 0, g: 0, b: 0, a: 1.22 }
412
}
513

614
com.badlogic.gdx.graphics.Color: {

versions.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
ext {
22

3-
pluginVersion = '1.16'
3+
pluginVersion = '1.15.1'
44

55
kotlinVersion = '1.2.41'
66

0 commit comments

Comments
 (0)