From 0cf9ec342f6fed9a82a65072f987e22a39ea63df Mon Sep 17 00:00:00 2001 From: srowen Date: Tue, 14 Feb 2012 09:51:59 +0000 Subject: [PATCH] Avoid some rare IllegalArgumentException and ArrayIndexOutOfBoundsException on bad input git-svn-id: https://zxing.googlecode.com/svn/trunk@2184 59b500cc-1b3d-0410-9834-0bbf25fbcc57 --- core/src/com/google/zxing/common/CharacterSetECI.java | 6 ++++-- .../zxing/qrcode/decoder/DecodedBitStreamParser.java | 7 +++++-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/core/src/com/google/zxing/common/CharacterSetECI.java b/core/src/com/google/zxing/common/CharacterSetECI.java index 492a9dc48..7dc171ed1 100644 --- a/core/src/com/google/zxing/common/CharacterSetECI.java +++ b/core/src/com/google/zxing/common/CharacterSetECI.java @@ -16,6 +16,8 @@ package com.google.zxing.common; +import com.google.zxing.FormatException; + import java.util.HashMap; import java.util.Map; @@ -97,9 +99,9 @@ public enum CharacterSetECI { * unsupported * @throws IllegalArgumentException if ECI value is invalid */ - public static CharacterSetECI getCharacterSetECIByValue(int value) { + public static CharacterSetECI getCharacterSetECIByValue(int value) throws FormatException { if (value < 0 || value >= 900) { - throw new IllegalArgumentException("Bad ECI value: " + value); + throw FormatException.getFormatInstance(); } return VALUE_TO_ECI.get(value); } diff --git a/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java b/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java index a2bbd2137..c003e45ee 100644 --- a/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java +++ b/core/src/com/google/zxing/qrcode/decoder/DecodedBitStreamParser.java @@ -248,6 +248,9 @@ final class DecodedBitStreamParser { // Read two characters at a time int start = result.length(); while (count > 1) { + if (bits.available() < 11) { + throw FormatException.getFormatInstance(); + } int nextTwoCharsBits = bits.readBits(11); result.append(toAlphaNumericChar(nextTwoCharsBits / 45)); result.append(toAlphaNumericChar(nextTwoCharsBits % 45)); @@ -316,7 +319,7 @@ final class DecodedBitStreamParser { } } - private static int parseECIValue(BitSource bits) { + private static int parseECIValue(BitSource bits) throws FormatException { int firstByte = bits.readBits(8); if ((firstByte & 0x80) == 0) { // just one byte @@ -332,7 +335,7 @@ final class DecodedBitStreamParser { int secondThirdBytes = bits.readBits(16); return ((firstByte & 0x1F) << 16) | secondThirdBytes; } - throw new IllegalArgumentException("Bad ECI bits starting with byte " + firstByte); + throw FormatException.getFormatInstance(); } }