Added and updated some strict overflow checks.

pull/134/head
Project Nayuki 6 years ago
parent 83a3c3b957
commit b967ae4aae

@ -92,7 +92,7 @@ final class BitBuffer {
public void appendBits(int val, int len) { public void appendBits(int val, int len) {
if (len < 0 || len > 31 || val >>> len != 0) if (len < 0 || len > 31 || val >>> len != 0)
throw new IllegalArgumentException("Value out of range"); throw new IllegalArgumentException("Value out of range");
if (Integer.MAX_VALUE - bitLength < len) if (len > Integer.MAX_VALUE - bitLength)
throw new IllegalStateException("Maximum length reached"); throw new IllegalStateException("Maximum length reached");
if (bitLength + len + 1 > data.length << 5) if (bitLength + len + 1 > data.length << 5)
@ -118,12 +118,14 @@ final class BitBuffer {
Objects.requireNonNull(vals); Objects.requireNonNull(vals);
if (len == 0) if (len == 0)
return; return;
if (len < 0 || len > vals.length * 32) if (len < 0 || len > vals.length * 32L)
throw new IllegalArgumentException("Value out of range"); throw new IllegalArgumentException("Value out of range");
int wholeWords = len / 32; int wholeWords = len / 32;
int tailBits = len % 32; int tailBits = len % 32;
if (tailBits > 0 && vals[wholeWords] << tailBits != 0) if (tailBits > 0 && vals[wholeWords] << tailBits != 0)
throw new IllegalArgumentException("Last word must have low bits clear"); throw new IllegalArgumentException("Last word must have low bits clear");
if (len > Integer.MAX_VALUE - bitLength)
throw new IllegalStateException("Maximum length reached");
while (bitLength + len > data.length * 32) while (bitLength + len > data.length * 32)
data = Arrays.copyOf(data, data.length * 2); data = Arrays.copyOf(data, data.length * 2);

@ -57,6 +57,8 @@ public final class QrSegment {
*/ */
public static QrSegment makeBytes(byte[] data) { public static QrSegment makeBytes(byte[] data) {
Objects.requireNonNull(data); Objects.requireNonNull(data);
if (data.length * 8L > Integer.MAX_VALUE)
throw new IllegalArgumentException("Data too long");
int[] bits = new int[(data.length + 3) / 4]; int[] bits = new int[(data.length + 3) / 4];
for (int i = 0; i < data.length; i++) for (int i = 0; i < data.length; i++)
bits[i >>> 2] |= (data[i] & 0xFF) << (~i << 3); bits[i >>> 2] |= (data[i] & 0xFF) << (~i << 3);
@ -243,7 +245,7 @@ public final class QrSegment {
public QrSegment(Mode md, int numCh, int[] data, int bitLen) { public QrSegment(Mode md, int numCh, int[] data, int bitLen) {
mode = Objects.requireNonNull(md); mode = Objects.requireNonNull(md);
this.data = Objects.requireNonNull(data); this.data = Objects.requireNonNull(data);
if (numCh < 0 || bitLen < 0 || bitLen > data.length * 32) if (numCh < 0 || bitLen < 0 || bitLen > data.length * 32L)
throw new IllegalArgumentException("Invalid value"); throw new IllegalArgumentException("Invalid value");
numChars = numCh; numChars = numCh;
bitLength = bitLen; bitLength = bitLen;

Loading…
Cancel
Save