Check length in byte array constructor
@@ -81,9 +81,16 @@ public static DERBMPString getInstance(
DERBMPString(
byte[] string)
{
- char[] cs = new char[string.length / 2];
+ int byteLen = string.length;
+ if (0 != (byteLen & 1))
+ {
+ throw new IllegalArgumentException("malformed BMPString encoding encountered");
+ }
+
+ int charLen = byteLen / 2;
+ char[] cs = new char[charLen];
- for (int i = 0; i != cs.length; i++)
+ for (int i = 0; i != charLen; i++)
{
cs[i] = (char)((string[2 * i] << 8) | (string[2 * i + 1] & 0xff));
}
