From b756fcd8b17de72a2a2060ae5a4490818cac3730 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Wed, 22 Aug 2018 18:03:17 +0000 Subject: [PATCH] Tweaked Java BitBuffer code to add size overflow checks. --- java/io/nayuki/qrcodegen/BitBuffer.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/java/io/nayuki/qrcodegen/BitBuffer.java b/java/io/nayuki/qrcodegen/BitBuffer.java index 8da04ee..b2b62b8 100644 --- a/java/io/nayuki/qrcodegen/BitBuffer.java +++ b/java/io/nayuki/qrcodegen/BitBuffer.java @@ -94,10 +94,14 @@ public final class BitBuffer implements Cloneable { * to this sequence. Requires 0 ≤ val < 2len. * @param val the value to append * @param len the number of low bits in the value to take + * @throws IllegalStateException if appending the data + * would make bitLength exceed Integer.MAX_VALUE */ 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"); for (int i = len - 1; i >= 0; i--, bitLength++) // Append bit by bit data.set(bitLength, QrCode.getBit(val, i)); } @@ -107,10 +111,14 @@ public final class BitBuffer implements Cloneable { * Appends the bit data of the specified segment to this bit buffer. * @param seg the segment whose data to append (not {@code null}) * @throws NullPointerException if the segment is {@code null} + * @throws IllegalStateException if appending the data + * would make bitLength exceed Integer.MAX_VALUE */ public void appendData(QrSegment seg) { Objects.requireNonNull(seg); BitBuffer bb = seg.data; + if (Integer.MAX_VALUE - bitLength < bb.bitLength) + throw new IllegalStateException("Maximum length reached"); for (int i = 0; i < bb.bitLength; i++, bitLength++) // Append bit by bit data.set(bitLength, bb.data.get(i)); }