From ba2d772668a4cd424cb36e4e7947eda71e0f38b4 Mon Sep 17 00:00:00 2001 From: Jack Conradson Date: Wed, 23 Nov 2016 13:57:14 -0800 Subject: [PATCH] Fix a VerifyError bug in Painless (#21765) This bug would cause a VerifyError when scripts using the === operator were comparing a def type against a primitive type since the primitive type wasn't being appropriately boxed. --- .../org/elasticsearch/painless/node/EComp.java | 18 ++++-------------- .../elasticsearch/painless/EqualsTests.java | 14 ++++++++++++++ 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EComp.java b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EComp.java index e78dd8eb729..d66f73ec685 100644 --- a/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EComp.java +++ b/modules/lang-painless/src/main/java/org/elasticsearch/painless/node/EComp.java @@ -148,13 +148,8 @@ public final class EComp extends AExpression { "[" + left.actual.name + "] and [" + right.actual.name + "].")); } - if (promotedType.sort == Sort.DEF) { - left.expected = left.actual; - right.expected = right.actual; - } else { - left.expected = promotedType; - right.expected = promotedType; - } + left.expected = promotedType; + right.expected = promotedType; left = left.cast(variables); right = right.cast(variables); @@ -246,13 +241,8 @@ public final class EComp extends AExpression { "[" + left.actual.name + "] and [" + right.actual.name + "].")); } - if (promotedType.sort == Sort.DEF) { - left.expected = left.actual; - right.expected = right.actual; - } else { - left.expected = promotedType; - right.expected = promotedType; - } + left.expected = promotedType; + right.expected = promotedType; left = left.cast(variables); right = right.cast(variables); diff --git a/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java b/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java index 1df7eb349fb..4698e2b0dcf 100644 --- a/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java +++ b/modules/lang-painless/src/test/java/org/elasticsearch/painless/EqualsTests.java @@ -129,6 +129,13 @@ public class EqualsTests extends ScriptTestCase { assertEquals(0, exec("def a = 1; Object b = new HashMap(); if (a === (Object)b) return 1; else return 0;")); } + public void testBranchEqualsDefAndPrimitive() { + assertEquals(true, exec("def x = 1000; int y = 1000; return x == y;")); + assertEquals(false, exec("def x = 1000; int y = 1000; return x === y;")); + assertEquals(true, exec("def x = 1000; int y = 1000; return y == x;")); + assertEquals(false, exec("def x = 1000; int y = 1000; return y === x;")); + } + public void testBranchNotEquals() { assertEquals(1, exec("def a = (char)'a'; def b = (char)'b'; if (a != b) return 1; else return 0;")); assertEquals(0, exec("def a = (char)'a'; def b = (char)'a'; if (a != b) return 1; else return 0;")); @@ -139,6 +146,13 @@ public class EqualsTests extends ScriptTestCase { assertEquals(1, exec("def a = 1; Object b = new HashMap(); if (a !== (Object)b) return 1; else return 0;")); } + public void testBranchNotEqualsDefAndPrimitive() { + assertEquals(false, exec("def x = 1000; int y = 1000; return x != y;")); + assertEquals(true, exec("def x = 1000; int y = 1000; return x !== y;")); + assertEquals(false, exec("def x = 1000; int y = 1000; return y != x;")); + assertEquals(true, exec("def x = 1000; int y = 1000; return y !== x;")); + } + public void testRightHandNull() { assertEquals(false, exec("HashMap a = new HashMap(); return a == null;")); assertEquals(false, exec("HashMap a = new HashMap(); return a === null;"));