This commit is contained in:
Alex Melkonyan 2019-08-22 12:31:28 +01:00 committed by Ben Gruver
parent 3cccf68502
commit b044a00353
2 changed files with 26 additions and 3 deletions

View File

@ -220,13 +220,13 @@ public class Preconditions {
}
public static <L extends List<? extends Number>> L checkArrayPayloadElements(int elementWidth, L elements) {
// mask of all bits that do not fit into an 'elementWidth'-bit number
long bitmask = -1L << elementWidth;
// mask of all bits that do not fit into an 'elementWidth'-byte number
long bitmask = -1L << (elementWidth * 8);
for (Number element : elements) {
if ((element.longValue() & bitmask) != 0) {
throw new IllegalArgumentException(
String.format("Number %d must fit into a %d-bit number", element.longValue(), elementWidth));
String.format("Number %d must fit into a %d-byte number", element.longValue(), elementWidth));
}
}

View File

@ -0,0 +1,23 @@
package org.jf.util;
import java.util.ArrayList;
import java.util.List;
import org.jf.dexlib2.Opcode;
import org.jf.dexlib2.iface.instruction.formats.ArrayPayload;
import org.jf.dexlib2.util.Preconditions;
import org.junit.Assert;
import org.junit.Test;
public class PreconditionsTest {
@Test
public void checkArrayPayloadElements() {
int intSize = 4;
int bigNumber = 16843071;
List<Number> numbers = new ArrayList<>();
numbers.add(bigNumber);
// Shouldn't throw any exceptions, payload is correct.
Preconditions.checkArrayPayloadElements(intSize, numbers);
}
}