diff --git a/asm-commons/src/main/java/org/objectweb/asm/commons/GeneratorAdapter.java b/asm-commons/src/main/java/org/objectweb/asm/commons/GeneratorAdapter.java index 44227784a65a969433dc996d3207476f6b720ccb..b8adb4cc6b9b0e9dfc26e324c1459a8651c2a97c 100644 --- a/asm-commons/src/main/java/org/objectweb/asm/commons/GeneratorAdapter.java +++ b/asm-commons/src/main/java/org/objectweb/asm/commons/GeneratorAdapter.java @@ -741,6 +741,12 @@ public class GeneratorAdapter extends LocalVariablesSorter { */ public void cast(final Type from, final Type to) { if (from != to) { + if (from.getSort() < Type.BOOLEAN + || from.getSort() > Type.DOUBLE + || to.getSort() < Type.BOOLEAN + || to.getSort() > Type.DOUBLE) { + throw new IllegalArgumentException("Cannot cast from " + from + " to " + to); + } if (from == Type.DOUBLE_TYPE) { if (to == Type.FLOAT_TYPE) { mv.visitInsn(Opcodes.D2F); @@ -781,8 +787,6 @@ public class GeneratorAdapter extends LocalVariablesSorter { mv.visitInsn(Opcodes.I2L); } else if (to == Type.SHORT_TYPE) { mv.visitInsn(Opcodes.I2S); - } else { - throw new IllegalArgumentException(); } } } diff --git a/asm-commons/src/test/java/org/objectweb/asm/commons/GeneratorAdapterTest.java b/asm-commons/src/test/java/org/objectweb/asm/commons/GeneratorAdapterTest.java index 09968813d1efa3dd1b8ed251abec0bdfa935b56f..654c75e984b1628c0d79e0b0698aea4efd7bdd2d 100644 --- a/asm-commons/src/test/java/org/objectweb/asm/commons/GeneratorAdapterTest.java +++ b/asm-commons/src/test/java/org/objectweb/asm/commons/GeneratorAdapterTest.java @@ -407,6 +407,11 @@ public class GeneratorAdapterTest { assertEquals("I2C", new Generator().cast(Type.INT_TYPE, Type.CHAR_TYPE)); assertEquals("I2S", new Generator().cast(Type.INT_TYPE, Type.SHORT_TYPE)); + assertEquals("", new Generator().cast(Type.BYTE_TYPE, Type.INT_TYPE)); + assertEquals("", new Generator().cast(Type.SHORT_TYPE, Type.INT_TYPE)); + + assertThrows( + IllegalArgumentException.class, () -> new Generator().cast(Type.VOID_TYPE, Type.INT_TYPE)); assertThrows( IllegalArgumentException.class, () -> new Generator().cast(Type.INT_TYPE, Type.VOID_TYPE)); }