diff --git a/src/io/nayuki/fastqrcodegen/BitBuffer.java b/src/io/nayuki/fastqrcodegen/BitBuffer.java index a2d573e..f187e9f 100644 --- a/src/io/nayuki/fastqrcodegen/BitBuffer.java +++ b/src/io/nayuki/fastqrcodegen/BitBuffer.java @@ -87,6 +87,8 @@ final class BitBuffer { public void appendBits(int val, int len) { if (len < 0 || len > 31 || val >>> len != 0) throw new IllegalArgumentException("Value out of range"); + if (Integer.MAX_VALUE - bitLength < len) + throw new IllegalStateException("Maximum length reached"); if (bitLength + len + 1 > data.length << 5) data = Arrays.copyOf(data, data.length * 2); diff --git a/src/io/nayuki/fastqrcodegen/QrCode.java b/src/io/nayuki/fastqrcodegen/QrCode.java index 42fdaec..4366d5f 100644 --- a/src/io/nayuki/fastqrcodegen/QrCode.java +++ b/src/io/nayuki/fastqrcodegen/QrCode.java @@ -381,6 +381,7 @@ public final class QrCode { // Returns a new byte string representing the given data with the appropriate error correction // codewords appended to it, based on this object's version and error correction level. private byte[] addEccAndInterleave(byte[] data) { + Objects.requireNonNull(data); if (data.length != getNumDataCodewords(version, errorCorrectionLevel)) throw new IllegalArgumentException(); diff --git a/src/io/nayuki/fastqrcodegen/QrSegment.java b/src/io/nayuki/fastqrcodegen/QrSegment.java index ee6a6a2..9f91ac4 100644 --- a/src/io/nayuki/fastqrcodegen/QrSegment.java +++ b/src/io/nayuki/fastqrcodegen/QrSegment.java @@ -146,12 +146,14 @@ public final class QrSegment { */ public static QrSegment makeEci(int assignVal) { BitBuffer bb = new BitBuffer(); - if (0 <= assignVal && assignVal < (1 << 7)) + if (assignVal < 0) + throw new IllegalArgumentException("ECI assignment value out of range"); + else if (assignVal < (1 << 7)) bb.appendBits(assignVal, 8); - else if ((1 << 7) <= assignVal && assignVal < (1 << 14)) { + else if (assignVal < (1 << 14)) { bb.appendBits(2, 2); bb.appendBits(assignVal, 14); - } else if ((1 << 14) <= assignVal && assignVal < 1000000) { + } else if (assignVal < 1_000_000) { bb.appendBits(6, 3); bb.appendBits(assignVal, 21); } else @@ -284,7 +286,7 @@ public final class QrSegment { /*-- Constructor --*/ private Mode(int mode, int... ccbits) { - this.modeBits = mode; + modeBits = mode; numBitsCharCount = ccbits; } @@ -294,6 +296,7 @@ public final class QrSegment { // Returns the bit width of the character count field for a segment in this mode // in a QR Code at the given version number. The result is in the range [0, 16]. int numCharCountBits(int ver) { + assert QrCode.MIN_VERSION <= ver && ver <= QrCode.MAX_VERSION; return numBitsCharCount[(ver + 7) / 17]; }