diff --git a/src/main/java/org/apache/bcel/generic/MULTIANEWARRAY.java b/src/main/java/org/apache/bcel/generic/MULTIANEWARRAY.java index 5c2899763f..bdc2f0272c 100644 --- a/src/main/java/org/apache/bcel/generic/MULTIANEWARRAY.java +++ b/src/main/java/org/apache/bcel/generic/MULTIANEWARRAY.java @@ -125,7 +125,7 @@ public ObjectType getLoadClassType(final ConstantPoolGen cpg) { @Override protected void initFromFile(final ByteSequence bytes, final boolean wide) throws IOException { super.initFromFile(bytes, wide); - dimensions = bytes.readByte(); + dimensions = (short) bytes.readUnsignedByte(); super.setLength(4); } diff --git a/src/test/java/org/apache/bcel/generic/MULTIANEWARRAYTest.java b/src/test/java/org/apache/bcel/generic/MULTIANEWARRAYTest.java new file mode 100644 index 0000000000..c991319b8d --- /dev/null +++ b/src/test/java/org/apache/bcel/generic/MULTIANEWARRAYTest.java @@ -0,0 +1,42 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.bcel.generic; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +import org.apache.bcel.Const; +import org.apache.bcel.util.ByteSequence; +import org.junit.jupiter.api.Test; + +class MULTIANEWARRAYTest { + + /** + * The {@code dimensions} operand of {@code multianewarray} is an unsigned byte (1-255), so a value above 127 must not be + * read as a negative number. + */ + @Test + void testInitFromFileReadsDimensionsUnsigned() throws Exception { + // multianewarray, index 0x0005, dimensions 0xC8 (200) + final byte[] code = {(byte) Const.MULTIANEWARRAY, 0x00, 0x05, (byte) 0xC8}; + try (ByteSequence bytes = new ByteSequence(code)) { + final MULTIANEWARRAY instruction = (MULTIANEWARRAY) Instruction.readInstruction(bytes); + assertEquals(200, instruction.getDimensions()); + } + } +}