From ac91c54ce1b5904a65adcd15dfc1dd7b94bcafe3 Mon Sep 17 00:00:00 2001 From: Project Nayuki Date: Fri, 21 Apr 2017 15:37:59 +0000 Subject: [PATCH] Fixed QrSegment.getTotalBits() Java logic to catch integer overflow correctly (related to the C++ fix in commit 873652a82f21). --- java/io/nayuki/qrcodegen/QrSegment.java | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/java/io/nayuki/qrcodegen/QrSegment.java b/java/io/nayuki/qrcodegen/QrSegment.java index 59aa5df..6c0ff5a 100644 --- a/java/io/nayuki/qrcodegen/QrSegment.java +++ b/java/io/nayuki/qrcodegen/QrSegment.java @@ -189,16 +189,18 @@ public final class QrSegment { if (version < 1 || version > 40) throw new IllegalArgumentException("Version number out of range"); - int result = 0; + long result = 0; for (QrSegment seg : segs) { Objects.requireNonNull(seg); int ccbits = seg.mode.numCharCountBits(version); // Fail if segment length value doesn't fit in the length field's bit-width if (seg.numChars >= (1 << ccbits)) return -1; - result += 4 + ccbits + seg.bitLength; + result += 4L + ccbits + seg.bitLength; + if (result > Integer.MAX_VALUE) + return -1; } - return result; + return (int)result; }