From 36f76322326013009fb59ac62edb3941c4f5a65b Mon Sep 17 00:00:00 2001 From: Jack Conradson Date: Tue, 14 Jun 2016 14:12:23 -0700 Subject: [PATCH 1/2] Correct type checking during casting related to Object. --- .../painless/AnalyzerCaster.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java index e724fa9e810..a37e4a4b0aa 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/AnalyzerCaster.java @@ -40,6 +40,10 @@ public final class AnalyzerCaster { case DEF: return new Cast(actual, Definition.DEF_TYPE, explicit, false, false, true, false); case OBJECT: + if (Definition.OBJECT_TYPE.equals(expected) && internal) + return new Cast(actual, actual, explicit, false, false, false, true); + + break; case BOOL_OBJ: if (internal) return new Cast(actual, actual, explicit, false, false, false, true); @@ -62,6 +66,10 @@ public final class AnalyzerCaster { case DEF: return new Cast(actual, Definition.DEF_TYPE, explicit, false, false, true, false); case OBJECT: + if (Definition.OBJECT_TYPE.equals(expected) && internal) + return new Cast(actual, actual, explicit, false, false, false, true); + + break; case NUMBER: case BYTE_OBJ: if (internal) @@ -117,6 +125,10 @@ public final class AnalyzerCaster { case DEF: return new Cast(actual, Definition.DEF_TYPE, explicit, false, false, true, false); case OBJECT: + if (Definition.OBJECT_TYPE.equals(expected) && internal) + return new Cast(actual, actual, explicit, false, false, false, true); + + break; case NUMBER: case SHORT_OBJ: if (internal) @@ -172,6 +184,10 @@ public final class AnalyzerCaster { case DEF: return new Cast(actual, Definition.DEF_TYPE, explicit, false, false, true, false); case OBJECT: + if (Definition.OBJECT_TYPE.equals(expected) && internal) + return new Cast(actual, actual, explicit, false, false, false, true); + + break; case NUMBER: case CHAR_OBJ: if (internal) @@ -229,6 +245,10 @@ public final class AnalyzerCaster { case DEF: return new Cast(actual, Definition.DEF_TYPE, explicit, false, false, true, false); case OBJECT: + if (Definition.OBJECT_TYPE.equals(expected) && internal) + return new Cast(actual, actual, explicit, false, false, false, true); + + break; case NUMBER: case INT_OBJ: if (internal) @@ -284,6 +304,10 @@ public final class AnalyzerCaster { case DEF: return new Cast(actual, Definition.DEF_TYPE, explicit, false, false, true, false); case OBJECT: + if (Definition.OBJECT_TYPE.equals(expected) && internal) + return new Cast(actual, actual, explicit, false, false, false, true); + + break; case NUMBER: case LONG_OBJ: if (internal) @@ -339,6 +363,10 @@ public final class AnalyzerCaster { case DEF: return new Cast(actual, Definition.DEF_TYPE, explicit, false, false, true, false); case OBJECT: + if (Definition.OBJECT_TYPE.equals(expected) && internal) + return new Cast(actual, actual, explicit, false, false, false, true); + + break; case NUMBER: case FLOAT_OBJ: if (internal) @@ -392,6 +420,10 @@ public final class AnalyzerCaster { case DEF: return new Cast(actual, Definition.DEF_TYPE, explicit, false, false, true, false); case OBJECT: + if (Definition.OBJECT_TYPE.equals(expected) && internal) + return new Cast(actual, actual, explicit, false, false, false, true); + + break; case NUMBER: case DOUBLE_OBJ: if (internal) @@ -432,6 +464,45 @@ public final class AnalyzerCaster { break; case OBJECT: + if (Definition.OBJECT_TYPE.equals(actual)) + switch (expected.sort) { + case BYTE: + if (internal && explicit) + return new Cast(actual, Definition.BYTE_OBJ_TYPE, true, false, true, false, false); + + break; + case SHORT: + if (internal && explicit) + return new Cast(actual, Definition.SHORT_OBJ_TYPE, true, false, true, false, false); + + break; + case CHAR: + if (internal && explicit) + return new Cast(actual, Definition.CHAR_OBJ_TYPE, true, false, true, false, false); + + break; + case INT: + if (internal && explicit) + return new Cast(actual, Definition.INT_OBJ_TYPE, true, false, true, false, false); + + break; + case LONG: + if (internal && explicit) + return new Cast(actual, Definition.LONG_OBJ_TYPE, true, false, true, false, false); + + break; + case FLOAT: + if (internal && explicit) + return new Cast(actual, Definition.FLOAT_OBJ_TYPE, true, false, true, false, false); + + break; + case DOUBLE: + if (internal && explicit) + return new Cast(actual, Definition.DOUBLE_OBJ_TYPE, true, false, true, false, false); + + break; + } + break; case NUMBER: switch (expected.sort) { case BYTE: From 5cef06fa3eff6de5ff7c62e99b7e5a0cfecac302 Mon Sep 17 00:00:00 2001 From: Jack Conradson Date: Tue, 14 Jun 2016 14:28:47 -0700 Subject: [PATCH 2/2] Add a test. --- .../org/elasticsearch/painless/WhenThingsGoWrongTests.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java index 7a597132227..a9e03095297 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/WhenThingsGoWrongTests.java @@ -229,4 +229,9 @@ public class WhenThingsGoWrongTests extends ScriptTestCase { assertThat(e.getMessage(), containsString("\\ujjjj")); } + public void testBadBoxingCast() { + expectScriptThrows(ClassCastException.class, () -> { + exec("BitSet bs = new BitSet(); bs.and(2);"); + }); + } }